簡體   English   中英

Pygame 事件錯誤

[英]Pygame event bug

我最近開始在 pygame 中制作 Snake。 首先,我在沒有面向對象編程的情況下編寫代碼。 當我注意到類會更好看時,我編輯了代碼。 現在前 3 次,蛇正常移動,但是當我按下向左箭頭或向右箭頭或向下箭頭時,蛇向下移動。 當我沒有課時,該程序按其應有的方式運行。 現在沒有了。 你能幫助我嗎?

這是我的代碼:

import pygame, math, random

pygame.init()

screen = pygame.display.set_mode((640,640))
pygame.display.set_caption('Snake')

class Snake(object):
    def __init__(self):
        self.x, self.y = 320,320
        self.dirUp, self.dirDown = False, False
        self.dirLeft, self.dirRight = False, False
        self.body = [(self.x, self.y)]
        self.snakeImg = [pygame.image.load('snakeblock.png') for i in range(len(self.body))]

snake = Snake()

squares = []
for i in range(640):
    if i % 32 == 0:
        squares.append(i)
food = pygame.image.load('fruit.png')
foodx,foody = random.choice(squares), random.choice(squares)

def isCollision(obsX, obsY, x, y):
#          D   I   S   T   A   N   C   E
    return math.sqrt(math.pow(obsX - x, 2) + math.pow(obsY - y, 2)) <= 0

over_font = pygame.font.Font('freesansbold.ttf',64)
score_font = pygame.font.Font('freesansbold.ttf',32)
score = 0

def show_text():
    score_text = score_font.render('Score: {}'.format(score), True, (255,255,255))
    over_text = over_font.render('GAME OVER', True, (255,255,255))
    screen.blit(score_text, (0,0))

running = True

while running:

    pygame.time.delay(160)
    screen.fill((0,128,0))


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                snake.dirUp = True
                snake.dirLeft, dirDown, dirRight = False, False, False
            if event.key == pygame.K_DOWN:
                snake.dirDown = True
                snake.dirUp, dirLeft, dirRight = False, False, False
            if event.key == pygame.K_RIGHT:
                snake.dirRight = True
                snake.dirUp, dirDown, dirLeft = False, False, False
            if event.key == pygame.K_LEFT:
                snake.dirLeft = True
                snake.dirUp, dirDown, dirRight = False, False, False
    if snake.dirUp:
        snake.y -= 32
    elif snake.dirDown:
        snake.y += 32
    elif snake.dirRight:
        snake.x += 32
    elif snake.dirLeft:
        snake.x -= 32

    for i in range(len(snake.body)):
        if isCollision(foodx,foody,snake.body[i][0],snake.body[i][1]):
            foodx, foody = random.choice(squares), random.choice(squares)
            score += 1
        screen.blit(food, (foodx, foody))
        screen.blit(snake.snakeImg[i], (snake.x, snake.y))

    show_text()
    pygame.display.update()

問題就在這里

snake.dirLeft, dirDown, dirRight = False, False, False

應該是

snake.dirLeft, snake.dirDown, snake.dirRight = False, False, False

在 python 中,除非您從元組/列表中解壓值,否則不建議使用多個賦值。 如您所見,這將更具可讀性:)

snake.dirLeft = False
snake.dirDown = False
snake.dirRight = False

還有另一個問題,因為蛇在創建 Snake 實例的那一刻使他的身體坐標靜止,這是可能的解決方案

class Snake(object):
    def __init__(self):
        self.x, self.y = 320, 320
        self.dirUp, self.dirDown = False, False
        self.dirLeft, self.dirRight = False, False
        self.snakeImg = [pygame.image.load('snakeblock.png')
                         for i in range(len(self.body))]

    @property
    def body(self):
        return [(self.x, self.y)]

但是你還是得想辦法增加它的體型

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM