PyGame运行报错:TypeError: draw_window()缺少red和yellow参数
解决
TypeError: draw_window() missing 2 required positional arguments: 'red' and 'yellow'问题 错误原因很明确:你定义的draw_window(red, yellow)函数声明需要接收red和yellow两个参数,但在main函数的循环中调用draw_window()时,没有传递这两个必要的Rect对象。
只需要修改main函数里的调用语句,把red和yellow作为参数传入即可:
def main(): red = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) yellow = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) clock = pygame.time.Clock() run = True while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False red.x += 1 # 传入red和yellow参数 draw_window(red, yellow) pygame.quit()
修改后的完整代码:
import pygame import os WIDTH, HEIGHT = 900, 500 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('Simple Game') WHITE = (255, 255, 255) FPS = 60 SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40 YELLOW_SPACESHIP_IMAGE = pygame.image.load( os.path.join('Assets', 'spaceship_yellow.png')) YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale( YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90 ) RED_SPACESHIP_IMAGE = pygame.image.load( os.path.join('Assets', 'spaceship_red.png')) RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale( RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), -90) def draw_window(red, yellow): WIN.fill(WHITE) WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y)) WIN.blit(RED_SPACESHIP, (red.x, red.y)) pygame.display.update() def main(): red = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) yellow = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT) clock = pygame.time.Clock() run = True while run: clock.tick(FPS) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False red.x += 1 draw_window(red, yellow) pygame.quit() if __name__ == "__main__": main()
内容的提问来源于stack exchange,提问作者DerpySmiley
相关产品推荐
相关产品推荐

