当尝试将文本消息blit到pygame上时,该函数仅在

2024-06-16 14:45:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个简单的RPG战斗系统。我希望打印的内容显示在pygame窗口的黑色文本框中。我已经这样做了,并编写了一个名为TEXT(X)的函数来显示3行不同的文本,但是dispate我在每个print命令后调用它,它只运行一次,只显示第一条消息。 如果你运行这个程序并将它与命令提示符进行比较,你就会明白我的意思

from pygame import *
    from userInterface import Title, Dead
WIN_WIDTH = 640
WIN_HEIGHT = 400
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0

init()
screen = display.set_mode(DISPLAY, FLAGS, DEPTH)
saveState = False

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (30, 30, 30)
FONT = font.SysFont("Courier New", 15)



heroHP = 1000

hero={'name' : 'Hero',
      'height':4,
      'lvl': 1,
      'xp' : 0,
      'reward' : 0,
      'lvlNext':25,
      'stats': {'str' : 12, # strength
                'dex' : 4, # dexterity
                'int' : 15, # intelligence
                'hp'  : heroHP, # health
                'atk' : [250,350]}} # range of attack values


boss1={'name' : 'Imp',
       'xp' : 0,
       'lvlNext':25,
       'reward' : 25,
       'stats': {'hp'  :400,
                'atk' : [300,350]}}


ONE = None
TWO = None
THREE = None

def TEXT(X):
    global ONE, TWO, THREE
    if ONE == None:
        ONE = X
    elif ONE == X and TWO == None:
        TWO = X
    elif ONE and TWO and THREE == None:
        THREE = X
    elif ONE and TWO and THREE:
        ONE = None
        TWO = None
        THREE = None

def level(char): # level up system
    #nStr, nDex, nInt=0,0,0
    while char['xp'] >= char['lvlNext']:
        char['lvl']+=1
        char['xp']=char['xp'] - char['lvlNext']
        char['lvlNext'] = round(char['lvlNext']*1.5)
        nStr=0.5*char['stats']['str']+1
        nDex=0.5*char['stats']['dex']+1
        nInt=0.5*char['stats']['int']+1
        print(f'{char["name"]} levelled up to level {char["lvl"]}!') # current level
        TEXT(f'{char["name"]} levelled up to level {char["lvl"]}!') # current level

        print(f'( INT {round((char["stats"]["int"] + nInt))} - STR {round(char["stats"]["str"] + nStr)} - DEX {round(char["stats"]["dex"] + nDex)} )') # print new stats
        TEXT(f'( INT {round((char["stats"]["int"] + nInt))} - STR {round(char["stats"]["str"] + nStr)} - DEX {round(char["stats"]["dex"] + nDex)} )') # print new statsm


        char['stats']['str'] += nStr
        char['stats']['dex'] += nDex
        char['stats']['int'] += nInt

from random import randint

def takeDmg(attacker, defender): # damage alorithm
    dmg = randint(attacker['stats']['atk'][0], attacker['stats']['atk'][1])
    defender['stats']['hp'] = defender['stats']['hp'] - dmg
    print(f'{defender["name"]} takes {dmg} damage!')
    TEXT(f'{defender["name"]} takes {dmg} damage!')

    if defender['stats']['hp'] <= 0:
            print(f'{defender["name"]} has been slain...')
            TEXT(f'{defender["name"]} has been slain...')

            attacker['xp'] += defender['reward']
            level(attacker)
            if defender==hero:
                #Dead()
                input("Press ENTER to exit")
            else:
                hero['stats']['hp']=heroHP
                #Title()
                input("Press ENTER to exit")


def Battle(player, enemy):
    global ONE, TWO, THREE
    mouse.set_visible(1)
    clock = time.Clock()
    YES = Rect(100, 100, 50, 50)
    NO = Rect(500, 100, 50, 50)
    Text = Rect(70, 300, 500, 60)

    #while ((enemy['stats']['hp']) > 0):
    while True:
        for e in event.get():
            if e.type == QUIT:
                exit("Quit") # if X is pressed, exit program
            elif e.type == KEYDOWN:
                if e.key == K_ESCAPE:
                    exit()
            elif e.type == MOUSEBUTTONDOWN:
                # 1 is the left mouse button, 2 is middle, 3 is right.
                if e.button == 1:
                    # `event.pos` is the mouse position.
                    if YES.collidepoint(e.pos):
                        takeDmg(player, enemy)
                        print(f'{enemy["name"]} takes the opportunity to attack!')
                        TEXT(f'{enemy["name"]} takes the opportunity to attack!')

                        takeDmg(enemy, player)
                    elif NO.collidepoint(e.pos):
                        print(f'{enemy["name"]} takes the opportunity to attack!')
                        TEXT(f'{enemy["name"]} takes the opportunity to attack!')

                        takeDmg(enemy, player)


        screen.fill(WHITE)
        draw.rect(screen, BLACK, YES)
        draw.rect(screen, BLACK, NO)
        draw.rect(screen, GRAY, Text)



        YES_surf = FONT.render(("YES"), True, WHITE)
        NO_surf = FONT.render(("NO"), True, WHITE)
        Text1_surf = FONT.render(ONE, True, WHITE)
        Text2_surf = FONT.render(TWO, True, WHITE)
        Text3_surf = FONT.render(THREE, True, WHITE)


        screen.blit(YES_surf, YES)
        screen.blit(NO_surf, NO)
        screen.blit(Text1_surf, (80, 305))
        screen.blit(Text2_surf, (80, 320))
        screen.blit(Text3_surf, (80, 335))




        display.update()

        clock.tick(60)

Battle(hero, boss1)

Tags: totextnamenoneifstatsscreenone
1条回答
网友
1楼 · 发布于 2024-06-16 14:45:35

一个小小的改变让它对我起了作用:

def TEXT(X):
    global ONE, TWO, THREE;
    if ONE == None:
        ONE = X
    elif ONE and TWO == None: # "ONE == X" changed to "ONE"
        TWO = X
    elif ONE and TWO and THREE == None:
        THREE = X
    elif ONE and TWO and THREE:
        ONE = None
        TWO = None
        THREE = None

我猜您不能将字符串与==进行比较。你可以将它们与“equals”之类的东西进行比较。我知道在Java ==中,操作符会比较地址,当有两个“相等”的字符串时,地址可能不一样

Edit:但即使这是真的,第二次调用“TEXT”也需要获得相同的参数值才能处理代码。。。你是在拿一比X

TEXT("A");

TEXT("B");

那就不行了

我再次测试了(您的代码):

TEXT("A");
TEXT("A");
TEXT("C");

而且很有效

相关问题 更多 >