基本上,当用户单击新游戏时,我已经被困了几个小时试图创建一个空白模板。我告诉代码然后取消 while 循环并使用 Background.fill() 函数创建一个空白屏幕,但它不起作用。这是我的代码。
import pygame
import random
import sys
from pygame import *
def text():
font = pygame.font.SysFont("monospace", 40)
surfacefont = font.render("Tower Defense", True, black)
surfaceR = surfacefont.get_rect()
surfaceR.center = (400,50)
Background.blit(surfacefont, surfaceR)
text = font.render("Start game", True, black)
textpos = surfacefont.get_rect()
textpos.center = (425,200)
Background.blit(text, textpos)
text = font.render("Exit Game", True, black)
textpos = surfacefont.get_rect()
textpos.center = (435,260)
Background.blit(surfacefont, surfaceR)
Background.blit(text, textpos)
pygame.display.update()
def button():
click = pygame.mouse.get_pressed()
pos = pygame.mouse.get_pos()
if 285+205 > pos[0] > 285 and 175+50 > pos[1] > 175:
pygame.draw.rect(Background, (100,100,100),(263,175,250,50))
if click[0] == 1:
cancelLoop()
else:
pygame.draw.rect(Background, white,(285,175,205,50))
if 278+220 > pos[0] > 278 and 234+50 > pos[1] > 234:
pygame.draw.rect(Background, (100,100,100),(278,234,220,50))
if click[0] == 1:
pygame.quit()
sys.exit()
else:
pygame.draw.rect(Background, white,(278,234,220,50))
def cancelLoop():
global intro
intro = 0
runGame()
def runGame():
Background.fill(red)
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
Background = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Tower Defense")
intro = 1
while intro == 1:
Background.fill(white)
button()
text()
for event in pygame.event.get():
if event.type == quit:
pygame.quit()
sys.exit()
pygame.display.update()
pygame.display.flip()