【问题标题】:It wont draw my rectangle (pygame on prdroid)它不会绘制我的矩形(pydroid 上的 pygame)
【发布时间】:2021-11-29 07:00:07
【问题描述】:

屏幕无法填充,它说屏幕没有填充命令,我做错了什么?

import pygame
    
pygame.init()

screen = pygame.display
set_mode((0, 0))

white = (255, 255, 255)
Square =(0, 0, 0)

loop = True
while loop:
    screen.fill(white)
    pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)
    pygame.display.update()

【问题讨论】:

  • 你能发布完整的回溯吗?
  • 嘿,我是python的新手,基本上刚刚开始学习,我不知道那是什么
  • 这应该在一行screen = pygame.display.set_mode((0, 0))
  • @ChainzGames 你有很多错误screen = pygame.display.set_mode((0, 0)),这就是你修复错误消息的方法,我认为那里也有缩进错误
  • @IainShelvington 我编辑了它,因为我发现一个新的错误 iv 试图在屏幕上添加一个矩形

标签: python pygame pydroid


【解决方案1】:

您的代码中有一些语法错误:

screen = pygame.display
set_mode((0, 0))

screen = pygame.display.set_mode((0, 0))

pygame.draw.rect(screen, Square, pygame)Rect(100, 100, 200, 200)

pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))

不仅如此,还缺少事件循环。这可能会导致您的应用程序冻结。分别见pygame.event.get()pygame.event.pump()

对于游戏的每一帧,您都需要对事件队列进行某种调用。这确保您的程序可以在内部与操作系统的其余部分进行交互。

正确的程序:

import pygame
    
pygame.init()
screen = pygame.display.set_mode((640, 480))

white = (255, 255, 255)
Square = (0, 0, 0)

loop = True
while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = False

    screen.fill(white)
    pygame.draw.rect(screen, Square, pygame.Rect(100, 100, 200, 200))
    pygame.display.update()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-09
相关资源
最近更新 更多