我写过一个简单的RPG战斗系统。我希望打印的内容出现在pygame窗口的黑色文本框中。我已经完成了这个并编写了一个名为TEXT(X)的函数来显示3个不同的行上的文本,但是在每次打印命令之后我都会调用它,它只运行一次而只显示第一条消息。 如果您运行该程序并将其与命令提示符进行比较,您将知道我的意思。
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)
答案 0 :(得分:1)
一个小小的改变使它对我有用:
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 ==
运算符中会比较地址,当有两个“相等”的字符串时可能不一样。
编辑:但即使这是真的,第二次调用“TEXT”需要获得相同的参数值才能使用你的代码......你正在将ONE与X进行比较。
TEXT("A");
TEXT("B");
那时候不会工作。
我再次测试(你的代码):
TEXT("A");
TEXT("A");
TEXT("C");
它有效。