0

My code is meant to be a simple, turn based game based on classic japanese role-playing games from the NES era. the display is mostly made up of static images and two text boxes, one displaying the players current hit points and magic points, and another describing the actions of each participant as they take their actions.

here is how I intend for it to work: the program begins, and the turn counter is set to "1". whenever the turn counter is "1" it is the players turn andd a numbered list of commands are displayed in the text box. the player selects which one they want by pressing the number key that corresponds to that action, the appropriate function is called, the turn counter is set to "2", the enemy takes their turn, and the turn counter is set back to "1". this continues until someones HP reaches 0 or lower, at which point a message declaring the winner is displayed.

the two issues i'm running into are as follows: the message text, declared early in the main with placeholder values, does not update to what it should when turn == 1. the second issue is that the code does not seem to be accepting any input, as I have set some text to display the current value of the "turn" variable, leading me to believe that there is either something preventing the inputs from being accepted, or something is preventing all text from being updated, not just the text box.

below is the main function of my code, and whole thing, along with the bmp and wav files used can be found here if that's more helpful: https://www.mediafire.com/file/nr151r4ysg4shmz/Code.zip/file

def main():
    pygame.init()

    size = [640, 480]
    window = pygame.display.set_mode(size)

    black = pygame.color.Color('#000000')
    white = pygame.color.Color('#FFFFFF')
    font = pygame.font.SysFont("Arial", 36)

    bg = pygame.image.load('Background.bmp')
    bg = pygame.transform.scale(bg, (640, 480))

    nme = pygame.image.load('GreenDragon.bmp')
    nme = pygame.transform.scale(nme, (200,150))

    sound = pygame.mixer.Sound('Battle_music.wav')
    sound.play()

    statusBoxX = 10
    statusBoxY = 10
    statusBoxWidth = 140
    statusBoxHeight = 140
    statusBorderX = 0
    ststusBorderY = 0
    statusBorderWidth = 160
    ststusBorderHeight = 160

    textBoxX = 10
    textBoxY = 330
    textBoxW = 620
    textBoxH = 140
    textBorderX = 0
    textBorderY = 320
    textBorderW = 640
    textBorderH = 160

    turn = 1
    message1 = "1"
    message2 = "2"    

    playerName = "Hero"
    playerMaxHP = 300
    playerCurHP = 300
    playerMaxMP = 120
    playerCurMP = 120
    playerBaseStr = 50
    playerCurStr = 50
    playerBaseDef = 45
    playerCurDef = 45
    playerMagPwr = 35
    playerMagDef = 40
    playerSpeed = 40
    playerDamage = 0
        
    enemyName = "Green Dragon"
    enemyMaxHP = 300
    enemyCurHP = 300
    enemyMaxMP = 120
    enemyCurMP = 120
    enemyBaseStr = 50
    enemyCurStr = 50
    enemyBaseDef = 45
    enemyCurDef = 45
    enemyMagPwr = 35
    enemyMagDef = 40
    enemySpeed = 40
    enemyDamage = 0
    
    keys = [False, False, False, False, False, False]  
    
    while True:

        window.fill((255, 255, 255))
        window.blit(bg, (0,0))
        window.blit(nme, (230, 160))

        pygame.draw.rect(window, white, [textBorderX, textBorderY, textBorderW, textBorderH], border_radius=10)
        pygame.draw.rect(window, black, [textBoxX, textBoxY, textBoxW, textBoxH], border_radius=10)

        messageText1 = font.render(message1, False, white)
        messageText2 = font.render(message2, False, white)

        window.blit(messageText1, (20, 345))
        window.blit(messageText2, (20, 385))

        pygame.draw.rect(window, white, [statusBorderX, ststusBorderY, statusBorderWidth, ststusBorderHeight], 
                         border_radius=10)
        pygame.draw.rect(window, black, [statusBoxX, statusBoxY, statusBoxWidth, statusBoxHeight], border_radius=10)

        name = font.render(playerName, False, white)
        playerHealth = font.render("HP: " + str(playerCurHP), False, white)
        playerMagic = font.render("MP: " + str(playerCurMP), False, white)

        window.blit(name, (45,20))
        window.blit(playerHealth, (25,60))
        window.blit(playerMagic, (25,100))
        
        debug = font.render(str(turn), False, white)
        window.blit(debug, (200, 20))

        pygame.display.flip()

        while playerCurHP > 0 and enemyCurHP > 0:
            if turn == 1:
                loopx = False
                while loopx == False:
                    playerCurDef = playerBaseDef
                    pygame.display.update()
                    message1 = "1. Attack 2. Heal 3. Fireball"
                    message2 = "4. Buff 5. Item 6. Defend"
                    
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            pygame.quit()
                            sys.exit(0)
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_1:
                                keys[0] = True
                            elif event.key == pygame.K_2:
                                keys[1] = True
                            elif event.key == pygame.K_3:
                                keys[2] = True
                            elif event.key == pygame.K_4:
                                keys[3] = True
                            elif event.key == pygame.K_5:
                                keys[4] = True
                            elif event.key == pygame.K_6:
                                keys[5] = True

                        if event.type == pygame.KEYUP:
                            if event.key == pygame.K_1:
                                keys[0] = False
                            elif event.key == pygame.K_2:
                                keys[1] = False
                            elif event.key == pygame.K_3:
                                keys[2] = False
                            elif event.key == pygame.K_4:
                                keys[3] = False
                            elif event.key == pygame.K_5:
                                keys[4] = False
                            elif event.key == pygame.K_6:
                                keys[5] = False

                if keys[0] == True:
                    playerAttack(playerName, playerCurStr, enemyName, enemyCurHP, enemyCurDef, playerDamage)
                    loopx = True
                elif keys[1] == True:
                    playerHeal(playerName, playerCurHP, playerMaxHP, playerCurMP)
                    loopx = True
                elif keys[2] == True:
                    playerFireball(playerName, playerMagPwr, enemyName, enemyMagDef, playerDamage)
                    loopx = True
                elif keys[3] == True:
                    playerBuff(playerName, playerCurMP, playerCurStr)
                    loopx = True
                elif keys[4] == True:
                    item(playerName, playerCurHP, playerMaxHP, playerCurMP, playerMaxMP)
                    loopx = True
                elif keys[5] == True:
                    defend(enemyAttack, enemyDamage)
                    loopx = True
                turn = 2                        

            if turn == 2:
                action = random.randint(1,100)
                if action < 41:
                    enemyAttack(enemyName, enemyCurStr, playerName, playerCurHP, playerCurDef, enemyDamage)
                elif action < 81:
                    magic = random.randint(1,100)
                    if magic < 26:
                        enemyHeal(enemyName, enemyCurHP, enemyMaxHP, enemyCurMP, enemyMagPwr)
                    elif magic < 66:
                        enemyFirebreath(enemyName, enemyCurMP, enemyMagPwr, playerName, playerCurHP, playerCurDef, enemyDamage)
                    else:
                        enemyDeMagic(enemyName, enemyCurMP, playerName, playerBaseStr, playerCurStr)
                else:
                    enemyDefend(enemyCurHP, playerDamage)

                     
                turn = 1
                continue

        if playerCurHP < 1:
            message1 = "Game Over!"
            message2 = " "
            break
        elif enemyCurHP < 1:
            message1 = enemyName + " was defeated!"
            message2 = " "
            break
  • 1
    I think the issue is you're going into a while which prevents the outer while loop from drawing anything to the screen. I think you'll need to find a way to restructure this code so that you can still draw every frame. Consider having a `redraw` function for drawing everything to the screen, or having classes which have `redraw` methods etc. A good youtuber to check out with guides on pygame is [tech with tim](https://www.youtube.com/channel/UC4JX40jDee_tINbkjycV4Sg) or if you need an example already built I have a simple game [here](https://github.com/Rolv-Apneseth/spaceship-shooters) – Rolv Apneseth Apr 09 '21 at 12:33
  • Hopefully that helps, I also had a problem with stuff not responding and it turned out to be a while loop with no drawing occurring – Rolv Apneseth Apr 09 '21 at 12:35

0 Answers0