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

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

系統(tǒng) 1758 0

按照其他博客,碼了最簡單的游戲(貪吃蛇),出現(xiàn)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
#根據(jù)方向移動蛇頭
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()

            
          

更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 福泉市| 蓬莱市| 棋牌| 大田县| 关岭| 鞍山市| 泸溪县| 阜康市| 锡林浩特市| 南汇区| 东平县| 竹溪县| 铁岭县| 陵水| 梁河县| 沛县| 德昌县| 乐东| 彰化县| 福鼎市| 开江县| 临江市| 克什克腾旗| 桦甸市| 鲁甸县| 长阳| 宜春市| 利津县| 左权县| 太仆寺旗| 顺昌县| 新余市| 道孚县| 洛川县| 堆龙德庆县| 山阳县| 石台县| 平和县| 河北区| 隆安县| 安溪县|