0
基本上我一直堅持幾個小時試圖創建一個空白模板,當用戶點擊新遊戲。我告訴代碼然後取消while循環並使用Background.fill()函數創建一個空白屏幕,但它不起作用。這是我的代碼。Pygame創建一個新的屏幕
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()
歡迎來到堆棧溢出!它看起來像你需要學習使用調試器。請幫助一些[互補調試技術](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。如果您之後仍然有問題,請隨時返回更多詳情。 –
你只能使用'pygame.display.update()'或'pygame.display.flip()'中的一個,因爲它們基本上是一樣的。任何一個都可以工作,但'update()'針對軟件顯示進行了優化,因此推薦用於這種情況。 –