1

I'm very new to pygame and programming in general. I'm currently working on a project in pygame using Python 3.4. It is EXTREMELY BASIC, but I am having some issues. So the program opens into the intro screen and the buttons work fine here. When you click play it goes into the battle screen, which, again, is very basic, but on this screen is where I run into my issue. I can't seem to make the attack button work. I know that I also need code for the actual sequence of events, but I would think that I need the button to work before doing so. Anyway, here is the code. It's VERY messy, sorry. Like I wrote before, I'm new to this. Any and all help if greatly appreciated. Thank you SO MUCH!

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()
RLeal90
  • 11
  • 3
  • could you please show exactly where in the code do you think the problem is? – pb772 Dec 02 '18 at 01:11
  • I think it may be in the definition for the attack: def attack(): enemy_health player_attack = enemy_health - random.randint(15,35) return enemy_health As the button changes color as it should when hovered over, but when clicked nothing happens. – RLeal90 Dec 02 '18 at 01:15

0 Answers0