-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

しかし、それは画面からそれを削除するだけであり、ドロップを追加したいので、Ai座標が変更されたときにドロップアイテム座標も変更されることを意味します。ドロップ座標をAI座標と等しくする方法しか知らないため、表示したい場合AIが死んだ場所で。

また、経験値と健康状態はどちらも問題なく低下しますが、モンスターのキルから経験値を追加しようとすると、無期限に約 10 ミリ秒増加し、作成しようとするとmaxhealth = health + endurance無期限に成長するという同じ問題が発生しました。

私は本当に助けが必要です。コードを複数の関数にソートしようとしましたが、スプライトをオブジェクトに変更するだけのオプションをほとんど終了するだけで、すべてが機能しなくなります...

4

1 に答える 1

1

あなたのコードは、敵が死ぬことはできないということを考慮していません。敵のリストを使用して、すべての敵に対してすべての操作を行うことをお勧めします。敵が死亡すると、経験値が 1 回だけ追加されます。

すべての描画、殺害などを行う 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 に答える