0

基本上,当用户单击新游戏时,我已经被困了几个小时试图创建一个空白模板。我告诉代码然后取消 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()
4

1 回答 1

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()
于 2016-11-20T21:43:23.107 回答