Python 字符响应键盘上的键,并在地图和其他功能中移动?
嗨,我13岁了,我已经做了1年半的编程了(因为我真正的意思是12岁)。我需要一些关于如何让你的角色在没有任何软件包或pygame的情况下用键盘移动的建议。我需要角色在我用特殊的角色键和其他东西创建的地图中实际移动,我还需要帮助创建一个菜单系统,以便它实际启动游戏。这是我的密码Python 字符响应键盘上的键,并在地图和其他功能中移动?,python,python-3.x,Python,Python 3.x,嗨,我13岁了,我已经做了1年半的编程了(因为我真正的意思是12岁)。我需要一些关于如何让你的角色在没有任何软件包或pygame的情况下用键盘移动的建议。我需要角色在我用特殊的角色键和其他东西创建的地图中实际移动,我还需要帮助创建一个菜单系统,以便它实际启动游戏。这是我的密码 class Map(self, keyboard): """ View of characted being moved to new location. """ class Character():
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)
类映射(自身,键盘):
“”“将字符移动到新位置的视图。”“”
类字符():
定义字符(对象):
C=玩家(英雄)
hero={'name':'hero',
“lvl”:1,
“xp”:0,
“lvlNext”:25,
'stats':{'str':1,
“dex”:1,
“int”:1,
“hp”:30,
'atk':[5,12]}
def更新(自我、C、映射):
“”“根据按下的键移动播放机。”“”
如果按下键盘键(K、w、C、map):
赛尔夫
自映射
self.y-=1
如果按下键盘键(K、C、map):
赛尔夫
自映射
self.y+=1
如果按了键盘键(K、a、C、map):
赛尔夫
自映射
self.x-=1
如果存在键盘(K、d、C、map):
赛尔夫
自映射
self.x+=1
其他:
打印(“等待您的移动”)
类敌人():
敌方(目标):
f=敌人(飞行)
fly={'name':'fly',
“lvl”:1,
"奖励":25,,
“xp”:0,
“lvlNext”:25,
'stats':{'str':1,
“int”:1,
“hp”:30,
'atk':[5,12]}
def移动(自身、f、映射):
“移动”
类墙()
def墙(对象):
P=墙(实心)
solid=['name':'solid',
“功能”:
c=胸部
d=门
DD=门
map=”“”
pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
P |-------------------------------------------P
P | | | | | | | | | | | | | P
P | | | | | ppdpppppppppp | | | P
P | C | | P P | f | | P
P | | | | | | P f P | | | P
P | | | P P | d | P
P | | | | | | | P
P | | | | | | | P
中交
P | | | | | | | ppppppppppppp | | c | P
P | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | P
pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
"""
类键盘(自身、字符、地图):
“”“键盘控制。”“”
def更新(自我):
“”“根据按下的键移动英雄。”“”
如果按下键盘键(K、w、p、map):
self.y-=1
如果按下键盘键(K、s、p、map):
self.y+=1
如果按了键盘键(K a、p、map):
self.x-=1
如果存在键盘(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,
"奖励":25,,
“xp”:0,
“lvlNext”:25,
'stats':{'str':1,
“int”:1,
“hp”:30,
'atk':[5,12]}
def液位(字符):
nStr,nDex,nInt=0,0,0
而char['xp']>=char['lvlNext']:
字符['lvl']+=1
char['xp']=char['xp']-char['lvlNext']
char['lvlNext']=round(char['lvlNext']*1.5)
nStr+=1
nDex+=1
nInt+=1
打印('级别:',字符['lvl'])
打印('STR{}+{}DEX{}+{}INT{}+{}'。格式(char['stats']['STR'],nStr,
字符['stats']['dex'],nDex,
字符['stats']['int'],nInt))
字符['stats']['str']+=nStr
字符['stats']['dex']+=nDex
字符['stats']['int']+=nInt
从随机导入randint
def takeDmg(攻击者、防御者):
dmg=randint(攻击者['stats']['atk'][0],攻击者['stats']['atk'][1])
defender['stats']['hp']=defender['stats']['hp']-dmg
如果defender['stats']['hp']一款带有地图的基于文本的冒险游戏很难在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中,您可以轻松地从图像生成地图网格(即,这是门,这是玩家,这是苍蝇,这是空地板,这是墙)。由于您已经制作了地图,如果将其转换为数组,则可以轻松生成地图。您将拥有一个数组数组,然后可以使用
for y in final_rows:
for x in y:
if x == 'P':
render_P()
elif x == 'f':
render_fly()
#... etc.
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