问题描述
我尝试复制游戏,但矩形(这是我需要的基本形状)没有出现。我做错了什么还是只是 pygame 发疯了?
代码:
# importing modules
import pygame
import sys
import random
# starting pygame
pygame.init()
# making a screen
(width,height) = (500,500)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.display.quit()
pygame.quit()
exit()
# colors
white = (255,255,255)
blue = (0,255)
red = (255,0)
green = (4,0)
# cube
if running == True:
pygame.draw.rect(screen,blue,(395,10,10)),pygame.draw.rect(screen,20,clock.tick(60)
还有我将如何使它成为空的和 3d 的。我知道我要求很多,但我相信有人可以解释
解决方法
您必须在应用程序循环中绘制场景:
# importing modules
import pygame
import sys
import random
# colors
white = (255,255,255)
blue = (0,255)
red = (255,0)
green = (4,0)
# starting pygame
pygame.init()
# making a screen
(width,height) = (500,500)
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.rect(screen,blue,(395,10,10))
pygame.draw.rect(screen,20,10))
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
exit()
典型的 PyGame 应用程序循环必须:
- 通过
pygame.event.pump()
或pygame.event.get()
处理事件。 - 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象) - 通过
pygame.display.update()
或pygame.display.flip()
更新显示 - 使用
pygame.time.Clock.tick
限制每秒帧数以限制 CPU 使用率