显示Python游戏的移动平铺
我试图用python创建一个简单的战术RPG,类似于Fire徽章或Advanced Wars,但使用ASCII。我一直在学习使用libtcod模块的教程,并为自己的目的修改教程代码 现在,我正在尝试为每个角色显示移动平铺。基本上,我希望游戏显示每个角色可以移动到的瓷砖,只要用户在该角色上单击鼠标。我创建了一个函数(显示Python游戏的移动平铺,python,Python,我试图用python创建一个简单的战术RPG,类似于Fire徽章或Advanced Wars,但使用ASCII。我一直在学习使用libtcod模块的教程,并为自己的目的修改教程代码 现在,我正在尝试为每个角色显示移动平铺。基本上,我希望游戏显示每个角色可以移动到的瓷砖,只要用户在该角色上单击鼠标。我创建了一个函数(get_total_squares),该函数根据角色的坐标和移动范围(目前硬编码为5)生成要高亮显示的平铺的坐标,并创建了一个函数(show_move_tiles)来高亮显示这些平铺。
get_total_squares
),该函数根据角色的坐标和移动范围(目前硬编码为5)生成要高亮显示的平铺的坐标,并创建了一个函数(show_move_tiles
)来高亮显示这些平铺。要记录单击坐标,我有另一个函数target\u tile
,还有一个函数可以查看坐标是否与字符的(target\u character
)匹配
程序运行时不会出错,但在角色上单击鼠标不会执行任何操作(不会显示平铺)
代码如下:
import libtcodpy as libtcod
#actual size of the window
SCREEN_WIDTH = 80
SCREEN_HEIGHT = 50
#size of the map
MAP_WIDTH = 80
MAP_HEIGHT = 43
LIMIT_FPS = 20 #20 frames-per-second maximum
color_dark_wall = libtcod.Color(0, 0, 50)
color_dark_ground = libtcod.Color(5, 50, 0)
color_move_tile = libtcod.Color (100, 0, 0)
class Tile:
#a tile of the map and its properties
def __init__(self, blocked, block_sight = None):
self.blocked = blocked
#by default, if a tile is blocked, it also blocks sight
if block_sight is None: block_sight = blocked
self.block_sight = block_sight
def target_tile():
#return the position of a tile left-clicked, or (None,None) if right-clicked.
global key, mouse
while True:
libtcod.console_flush()
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE, key, mouse)
render_all()
(x, y) = (mouse.cx, mouse.cy)
if mouse.rbutton_pressed or key.vk == libtcod.KEY_ESCAPE:
return (None, None) #cancel if the player right-clicked or pressed Escape
if mouse.lbutton_pressed:
return (x, y)
print "works!"
def target_character():
#Shows movement tiles if character is on click-coordinate
(x, y) = target_tile()
#return the first clicked character
for obj in objects:
if obj.x == x and obj.y == y:
obj.show_move_tiles()
def make_map():
global map
#fill map with "unblocked" tiles
map = [[ Tile(False)
for y in range(MAP_HEIGHT) ]
for x in range(MAP_WIDTH) ]
map[30][22].blocked = True
map[30][22].block_sight = True
map[50][22].blocked = True
map[50][22].block_sight = True
class Object:
#this is a generic object: the player, a monster, an item, the stairs...
#it's always represented by a character on screen.
def __init__(self, x, y, char, color):
self.x = x
self.y = y
self.char = char
self.color = color
def move(self, dx, dy):
#move by the given amount
if not map[self.x + dx][self.y + dy].blocked:
self.x += dx
self.y += dy
def draw(self):
#set the color and then draw the character that represents this object at its position
libtcod.console_set_default_foreground(con, self.color)
libtcod.console_put_char(con, self.x, self.y, self.char, libtcod.BKGND_NONE)
def show_move_tiles(self):
global mouse
global color_move_tile
(x,y) = (mouse.cx, mouse.cy)
coord_in_range = get_total_squares(5, self.x, self.y)
for [x,y] in coord_in_range:
if [x,y] is not [self.x, self.y]:
libtcod.console_set_char_background(con, x, y, color_move_tile, libtcod.BKGND_SET)
def clear(self):
#erase the character that represents this object
libtcod.console_put_char_ex(con, self.x, self.y, '.', libtcod.white, libtcod.black)
def handle_keys():
#key = libtcod.console_check_for_keypress() #real-time
global key #turn-based
if key.vk == libtcod.KEY_ENTER and key.lalt:
#Alt+Enter: toggle fullscreen
libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen())
elif key.vk == libtcod.KEY_ESCAPE:
return True #exit game
def get_total_squares(dist, playerx, playery):
coord_in_range = []
for x in range(-dist,dist+1):
for y in range(-dist, dist+1):
if abs(x)+abs(y) <= dist:
coord_in_range.append([playerx+x, playery+y])
return coord_in_range
def render_all():
global color_dark_wall
global color_dark_ground
#go through all tiles, and set their background color
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
wall = map[x][y].block_sight
if wall:
libtcod.console_put_char_ex(con, x, y, '#', libtcod.white, libtcod.black)
else:
libtcod.console_put_char_ex(con, x, y, '.', libtcod.white, libtcod.black)
#draw all objects in the list and show movement if mouse hovers over player
for item in objects:
item.draw()
if mouse.cx == item.x and mouse.cy == item.y:
item.show_move_tiles()
#blit the contents of "con" to the root console
libtcod.console_blit(con, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0, 0)
#############################################
# Initialization & Main Loop
#############################################
libtcod.console_set_custom_font('arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(SCREEN_WIDTH, SCREEN_HEIGHT, 'python/libtcod tutorial', False)
libtcod.sys_set_fps(LIMIT_FPS)
con = libtcod.console_new(SCREEN_WIDTH, SCREEN_HEIGHT)
#create object representing the player
player = Object(30, 15, '@', libtcod.white)
#create an NPC
npc = Object(SCREEN_WIDTH/2 - 5, SCREEN_HEIGHT/2, '@', libtcod.yellow)
#the list of objects with those two
objects = [npc, player]
make_map()
mouse = libtcod.Mouse()
key = libtcod.Key()
while not libtcod.console_is_window_closed():
libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS|libtcod.EVENT_MOUSE,key,mouse)
#render the screen
render_all()
target_tile()
libtcod.console_flush()
#erase all objects at their old locations, before they move
for object in objects:
object.clear()
#handle keys and exit game if needed
exit = handle_keys()
if exit:
break
将libtcodpy作为libtcod导入
#窗口的实际大小
屏幕宽度=80
屏幕高度=50
#地图的大小
地图宽度=80
地图高度=43
极限每秒帧数=20#最大每秒20帧
color\u dark\u wall=libtcod.color(0,0,50)
颜色\u深\u地面=libtcod.color(5,50,0)
color\u move\u tile=libtcod.color(100,0,0)
类别磁贴:
#地图的平铺及其属性
def u u初始(自身、阻塞、阻塞视距=无):
self.blocked=blocked
#默认情况下,如果瓷砖被阻挡,它也会阻挡视线
如果阻塞视距为无:阻塞视距=阻塞
self.block\u sight=block\u sight
def target_tile():
#返回左键单击的平铺的位置,如果右键单击,则返回(无,无)。
全局键,鼠标
尽管如此:
libtcod.console_flush()
libtcod.sys_check_for_event(libtcod.event_KEY_PRESS|libtcod.event_鼠标,键,鼠标)
全部呈现
(x,y)=(mouse.cx,mouse.cy)
如果按下mouse.rbutton\或key.vk==libtcod.key\退出:
return(None,None)#如果玩家右键单击或按Escape键,则取消
如果按下mouse.lbutton_:
返回(x,y)
打印“作品!”
def target_character():
#如果角色处于单击坐标位置,则显示移动平铺
(x,y)=目标片()
#返回第一个单击的字符
对于对象中的obj:
如果对象x==x且对象y==y:
对象显示\移动\瓷砖()
def make_map():
全球地图
#用“未阻塞”分幅填充地图
map=[[Tile(False)
对于范围内的y(贴图高度)]
对于范围内的x(贴图宽度)]
map[30][22]。blocked=True
地图[30][22]。街区视野=真
map[50][22]。blocked=True
地图[50][22]。街区视野=真
类对象:
#这是一个通用对象:玩家、怪物、物品、楼梯。。。
#它总是由屏幕上的一个字符来表示。
定义初始化(self,x,y,char,color):
self.x=x
self.y=y
self.char=char
self.color=颜色
def移动(自身、dx、dy):
#按给定数量移动
如果不映射[self.x+dx][self.y+dy]。已阻止:
self.x+=dx
self.y+=dy
def牵引(自):
#设置颜色,然后在其位置绘制表示此对象的角色
libtcod.console\u set\u default\u前台(con,self.color)
libtcod.console\u put\u char(con、self.x、self.y、self.char、libtcod.BKGND\u NONE)
def显示\移动\分幅(自):
全局鼠标
全局颜色\u移动\u平铺
(x,y)=(mouse.cx,mouse.cy)
coord_in_range=获取总平方(5,self.x,self.y)
对于坐标范围内的[x,y]:
如果[x,y]不是[self.x,self.y]:
libtcod.console\u set\u char\u background(con、x、y、color\u move\u tile、libtcod.BKGND\u set)
def清除(自):
#删除表示此对象的字符
libtcod.console\u put\u char\u ex(con,self.x,self.y,“.”,libtcod.white,libtcod.black)
def handle_keys():
#key=libtcod.console_check_for_keypress()#实时
全局密钥#基于回合
如果key.vk==libtcod.key\u输入key.lalt:
#Alt+Enter:全屏切换
libtcod.console\u set\u全屏(不是libtcod.console\u是\u全屏())
elif key.vk==libtcod.key\u转义:
返回真值#退出游戏
def get_total_方格(距离、玩家X、玩家Y):
协调范围内的协调=[]
对于范围内的x(-dist,dist+1):
对于范围内的y(-dist,dist+1):
如果abs(x)+abs(y)