基本上,当用户点击新游戏时,我已经被困了几个小时试图创建一个空白模板。我告诉代码然后取消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()
答案 0 :(得分:0)
问题是当你拨打intro == 0
时,你正在有效地退出游戏,因为你取消了你的while循环。因此,该计划将在介绍结束后结束。你可以创建一个主循环,而不是为你的介绍制作一个while循环,也可以调用runGame()
函数,或者你可以为你的介绍和实际游戏制作单独的循环。
这就是我个人写的方式:
import pygame
import random
import sys
from pygame import *
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")
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:
runGame()
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 runGame():
Background.fill(red)
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()