-5
我想在python(它的一個學校任務)中編寫一個遊戲,我希望遊戲有多個屏幕,例如標題屏幕,關於屏幕和屏幕,玩遊戲。但是,爲了簡單起見,我希望所有這些屏幕都顯示在同一個窗口中。編碼錯誤與Python
目前,我一直在定義運行特定屏幕的命令,並告訴python在變量正確時(即用戶按下按鈕將它們帶到下一個屏幕)運行該命令。但是,我所得到的只是一個黑屏,python會立即關閉。有趣的是日誌顯示沒有任何問題,這很奇怪。
如果你能幫我修復這段代碼,那將不勝感激。
import pygame
import random
name = 'Squash Ninja'
size = (700, 500)
rect_x = 50
rect_y = 50
rect_x1 = 345
rect_y1 = 397
rect_x_change = 10
rect_y_change = 10
rect_x_change1 = -10
rect_y_change1 = -10
vari = 0
#Colours#
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (18, 128, 13)
YELLOW = (234, 255, 0)
ORANGE = (255, 132, 0)
LGREEN = (0,255,0)
PURPLE = (204, 14, 166)
BLACK = (0, 0, 0)
#Starts Pygame#
pygame.init()
#Opens window#
size = (700, 500)
screen = pygame.display.set_mode(size)
#Running Variables#
done = False
title = True
about = False
#Screens#
def title():
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('User pressed mouse.')
elif event.type == pygame.KEYDOWN:
print("User pressed a key")
pygame.draw.line(screen, BLUE, [0,0], [100,100], 5)
elif event.type == pygame.KEYUP:
print("User let go of a key.")
elif event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
pygame.draw.rect(screen,BLACK,[200,20,350,100],0)
pygame.draw.rect(screen,GREEN,[280,180,200,50],0)
pygame.draw.rect(screen,GREEN,[280,240,200,50],0)
pygame.draw.rect(screen,GREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 45, True, False)
text = font.render("Squash Ninja", True, LGREEN)
screen.blit(text, [255, 75])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Play", True, YELLOW)
screen.blit(text, [350, 200])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("About", True, YELLOW)
screen.blit(text, [340, 260])
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Quit", True, YELLOW)
screen.blit(text, [355, 320])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Version 1.2", True,WHITE)
screen.blit(text, [650, 470])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Created by Adrian Ngai", True,WHITE)
screen.blit(text, [600, 480])
font = pygame.font.SysFont('Calibri', 10, True, False)
text = font.render("Copyright 2014, all rights reserved.", True,WHITE)
screen.blit(text, [555, 490])
mouse = pygame.mouse.get_pos()
if 280 + 200 > mouse [0] > 280 and 180+50 > mouse[1] > 180:
pygame.draw.rect(screen,LGREEN,[280,180,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Play", True, RED)
screen.blit(text, [350, 200])
if 280 + 200 > mouse [0] > 280 and 240+50 > mouse[1] > 240:
pygame.draw.rect(screen,LGREEN,[280,240,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("About", True, RED)
screen.blit(text, [340, 260])
if event.type == pygame.MOUSEBUTTONDOWN:
title = False
about = True
if 280 + 200 > mouse [0] > 280 and 300+50 > mouse[1] > 300:
pygame.draw.rect(screen,LGREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Quit", True, RED)
screen.blit(text, [355, 320])
if event.type == pygame.MOUSEBUTTONDOWN:
done = True
#bouncing rectangle#
pygame.draw.rect(screen, LGREEN, [rect_x, rect_y, 50, 50])
rect_x += rect_x_change
rect_y += rect_y_change
if rect_y > 450 or rect_y < 0:
rect_y_change = rect_y_change * -1
if rect_x > 650 or rect_x < 0:
rect_x_change = rect_x_change * -1
pygame.draw.rect(screen, PURPLE, [rect_x1, rect_y1, 50, 50])
rect_x1 += rect_x_change1
rect_y1 += rect_y_change1
if rect_y1 > 450 or rect_y1 < 0:
rect_y_change1 = rect_y_change1 * -1
if rect_x1 > 650 or rect_x1 < 0:
rect_x_change1 = rect_x_change1 * -1
for i in range(50):
x = random.randrange(0, 800)
y = random.randrange(0, 800)
pygame.draw.circle(screen,WHITE,[x,y],3)
pygame.display.flip()
clock.tick(20)
def about() :
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
print('User pressed mouse.')
elif event.type == pygame.KEYDOWN:
print("User pressed a key")
pygame.draw.line(screen, BLUE, [0,0], [100,100], 5)
if event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
pygame.draw.rect(screen,BLACK,[200,20,350,100],0)
font = pygame.font.SysFont('Calibri', 45, True, False)
text = font.render("About", True, LGREEN)
screen.blit(text, [255, 75])
pygame.draw.rect(screen,GREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Back", True, YELLOW)
screen.blit(text, [355, 320])
mouse = pygame.mouse.get_pos()
if 280 + 200 > mouse [0] > 280 and 300+50 > mouse[1] > 300:
pygame.draw.rect(screen,LGREEN,[280,300,200,50],0)
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Back", True, RED)
screen.blit(text, [355, 320])
if event.type == pygame.MOUSEBUTTONDOWN:
title = True
about = False
pygame.display.flip()
clock.tick(20)
def done():
while not done:
print()
while done:
pygame.quit()
#Game Loop#
while vari == 0:
done()
while title:
title()
while about:
about()
#Closing Sequence#
print('Test fail :(')
pygame.quit()
它在你編寫它的某個階段工作,還是你沒有在測試過程中編寫所有代碼? – 2014-12-06 16:35:13