日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python初學者該如何解決pygame.error: video

系統 1761 0

按照其他博客,碼了最簡單的游戲(貪吃蛇),出現bug,pygame.error: video system not initialized,無法解決,pygame.init()也多次調用,無法解決,先放在博客上,等會用了再來解決,使用的工具是pucharm

            
              import pygame,sys,random,time
from pygame.locals import *
#定義顏色變量 0=黑色  255=白色
redColor=pygame.Color(255,0,0)
blackColor=pygame.Color(0,0,0)
whiteColor=pygame.Color(255,255,255)
grayColor=pygame.Color(150,150,150)

#定義gameOver()
def gameOver(playSurface):
    gameOverFont = pygame.font.Font('arial.ttf', 72)
    gameOverSurf = gameOverFont.render('Game Over', True, greyColour)
    gameOverRect = gameOverSurf.get_rect()
    gameOverRect.midtop = (320, 10)
    playSurface.blit(gameOverSurf, gameOverRect)
    pygame.display.flip()
    time.sleep(5)
    pygame.quit()
    sys.exit()

def main():
    pygame.init()
    fpsClock = pygame.time.Clock()
    playSurface=pygame.display.set_mode((640,480))
    pygame.display.set_caption("貪吃蛇")

#初始化變量
snakePosition = [100,100]
snakeSegments=[[100,100],[80,100],[60,100]]
cheesePosition=[300,300]
cheeseSpawned= 1
dirction ='right'
changDirection=dirction
while True:
    #檢測列如按鍵等pygame的事件:
    for event in pygame.event.get():
        # 從隊列中獲取事件
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT or event.key==ord('d'):
                changeDirection='right'
            if event.key == K_LEFT or event.key==ord('a'):
                changeDirection='left'
            if event.key == K_UP or event.key==ord('w'):
                changeDirection='up'
            if event.key == K_Down or event.key==ord('s'):
                changeDirection='down'
            if event.key == K_ESCAPE :
                pygame.quit()
#direction 是否正確
if changeDirection =='right' and not direction == 'left':
    direction = changeDirection
if changeDirection =='left' and not direction == 'right':
    direction = changeDirection
if changeDirection =='up' and not direction == 'down':
    direction = changeDirection
if changeDirection =='down' and not direction == 'up':
    direction = changeDirection
#根據方向移動蛇頭
if direction == 'right':
    snakePosition[0] += 20
if direction == 'left':
    snakePosition[0] -= 20
if direction == 'up':
    snakePosition[0] -= 20
if direction == 'down':
    snakePosition[0] += 20

#判斷是否吃掉了奶酪
if snakePosition[0]==cheesePosition[0] and snakePosition[1]==cheesePosition[1]:
    cheeseSpawned =0
else:
    snakeSeqments.pop()
if cheeseSpawned==0:
    x=random.randrange(1,32)
    y=random.randrange(1,24)
    cheesePosition=[int(x*20),int(y*20)]
    cheeseSpawned=1

playSurface.fill(blackColor)
for position in snakeSegments:
    pygame.draw.rect(playSurface, whiteColour, Rect(position[0], position[1], 20, 20))
    pygame.draw.rect(playSurface, redColour, Rect(cheesePosition[0], cheesePosition[1], 20, 20))

    pygame.display.flip()
    if snakePosition[0]>620 or snakePosition(0)<0:
        gameOver(playSurface)
    if snakePosition[1]>460 or snakePosition[1]<0:
        for snakeBody in snakeSegments[1:]:
            if snakePosition[0]==snakeBody[0] and snakePosition[1]==snakeBody[1]:
                gamerOver(playSurface)
        fpsClock.tick(5)

if _name_ =="_main_":
 main()

            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 城口县| 深泽县| 蒙城县| 龙门县| 定兴县| 洞头县| 嵩明县| 潢川县| 蒙阴县| 双辽市| 延安市| 三门峡市| 肥西县| 绥棱县| 洛扎县| 襄樊市| 普定县| 长治市| 三穗县| 曲沃县| 电白县| 通渭县| 巴彦淖尔市| 霍山县| 罗江县| 阿瓦提县| 抚松县| 宜兰县| 阿合奇县| 富宁县| 昭苏县| 阿瓦提县| 同德县| 遵义县| 红原县| 广昌县| 方正县| 辛集市| 称多县| 万安县| 台州市|