嗨,我13岁,我现在已经编程了1 1/2年(因为我真正的意思是12岁).我需要一些关于如何使您的角色在不使用任何程序包或pygame的情况下通过键盘移动的建议.我需要角色在使用特殊角色键和材质创建的地图中实际移动,我还需要创建菜单系统的帮助,以便它可以真正启动游戏.这是我的代码.
class Map(self, keyboard):
""" View of characted being moved to new location. """
class Character():
def Character(object):
C = player(hero)
hero = {'name' : 'Hero',
'lvl' : 1,
'xp' : 0,
'lvlNext' : 25,
'stats' : {'str' : 1,
'dex' : 1,
'int' : 1,
'hp' : 30,
'atk' : [5, 12]}}
def update(self, C, map):
""" Move player based on keys pressed. """
if keyboard.is_pressed(K_w, C, map):
self.C
self.map
self.y -= 1
if keyboard.is_pressed(K_s, C, map):
self.C
self.map
self.y += 1
if keyboard.is_pressed(K_a, C, map):
self.C
self.map
self.x -= 1
if keyboard.is_preseed(K_d, C, map):
self.C
self.map
self.x += 1
else:
print(" Waiting for your move. ")
class Enemy():
def Enemy(object):
f = enemy(fly)
fly = {'name' : 'Fly',
'lvl' : 1,
'reward' : 25,
'xp' : 0,
'lvlNext' : 25,
'stats' : {'str': 1,
'int' : 1,
'hp' : 30,
'atk' : [5, 12]}}
def move(self, f, map):
""" Move """
class Wall()
def Wall(object):
P = wall(solid)
solid = ['name' : 'Solid',
'Function' :
c = chest
d = door
DD = door
map = """
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
P|-----------------------------------------------------|P
P| |--------| |-------|------------| |---------------|P
P| | |PPDPPPPPPPP| | | |P
P| C| |P P| f | | |P
P| | |P f P| | | |P
P| | |P P| d d |P
P| | |P P| | | |P
P| | |P P| | | |P
P| | |P P| | | cccc |P
P| | |PPPPPPPPPPP| | | c c |P
P|__d_______|P|_________d___________| |____cccc_______|P
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP
"""
class Keyboard(self, character, map):
""" Key board controls. """
def update(self):
""" Move hero based on keys pressed. """
if keyboard.is_pressed(K_w, p, map):
self.y -= 1
if keyboard.is_pressed(K_s, p, map):
self.y += 1
if keyboard.is_pressed(K_a, p, map):
self.x -= 1
if keyboard.is_preseed(K_d, p, map):
self.x += 1
hero = {'name' : 'Hero',
'lvl' : 1,
'xp' : 0,
'lvlNext' : 25,
'stats' : {'str' : 1,
'dex' : 1,
'int' : 1,
'hp' : 30,
'atk' : [5, 12]}}
fly = {'name' : 'Fly',
'lvl' : 1,
'reward' : 25,
'xp' : 0,
'lvlNext' : 25,
'stats' : {'str': 1,
'int' : 1,
'hp' : 30,
'atk' : [5, 12]}}
def level(char):
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 += 1
nDex += 1
nInt += 1
print('level:', char['lvl'])
print('STR {} +{} DEX {} +{} INT {} +{}'.format(char['stats']['str'], nStr,
char['stats']['dex'], nDex,
char['stats']['int'], nInt))
char['stats']['str'] += nStr
char['stats']['dex'] += nDex
char['stats']['int'] += nInt
from random import randint
def takeDmg(attacker, defender):
dmg = randint(attacker['stats']['atk'][0], attacker['stats']['atk'][1])
defender['stats']['hp'] = defender['stats']['hp'] - dmg
if defender['stats']['hp'] <= 0:
print('{} has been slain'.format(defender['name']))
hero['xp'] += fly['reward']
level(hero)
input('Press any key to quite.')
exit(0)
因此,您至少可以看到我正在尝试做的事情.我需要地图,以及等于菜单的“ Game_Sys”,并且我需要菜单来响应我拥有的任何关键功能.现在,我构建了它实际起作用的关卡系统.我建立了具有奖励系统的敌人,一旦杀死敌人,我的等级便会增加到我的等级,如果我的xp超过当前等级的最大xp,我会升级.但是,现在我想在没有PYGAME且没有包装的情况下在地图上移动“ p = player blablabla”.它基于文本,因此希望我能以某种方式运行该程序.无论如何,如果有任何错误,请给我一些有关此游戏的帮助和建议.我的目标是让“ C”在我创建的地图上移动,如果我接近f,我会受到攻击,或者我开始战斗,然后我就从那开始.
我可以使用Livewires打开游戏窗口,还是需要pygame才能打开游戏窗口?我可以找一个伙伴帮忙吗?我需要一个人.
解决方法:
带有地图的基于文本的冒险游戏很难(最好)使用Python来实现. Livewires本身不是GUI显示器,但是课程中使用的许多示例都需要pygame.我实际上不知道它仍然可以运行/维护;这是一个相当古老的工具.
因此,您的困难在于地图显示.大多数Python解释器的问题在于,要在地图上显示移动,您必须为每一次移动逐字地打印地图.
my_map = """
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
P|-----------------------------------------------------|P
P| |--------| |-------|------------| |---------------|P
P| | |PPDPPPPPPPP| | | |P
P| C| |P P| f | | |P
P| | |P f P| | | |P
P| | |P P| d d |P
P| | |P P| | | |P
P| | |P P| | | |P
P| | |P P| | | cccc |P
P| | |PPPPPPPPPPP| | | c c |P
P|__d_______|P|_________d___________| |____cccc_______|P
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP
"""
那将是很多次,这将是巨大的内存损失.但是,使用此地图和Pygame,您可以生成一个使用大多数(如果不是全部)代码的游戏.
您必须要做的事情:
>将地图转换成数组.在pygame中,您可以轻松地从图像生成地图网格(即,这是门,这是玩家,这是飞车,这是空地板,这是墙).由于已经制作了地图,因此如果将其转换为数组,则可以轻松生成地图.您将拥有一个数组数组,然后可以将其作为(x,y)网格进行访问.
伪代码示例:
for y in final_rows:
for x in y:
if x == 'P':
render_P()
elif x == 'f':
render_fly()
#... etc.
>获取用户输入.现在,您没有用户输入. Pygame使操作变得容易,请查看documentation和this answer了解更多信息.
>创建菜单以及制作游戏的所有其他内容.您已经知道您需要执行此操作,为了完整起见,此处仅将其包括在内.
如果您无法确定,我强烈建议您将pygame用于所有GUI,但是如果发现它不适合您,请查看this page,以获取Python GUI和其他游戏性的详尽列表.
祝好运!
编辑:这就是我说要为您的地图制作数组/列表时的意思:
my_map = """
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
P|-----------------------------------------------------|P
P| |--------| |-------|------------| |---------------|P
P| | |PPDPPPPPPPP| | | |P
P| C| |P P| f | | |P
P| | |P f P| | | |P
P| | |P P| d d |P
P| | |P P| | | |P
P| | |P P| | | |P
P| | |P P| | | cccc |P
P| | |PPPPPPPPPPP| | | c c |P
P|__d_______|P|_________d___________| |____cccc_______|P
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP
"""
#split map into rows (y-values)
unprocessed_rows = my_map.split('\n')
#initialize list
final_rows = []
#process rows
for row in unprocessed_rows:
#remove spaces on either side
row = row.strip()
#only add the row if it is not empty
#(the first and last are in the variable my_map)
if len(row)>0:
final_rows.append(row)
#see what this created:
print final_rows
['PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP', 'P|-----------------------------------------------------|P', 'P| |--------| |-------|------------| |---------------|P', 'P| | |PPDPPPPPPPP| | | |P', 'P| C| |P P| f | | |P', 'P| | |P f P| | | |P', 'P| | |P P| d d |P', 'P| | |P P| | | |P', 'P| | |P P| | | |P', 'P| | |P P| | | cccc |P', 'P| | |PPPPPPPPPPP| | | c c |P', 'P|__d_______|P|_________d___________| |____cccc_______|P', 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP']
#it's obviously a list of strings, but it's hard to read.
#Let's print it row by row to make sure everything turned out okay.
for r in final_rows:
print r
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP
P|-----------------------------------------------------|P
P| |--------| |-------|------------| |---------------|P
P| | |PPDPPPPPPPP| | | |P
P| C| |P P| f | | |P
P| | |P f P| | | |P
P| | |P P| d d |P
P| | |P P| | | |P
P| | |P P| | | |P
P| | |P P| | | cccc |P
P| | |PPPPPPPPPPP| | | c c |P
P|__d_______|P|_________d___________| |____cccc_______|P
PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPDDPPPPPPPPPPPPPPPPPP
#good, no extra lines or random spaces.
#we can now access specific map coordinates like this:
#my_map[y][x] (starting at 0)
print final_rows[0][0]
#prints P
print final_rows[4][3]
#prints C
将地图转换为列表后,即可使用我在上面给出的伪代码来访问它.
标签:python-3-x,python