这是我的代码,请记住我几天前选择了python,所以我的代码可能会被错误地制作,等等。我正在尝试创建一个窗口,显示一些文本(测试版),并显示两个我想制作按钮的小矩形。
import sys
import pygame
from pygame.locals import *
pygame.init()
size = width, height = 720, 480
speed = [2, 2]
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
red = (255,0,0)
screen = pygame.display.set_mode(size)
screen.fill((blue))
pygame.display.set_caption("BETA::00.0.1")
clock = pygame.time.Clock()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(blue)
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("BETA", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.draw.rect(gameDisplay, green,(150,450,100,50))
pygame.draw.rect(gameDisplay, red,(550,450,100,50))
pygame.display.flip(screen)
pygame.display.update(screen)
clock.tick(15)
答案 0 :(得分:1)
问题:
game_intro()
函数已定义,但从未调用gameDisplay
而不是screen
4次,display_width, display_height
代替width, height
game_intro()
中,永远不会调用无限循环后的代码pygame.display.update(screen)
不起作用,应该pygame.display.update()
更新整个屏幕。 flip(screen)
也需要更改为flip()
text_objects()
未定义,但我在示例中找到了您可能复制代码的定义:)更正后的代码:
import sys
import pygame
from pygame.locals import *
pygame.init()
size = width, height = 720, 480
speed = [2, 2]
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
red = (255,0,0)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("BETA::00.0.1")
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def game_intro():
screen.fill(blue)
largeText = pygame.font.Font('freesansbold.ttf',115)
TextSurf, TextRect = text_objects("BETA", largeText)
TextRect.center = ((width/2),(height/2))
screen.blit(TextSurf, TextRect)
pygame.draw.rect(screen, green,(150,450,100,50))
pygame.draw.rect(screen, red,(550,450,100,50))
pygame.display.flip()
pygame.display.update()
clock.tick(15)
intro = True
while intro:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_intro()