-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()

这是我当前的代码,我已经研究了一段时间。

显然这些图像不适用于任何测试它的人,但我目前的问题是,当敌人hp低于 0 时,我不知道如何让敌人完全被移除。我试着做一个

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

但这只会将它从屏幕上删除,并且由于我还想添加drop,这意味着当Ai坐标发生变化时,drop item坐标也会发生变化,因为我只知道如何使drop坐标等于AI坐标,如果我希望它出现在人工智能死亡的地方。

经验值和生命值都下降得很好,但是当我尝试从怪物杀死中添加经验值时,它会以每毫秒 10 毫秒的速度无限增长,而当我试图让它maxhealth = health + endurance无限增长时,它也是同样的问题。

我真的需要帮助,我尝试将我的代码排序为多个函数,但它只会让整个事情停止工作,这几乎退出了将精灵更改为对象的选项......

4

1 回答 1

1

您的代码没有考虑到敌人不能死。我建议使用敌人列表,然后对所有敌人执行所有操作。在敌人死亡时,您将只添加一次 xp。

最好做一个 Enemy 类,它会完成所有的绘图、杀戮等,所以你不需要在你的主代码中担心它。以下是示例调用:

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

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

    enemy.move()
    enemy.draw(screen)

一个球员也是如此。如果你把它分开,你将更容易添加新功能。整个玩家运动可以是 Player 类中的一个函数,因为它不与其他任何东西交互。

不要使用 eval,玩家将能够执行任何代码。您想将 str 转换为 int。像这样:

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

如果更改代码太困难,您可以随时重新开始。您已经编写了代码,因此您将知道什么在哪里。

于 2014-06-08T08:38:58.680 回答