-4
import random
from random import *
import math
from math import *
from pygame import *
import pygame, sys
from pygame.locals import *
import pygame.font
from pygame.font import *


bif=  "grass.png"
mif= "character front.png"
mifB=  "character back.png"
mifL= "character left.png"
mifR= "character right.png"
mifRS=  "character right still.png"
mifLS=  "character left still.png"
skel=  "skeleton front.png"
skelB=  "skeleton back.png"
skelL=  "skeleton left.png"
skelR= "skeleton right.png"
swordsky="sword_sky.png"
sworddown="sword_down.png"
swordleft="sword_left.png"
swordright="sword_right.png"
swordblank="sword_blank.png"
healthpot="healthpot.png"
levelup = 1
pygame.init()
screen=pygame.display.set_mode((700,600),0,32)
#background create
r = 0
healthpotion=pygame.image.load(healthpot).convert_alpha()
sword_blank=pygame.image.load(swordblank).convert_alpha()
sword=pygame.image.load(sworddown).convert_alpha()
sword_down=pygame.image.load(sworddown).convert_alpha()
sword_sky=pygame.image.load(swordsky).convert_alpha()
sword_right=pygame.image.load(swordright).convert_alpha()
sword_left=pygame.image.load(swordleft).convert_alpha()
background=pygame.image.load(bif).convert()
character=pygame.image.load(mif).convert_alpha()
character_back=pygame.image.load(mifB).convert_alpha()
character_left=pygame.image.load(mifL).convert_alpha()
character_right=pygame.image.load(mifR).convert_alpha()
character_front=pygame.image.load(mif).convert_alpha()
character_right_still=pygame.image.load(mifRS).convert_alpha()
character_left_still=pygame.image.load(mifLS).convert_alpha()
skeleton=pygame.image.load(skel).convert_alpha()
skeleton_back=pygame.image.load(skelB).convert_alpha()
skeleton_left=pygame.image.load(skelL).convert_alpha()
skeleton_right=pygame.image.load(skelR).convert_alpha()
skeleton_front=pygame.image.load(skel).convert_alpha()
#convert image files to python useable files
x,y = 300, 250
movex,movey = 0,0
Ai_x, Ai_y = 0, 500
moveAi_x, moveAi_y=0,0
movesx, movesy=0,0
ix, iy = -500, -500
experience = 0
aihp = 15
health = 100
sx,sy = x+10, y+20
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
     #player movement       
        if event.type==KEYDOWN:
            if event.key==K_a:
                if character==character_right:
                    sx,sy= x+25, y+15
                    sword=sword_right
                elif character==character_right_still:
                    sx,sy= x+25, y+15
                    sword=sword_right
                elif character==character_left_still:
                    sx,sy= x-25, y+15
                    sword=sword_left
               elif character==character_left:
                    sx,sy= x-25, y+15
                    sword=sword_left
                elif character==character_front:
                    sx,sy= x, y+30
                    sword=sword_down
                elif character==character_back:
                    sx,sy= x, y-20
                    sword=sword_sky
            if event.key==K_RIGHT:
                movex += .3
                character=character_right
            elif event.key==K_LEFT:
                movex -= .3
                character=character_left
            elif event.key==K_UP:
                movey -= .3
                character=character_back
            elif event.key==K_DOWN:
                movey += .3
                character=character_front
        if event.type==KEYUP:
            if event.key==K_RIGHT:
                    movex = 0
                    character=character_right_still
            elif event.key==K_LEFT:
                    movex = 0
                    character=character_left_still
            elif event.key==K_UP:
                    movey = 0
                    character=character_back
            elif event.key==K_DOWN:
                    movey = 0
                    character=character_front
            if event.key==K_a:
                if character==character_right:
                    sx,sy= x+25, y+15
                    sword=sword_blank
                elif character==character_right_still:
                    sx,sy= x+25, y+15
                    sword=sword_blank
                elif character==character_left_still:
                    sx,sy= x-25, y+15
                    sword=sword_blank
                elif character==character_left:
                    sx,sy= x-25, y+15
                    sword=sword_blank
                elif character==character_front:
                    sx,sy= x, y+30
                    sword=sword_blank
                elif character==character_back:
                    sx,sy= x, y-20
                    sword=sword_blank
    x+=movex
    y+=movey
    #Creep movement
    sdist = sqrt((sx - Ai_x)**2 + (sy - Ai_y)**2)
    #damage
    y2 = y - 20
    if (sx, sy) != (x+40,y2):
        if x > sx:
            movesx = +.3
        if x < sx:
             movesx = -.3
        if y > sy:
            movesy = +.3
        if y < sy:
            movesy = -.3
    sx+=movesx
    sy+=movesy
    #sword movement
    if (Ai_x, Ai_y) != (x,y):
        if x > Ai_x:
            moveAi_x = .2

        if x < Ai_x:
             moveAi_x = -.2

        if y > Ai_y:
            moveAi_y = +.2

        if y < Ai_y:
            moveAi_y = -.2
    Ai_x+=moveAi_x
    Ai_y+=moveAi_y
    #creep movement  
    #Controls for character
    newallocatedstr=0
    newallocatedend=0
    newallocatedagi=0
    newallocatedchr=0
    newallocatedwis=0
    #stats
    if levelup == 2:
        newallocatedstr = eval(input("strength? (0-5):" ))
        newallocatedend = eval(input("endurance? (0-5):" ))
        newallocatedagi= eval(input(" agility? (0-5):" ))
        newallocatedchr = eval(input(" charisma? (0-5):" ))
        newallocatedwis = eval(input("wisdom? (0-5):" ))
        if ((newallocatedstr + newallocatedend)
        + (newallocatedagi + newallocatedchr) + newallocatedwis) > 5:
            print("You filthy cheater.")
            pygame.quit()
            sys.exit()
            levelup = 1
    #levelup
    strn = newallocatedstr
    end = newallocatedend
    agi = newallocatedagi
    chra = newallocatedchr
    wis = newallocatedwis

    endurance= 5 + end
    strength=5 + strn
    wisdom=5 + wis
    charisma=5 + chra
    agility=5 + agi
    #stats

    # render health text
    maxhealth = health 
    health = maxhealth

    if sdist<12:
        aihp = aihp - 1

    dist = sqrt((Ai_x - x)**2 + (Ai_y - y)**2)
    #damage    
    if dist<6:
        health = health - 1

    font = pygame.font.Font(None, 25)
    mytext = font.render("Health:{0}".format(health), 1, (255,255,255))
    exp = font.render("Experience:{0}".format(experience), 1, (255,255,255))
    mytext = mytext.convert_alpha()
    if health == 0:
        print("Game over")
        pygame.quit()
        sys.exit()


    mana = 50 + wisdom
    font = pygame.font.Font(None, 25)
    mana = font.render("Mana:"+str(mana), 1, (255,255,255))
    font = pygame.font.Font(None, 20)
    aihealth = font.render("Ai Health:{0}".format(aihp), 1, (255,255,255))

    #create background


    screen.blit(background, (0,0))
    if aihp >= 0:
        screen.blit(skeleton, (Ai_x, Ai_y))
        screen.blit(aihealth, (Ai_x-25, Ai_y+40))
    else:
        experience = experience + 10
    screen.blit(exp, (15, 5))    
    screen.blit(mytext, (15, 25))
    screen.blit(mana, (15, 50))
    screen.blit(aihealth, (Ai_x-25, Ai_y+40))
    screen.blit(sword, (sx, sy))
    screen.blit(healthpotion, (ix, iy))
    screen.blit(character, (x,y))
    pygame.display.flip()
    pygame.display.update()

