0

当我玩它时,Python 没有响应。也没有出现语法错误。不知道出了什么问题。我尝试运行我制作的另一款游戏,它运行良好,所以我认为它不是我的电脑。 错误信息

这是我的代码:

import pygame 

pygame.init()

screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("Draft")

icon = pygame.image.load("doctor.png")

pygame.display.set_icon(icon)

white = (255,255,255)
black = (0,0,0) red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

def draw_ground():
    groundx = 20
    groundy = 30
    Ground = pygame.Rect(groundx,groundy,width = 800,height = 20)
    pygame.draw.rect(screen,green,Ground)

running = True
while running:

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

    draw_ground()

它还没有完成,我正在尝试在继续之前先对其进行测试。

4

1 回答 1

3

您的代码中有一个错字:

black = (0,0,0) red = (255,0,0)

它一定要是

black = (0,0,0) 
red = (255,0,0)

然而,还有更多。


pygame.Rect不接受关键字参数:

Ground = pygame.Rect(groundx,groundy,width = 800,height = 20)

Ground = pygame.Rect(groundx,groundy,800,20)

您必须通过调用pygame.display.update()或更新显示pygame.display.flip()

clock = pygame.time.Clock()
running = True
while running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # clear dispaly
    screen.fill(0)

    # draw objects
    draw_ground()

    # update dispaly
    pygame.display.flip()

典型的 PyGame 应用程序循环必须:

于 2021-10-26T07:21:12.897 回答