Python Pygame事件错误

Python Pygame事件错误,python,python-3.x,events,pygame,Python,Python 3.x,Events,Pygame,我最近开始在pygame中制作蛇。首先,我是在没有面向对象编程的情况下编写代码的。当我注意到使用类会更好时,我编辑了代码。前三次,蛇正常移动,但之后当我按下左箭头、右箭头或向下箭头时,蛇向下移动。 当我没有课的时候,这个程序就按它应该的方式运行。现在没有了。你能帮我吗 这是我的代码: import pygame, math, random pygame.init() screen = pygame.display.set_mode((640,640)) pygame.display.set_

我最近开始在pygame中制作蛇。首先,我是在没有面向对象编程的情况下编写代码的。当我注意到使用类会更好时,我编辑了代码。前三次,蛇正常移动,但之后当我按下左箭头、右箭头或向下箭头时,蛇向下移动。 当我没有课的时候,这个程序就按它应该的方式运行。现在没有了。你能帮我吗

这是我的代码:

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()
导入pygame、数学、随机
pygame.init()
screen=pygame.display.set_模式((640))
pygame.display.set_标题('Snake'))
类(对象):
定义初始化(自):
self.x,self.y=320320
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')表示范围内的i(len(self.body))]
蛇
正方形=[]
对于范围内的i(640):
如果i%32==0:
正方形。附加(i)
food=pygame.image.load('fruit.png')
foodx,foody=random.choice(正方形),random.choice(正方形)
def isCollision(obsX,obsY,x,y):
#D I S A N C E
返回math.sqrt(math.pow(obsX-x,2)+math.pow(obsY-y,2))问题就在这里

snake.dirLeft, dirDown, dirRight = False, False, False
应该是什么

snake.dirLeft, snake.dirDown, snake.dirRight = False, False, False
在python中,除非您从tuple/list中解包值,否则不建议使用多个assignment。如您所见,这将更具可读性:)

还有另一个问题,因为蛇在创建蛇实例的瞬间,他的身体保持静止,这里有一个可能的解决方案

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)]
但是你还是得想办法增加它的体重

问题就在这里

snake.dirLeft, dirDown, dirRight = False, False, False
应该是什么

snake.dirLeft, snake.dirDown, snake.dirRight = False, False, False
在python中,除非您从tuple/list中解包值,否则不建议使用多个assignment。如您所见,这将更具可读性:)

还有另一个问题,因为蛇在创建蛇实例的瞬间,他的身体保持静止,这里有一个可能的解决方案

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)]
但是你还是要想办法增加它的体积