This is my current code, ive been working on it for a while.

Obviously the images won't work for anyone testing it, but my current problem is, when an enemy's hp goes below ``0, I don't know how to make the enemy completely get removed. I tried to do an

if AIhp <= 0: 
    Ai_x, Ai_y = -50, -50

But that only removes it from the screen, and since I also want to add drops it means when the Ai coordinates change so does the drop item coordinates since I only know how to make the drop coordinates equal to AI coordinates if I want it to appear in the place the AI died.

Also the exp, and health both go down fine, but when I try to add exp from monster kill it goes up by about 10 a millisecond indefinitely, and when I tried to make the maxhealth = health + endurance it was same issue with growing indefinitely.

I really need help, I have tried sorting my code into multiple functions but it only makes the entire thing stop working which pretty much exits out the option of just changing the sprites to objects...

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    Please **stop** using wildcard imports! Using them turns your code into a HUGE mess. – ThiefMaster Jun 08 '14 at 00:58
  • @ThiefMaster I think that's the text layout and lack of logical structure, but I agree that the OP should avoid `from x import *` (particularly where they, inexplicably, also `import x`). – jonrsharpe Jun 08 '14 at 07:06
  • http://www.python.org/dev/peps/pep-0008/ - that link is really worth reading @OP – ThiefMaster Jun 08 '14 at 08:53

1 Answers1

1

Your code does not take into account, that an enemy cannot be dead. I recommend to use a list of enemies, and then do all the operations for all the enemies. On enemy death, you will add the xp only once.

It's better do make an Enemy class, that will do all the drawing, killing, etc, so you will not need to worry about it in your main code. Here are example calls:

for enemy in enemies:
    if sdist<12:
       enemy.hit()

    if not enemy.isAlive():
        enemies.remove(enemy)
        #add Drops

    enemy.move()
    enemy.draw(screen)

Same goes for a player. If you divide this up, you will have a much easier time adding new functionality. The whole player movement could be a function in the class Player, since it does not interact with anything else.

Do not use eval, a player would be able to execute any code. You want to cast the str to int. Like this:

newallocatedwis = int(input("wisdom? (0-5):" ))

If changing the code is too difficult, you can always start over. You already have the code written, so you will know what goes where.

Bartlomiej Lewandowski
  • 10,771
  • 14
  • 44
  • 75
  • Tried to divide it up, entire thing is broken now. Would appreciate if you could check it out and tell me what I did wrong? http://stackoverflow.com/questions/24112816/code-not-working-it-doesnt-show-any-errors-but-it-doesnt-seem-to-do-anything?noredirect=1#comment37199091_24112816 – Kelith White Jun 09 '14 at 03:56