import pygame
import time
import random
pygame.init()
display_height = 800#display width and height
display_width = 1000
black = (0,0,0)
white = (255,255,255)#defining colours
red = (255,0,0)
blue = (127, 179, 213)
peru = (139,119,101)
beige = (250,250,225)
lightb = (245,222,179)
bg = (255,248,220)
grey_slate = (50,50,50)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('The Algebra Game')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',50)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def message_display2(text):
largeText = pygame.font.Font('freesansbold.ttf',30)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/3))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def Try_Again1(msg,x,y,w,h,inac,act,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, act,(x,y,w,h))
if click[0] == 1 and action != None:
if action == "Try Again":
Addition_loop1()
else:
pygame.draw.rect(gameDisplay, inac,(x,y,w,h))
def home_button(msg,x,y,w,h,inac,act,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.ellipse(gameDisplay, act,(x,y,w,h))
if click[0] == 1 and action != None:
if action == "Home":
game_intro()
else:
pygame.draw.ellipse(gameDisplay, inac,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def button(msg,x,y,w,h,inac,act,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, act,(x,y,w,h))
if click[0] == 1 and action != None:
if action == "Addition":
Addition_loop1()
elif action == "Subtraction":
Subtraction_loop1()
elif action == "Multiplication":
Multiplication_loop1()
elif action == "Division":
Division_loop1()
elif action == "Option 1":
incorrect_loop1()
else:
pygame.draw.rect(gameDisplay, inac,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def incorrect_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
message_display2("You are Incorrect")
Try_Again1("Try Again",25,25,150,50,peru,grey_slate,"Try Again")
pygame.display.update()
def Addition_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
addition = pygame.image.load("addition.jpg")
gameDisplay.blit(addition,(330,50))
button("Option 1",50,500,200,50,peru,black,"Option 1")
pygame.display.update()
clock.tick(60)
def Subtraction_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
pygame.display.update()
clock.tick(60)
def Multiplication_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
pygame.display.update()
clock.tick(60)
def Division_loop1():
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(black)
home_button("Home",25,25,150,50,peru,grey_slate,"Home")
pygame.display.update()
clock.tick(60)
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(grey_slate)
largeText = pygame.font.Font('freesansbold.ttf',50)
TextSurf, TextRect = text_objects("The Algebra Game", largeText)
TextRect.center = ((display_width/2),(display_height/14))
gameDisplay.blit(TextSurf, TextRect)
button("Addition",250,125,500,85,peru,black,"Addition")
button("Subtraction",250,250,500,85,peru,black,"Subtraction")
button("Multiplication",250,375,500,85,peru,black,"Multiplication")
button("Division",250,500,500,85,peru,black,"Division")
mouse = pygame.mouse.get_pos()
pygame.display.update()
clock.tick(15)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
clock.tick(60)
game_intro()
pygame.quit()
quit
嗨,我遇到了一些难以获得" Try_Again1"按钮功能工作,它当前没有显示文本或带我到所需的页面(Addition_loop1)一旦点击,该按钮也在运行时从屏幕上闪烁。我使用的是Python 3.5(32位)。任何有关我的问题的帮助将不胜感激。谢谢你