我在python IDLE中创建了一个蛇游戏,但第24行有一个属性错误,我不太清楚这意味着什么。有人知道吗?游戏开始工作,因为它显示了我的矩形和屏幕,但它只是一个黑色的窗口。不知道我哪里出错了......这是我的一级课程。
from typing import List, Tuple
import pygame
FPS = 10
WINDOW_WIDTH = 900 #how big the window is going to be
WINDOW_HEIGHT = 900
class Snake(object):
WIDTH = 40 #how big ther snakes body starts off
HEIGHT = 40
def __init__(self):
self.pos_a = 1
self.pos_b = 1 #position it starts in
self.vel_a = 0
self.vel_b =0
def update(self, events: List[pygame.event.Event]) -> None:
self.pos_a += self.vel_a
self.pos_a += self.vel_b
self.vel_a, self.vel_b = self.handle_mov(events)
def draw(self,screen: pygame.Surface):
pygame.draw.rect( #what colour the snake is
screen,
pygame.color.Color("purple"),
(
self.pos_a,
self.pos_b,
self.WIDTH, #spelt it colour
self.HEIGHT,
), 0
)
def handle_mov(self, events: List[pygame.event.Event]) -> Tuple[int, int]:
travelling = (self.vel_a, self.vel_b)
for ev in events:
if ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_UP:
travelling = (0, -1)
elif ev.key == pygame.K_DOWN:
travelling = (0, 1)
elif ev.key == pygame.K_LEFT:
travelling = (-1, 0)
elif ev.key == pygame.K_RIGHT:
travelling = (1, 0)
return travelling
def run() -> None:
pygame.init()
screen = pygame.display.set_mode(
(WINDOW_WIDTH, WINDOW_HEIGHT), #syntax error on height
pygame.HWSURFACE
)
snakegame_loop(screen)
def snakegame_loop(screen: pygame.Surface) -> None:
snake = Snake()
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
handle_quit(events)
screen.fill(pygame.color.Color("pink"))
snake.update(events)
snake.draw(screen) #the screen color
pygame.display.update() #updating all the adjustments added
clock.tick(FPS)
def handle_quit(events: List[pygame.event.Event]) -> None:
for ev in events:
if ev.type == pygame.QUIT:
exit(0)
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
exit(0)
run()