我一般来说对pygame和编程都是新手。我目前正在使用Python 3.4在pygame中进行项目。这是非常基础,但是我遇到了一些问题。因此,程序将打开到“简介”屏幕,并且此处的按钮可以正常工作。当您单击播放时,它会进入战斗屏幕,这再次是非常基本的,但是在该屏幕上,我遇到了问题。我似乎无法使“攻击”按钮起作用。我知道我也需要用于实际事件序列的代码,但是我认为我需要按钮才能起作用。无论如何,这是代码。非常混乱,对不起。就像我之前写的一样,我是新手。任何和所有帮助,如果不胜感激。非常感谢!
import pygame
import time
import random
pygame.init()
display_width = 900
display_height = 700
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
SILVER = (192, 192, 192)
MAROON = (110, 0, 0)
GOLD = (120, 120, 0)
DARKGREEN = (10,62,2)
BRIGHT_GREEN = (0,255,0)
BRIGHT_YELLOW = (255,255,0)
BRIGHT_RED = (255,0,0)
fireImg = pygame.image.load('Fire_Fury.png')
windImg = pygame.image.load('Wind_Fury.png')
waterImg = pygame.image.load('Water_Fury.png')
woodImg = pygame.image.load('Wood_Fury.png')
earthImg = pygame.image.load('Earth_Fury.png')
RillImg = pygame.image.load('Rill.png')
BrutusImg = pygame.image.load('Brutus.png')
CirrusImg = pygame.image.load('Cirrus.png')
CyprusImg = pygame.image.load('Cyprus.png')
PhyllisImg = pygame.image.load('Phyllis.png')
fireImg = pygame.transform.scale(fireImg,(200,320))
waterImg = pygame.transform.scale(waterImg,(200,320))
woodImg = pygame.transform.scale(woodImg,(200,320))
windImg = pygame.transform.scale(windImg,(200,320))
earthImg = pygame.transform.scale(earthImg,(200,320))
RillImg = pygame.transform.scale(RillImg,(200,320))
BrutusImg = pygame.transform.scale(BrutusImg,(200,320))
CirrusImg = pygame.transform.scale(CirrusImg,(200,320))
CyprusImg = pygame.transform.scale(CyprusImg,(200,320))
PhyllisImg = pygame.transform.scale(PhyllisImg,(200,320))
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Furies')
clock = pygame.time.Clock()
def text_objects(text, font):
textSurface = font.render(text, True, SILVER)
return textSurface, textSurface.get_rect()
def lose_text(text, font):
textSurface = font.render(text, True, WHITE)
return textSurface, textSurface.get_rect()
def small_text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 48)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
def woodfury(x,y):
gameDisplay.blit(woodImg,(x,y))
def firefury(x,y):
gameDisplay.blit(fireImg,(x,y))
def waterfury(x,y):
gameDisplay.blit(waterImg,(x,y))
def windfury(x,y):
gameDisplay.blit(windImg,(x,y))
def earthfury(x,y):
gameDisplay.blit(earthImg,(x,y))
def Rill(bx,by):
gameDisplay.blit(RillImg, (bx,by))
def Brutus(bx,by):
gameDisplay.blit(BrutusImg, (bx,by))
def Cirrus(bx,by):
gameDisplay.blit(CirrusImg, (bx,by))
def Phyllis(bx,by):
gameDisplay.blit(PhyllisImg, (bx,by))
def Cyprus(bx,by):
gameDisplay.blit(CyprusImg, (bx,by))
def button(msg,x,y,w,h,ic,ac,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, ac, (x, y, w, h))
if click[0] == 1 and action!= None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x, y, w, h))
smallText = pygame.font.Font('freesansbold.ttf', 20)
textSurf, textRect = small_text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gameDisplay.fill(BLACK)
largeText = pygame.font.Font('freesansbold.ttf', 48)
TextSurf, TextRect = text_objects("Welcome to Alera...", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
button("Click here to play.",250, 375, 300, 25,SILVER,GOLD,game_loop)
button("Click here to exit.",275, 415, 300, 25,SILVER,MAROON,quitgame)
pygame.display.update()
clock.tick(15)
def health_bars(player_health, enemy_health):
if player_health > 75:
player_health_color = BRIGHT_GREEN
elif player_health > 50:
player_health_color = BRIGHT_YELLOW
else:
player_health_color = BRIGHT_RED
if enemy_health > 75:
enemy_health_color = BRIGHT_GREEN
elif enemy_health > 50:
enemy_health_color = BRIGHT_YELLOW
else:
enemy_health_color = BRIGHT_RED
pygame.draw.rect(gameDisplay, player_health_color, (0, 500, player_health, 25))
pygame.draw.rect(gameDisplay, enemy_health_color, (800, 50, enemy_health, 25))
def question_box():
smallText = pygame.font.Font('freesansbold.ttf', 20)
textSurf, textRect = small_text_objects("What would you like to do?", smallText)
textRect.center = (600,575)
gameDisplay.blit(textSurf, textRect)
player_health = 100
enemy_health = 100
def attack():
enemy_health
player_attack = enemy_health - random.randint(15,35)
return enemy_health
def enemy_attack():
player_health
opponent_attack = player_health - random.randint(15, 35)
return player_health
def game_loop():
defeated = False
wildfury = [firefury, waterfury, earthfury, windfury, woodfury]
opponent = random.choice(wildfury)
randomfury = [Cyprus, Phyllis, Brutus, Rill, Cirrus]
self = random.choice(randomfury)
while not defeated:
for event in pygame.event.get():
if event.type == pygame.QUIT:
defeated = True
pygame.display.update()
gameDisplay.fill(DARKGREEN)
health_bars(player_health,enemy_health)
button("Attack",475,600,100,50,SILVER,MAROON,attack)
question_box()
x = (display_width * 0.75)
y = (display_height * 0.1)
opponent(x,y)
bx = (display_width * 0.1)
by = (display_height * 0.55)
self(bx,by)
if enemy_health <= 0:
largeText = pygame.font.Font('freesansbold.ttf', 48)
TextSurf, TextRect = text_objects("You have won the battle!", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
if player_health <= 0:
largeText = pygame.font.Font('freesansbold.ttf', 48)
TextSurf, TextRect = lose_text("You lost this battle.", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
clock.tick(30)
game_intro()
game_loop()
pygame.quit()
quit()