首页 > 解决方案 > 我的 pygame 窗口不断崩溃,我正在关注 mac 上的 youtube 教程

问题描述

我对 python 很陌生,我一直在关注 Tech With Tim 的 pygame 教程,但我的窗口没有响应或关闭。我正在使用 Mac,但我不知道这是否会改变任何东西......而且我不断收到错误消息

错误:

function missing required argument 'rect' (pos 3).

代码:

import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))

pygame.display.set_caption("First game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        x -= vel
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel
    
    pygame.draw.rect(win, (255, 0, 0))
    pygame.display.update()

pygame.quit()

标签: pythonpygame

解决方案


rect函数至少需要三个参数,只给出了 2 个:

pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))

它也应该在mac上工作

检查 pygame 文档以供参考:https ://www.pygame.org/docs/ref/draw.html#pygame.draw.rect


推荐阅读