qq堂
人物行走图
介绍
人物行走图是把人物分为上下左右 四个方向的动画图片,有时候甚至有8方向图。一般情况 上下左右把空间分为4个区域,每个区域90度。特殊情况下,有45度视角的游戏系统会把 复合方向作为 四个主方向而不是 一般意义的 上北下南左西右东。在qq堂里是上北下南左西右东,标准的2d地图。
processing载入背景图片
def setup():
global bg_img
bg_img =loadImage( 'grass.png')
def draw():
global bg_img
#background(255)
image(bg_img,0,0)
图片轮换
四个方向至少4张图片,算上动画就更多了。这次我用的素材是每个方向4张的
因此取名为 方向+ 序号
载入图片的方法一样
def setup():
global player
player = {'left':[],'right':[],'up':[],'down':[]}
for direction in player.keys():
for i in range(4):
img = loadImage('{}_{}.png'.format(direction,i))
player[direction].append(img)
变量设置
player = {'left':[],'right':[],'up':[],'down':[]}
主逻辑
def draw():
global bg_img
#background(255)
image(player['bg'] ,0,0)
global player
global player_index
global player_position
global current_direction
img = player[current_direction][player_index[current_direction]%4]
image(img, player_position.x, player_position.y)
轮换
图片序号
player_index[current_direction]%4
图片方向
current_direction
img = player[current_direction][player_index[current_direction]%4]
player_index 的控制在下面
方向控制
按adws 的时候 , 人物方向会变,同时坐标要变
def keyPressed():
global player_position
global current_direction
global player_index
if key == 'a':
player_position.x -= 10#同时坐标要变
player_index['left']+= 1 # 图片轮换
current_direction = 'left'#人物方向会变
if key == 'd':
player_position.x += 10 #同时坐标要变
player_index['right']+= 1
current_direction = 'right' #人物方向会变
if key == 'w':
player_position.y -= 10#同时坐标要变
player_index['up']+= 1
current_direction = 'up' #人物方向会变
if key == 's':
player_position.y += 10 #同时坐标要变
player_index['down']+= 1
current_direction = 'down' #人物方向会变
全部代码
#qq tang
w,h = 600,520
def setup():
size(w, h)
global player_index
global player_position
global current_direction
global bg_img
current_direction = 'down'
player_position= PVector(100, 200)
player_index = {'left':0,'right':0,'up':0,'down':0
}
global player
player = {'left':[],'right':[],'up':[],'down':[]}
for direction in player.keys():
for i in range(4):
img = loadImage('{}_{}.png'.format(direction,i))
player[direction].append(img)
bg_img =loadImage( 'grass.png')
player['bg'] = bg_img
def draw():
global bg_img
#background(255)
image(player['bg'] ,0,0)
global player
global player_index
global player_position
global current_direction
img = player[current_direction][player_index[current_direction]%4]
image(img, player_position.x, player_position.y)
def keyPressed():
global player_position
global current_direction
global player_index
if key == 'a':
player_position.x -= 10
player_index['left']+= 1
current_direction = 'left'
if key == 'd':
player_position.x += 10
player_index['right']+= 1
current_direction = 'right'
if key == 'w':
player_position.y -= 10
player_index['up']+= 1
current_direction = 'up'
if key == 's':
player_position.y += 10
player_index['down']+= 1
current_direction = 'down'