qq堂放炮
这个游戏的玩法是用泡泡的爆炸产生的余波 去炸死敌人,游戏开局的时候,每个人只能放1-3个泡,所以需要先进行发育, 通过炸砖块来吃掉产生的道具,泡泡的威力的个数会增加。人物的移动速度也会增加。最后用适当的泡泡 进行 爆炸范围的叠加和包围,从而把敌人炸死
加入敌人(机器人)
在这个版本中, 我们暂时加入机器人作为敌人,不考虑联机or本地多人玩法。
bots = []
i = 0
n_bot = 5
def new_bot():
global i
p = PVector(300, 200)
d = {'id':i,'position':p,'state':'alive'}
i+=1
print('cur'+str(i))
return d
首先定义 bot的个数 , 用 n bot 来设置,然后实现新增 bot的效果。
由于所有的bot都需要和泡泡进行 攻击判定,所以我们把bot放在一个列表中, 并且每次给一个 id。初始状态bot是活着的alive, 炸死后变成dead
加入网格化
我们发现泡泡永远是放置在格子里,而且砖块和道具也是一样。也就是所谓的网格。整个地图可以被分成n个网格,网格涉及到泡泡的攻击范围, 一般来说,一个泡泡的攻击范围是一个十字。
我们要实现2个功能, 一个是通过 网格编号找 坐标, 另一个是通过坐标找网格。
网格编号 从0开始,例如0 , 0就是第一排,第一列。
范围是 0-grid w ,0-gird_h
grid_w = 40# 网格宽度
grid_h = 40# 网格高度
def get_grid_centre(row,col):
return row*grid_h+ 0.5*grid_h, col * grid_w + 0.5 * grid_w
def get_grid_topleft(row,col):
return row*grid_h , col * grid_w
def get_grid_idx(x,y):
return int(x/grid_w),int(y/grid_h)
加入爆炸攻击检测
对于每一个泡泡, 当它爆炸时候我们要 检测他的攻击范围,十字网格。检测方向是从中心向外检测,如果有砖块就会被挡住,如果没有检测更远的网格。
如果一个网格上有 机器人, 机器人为被炸死。
def try_boom_bots(boom_grid_pos):
for bot in bots:
pp = bot['position']
gridx,gridy = get_grid_idx(pp.x,pp.y)
if ( gridx,gridy) ==boom_grid_pos:
bot['state'] = 'Dead!'
全部代码
#qq tang
add_library('sound')
w,h = 600,520
bubble_log = {}
brick_log = {}
bubble_time = {}
exptime = 3000
endtime = 4500
grid_w = 40
grid_h = 40
game_state = {'update':0}
bots = []
i = 0
n_bot = 5
def new_bot():
global i
p = PVector(300, 200)
d = {'id':i,'position':p,'state':'alive'}
i+=1
print('cur'+str(i))
return d
def boom_hit_player(boom,p_bot):
x,y = boom
if abs(x-p_bot.x) <20 and abs(y-p_bot.y) <20:
return True
return False
def bots_bubble(boom):
for bot in bots:
p_bot = bot['position']
if boom_hit_player(boom,p_bot):
bot['state'] = 'Dead!'
def bots_move():
v = 10
for bot in bots:
r = random(0,4)
ds = [(0,-v),(0,v),(-v,0),(v,0)]
dx,dy = ds[int(r)]
bot['position'].x+=dx
bot['position'].y+=dy
bot['position'].x,bot['position'].y = limit_player(bot['position'].x,bot['position'].y )
def setup():
size(w, h)
global player_index
global player_position
global current_direction
global bg_img
global player
global bubbles
global boom
bgm = SoundFile(this,'bgm.mp3')
bgm.loop()
bubbles = []
player = {'left':[],'right':[],'up':[],'down':[]}
current_direction = 'down'
player_position= PVector(100, 200)
player_index = {'left':0,'right':0,'up':0,'down':0
}
for i in range(n_bot):
bot = new_bot()
bots.append(bot)
for direction in player.keys():
for i in range(4):
img = loadImage('{}_{}.png'.format(direction,i))
#print(img.width,img.height)#44, 59
player[direction].append(img)
bg_img =loadImage( 'grass.png')
global brick_img
brick_img= loadImage('brick_y.png')
global p2
p2= loadImage('role1_4_1.png')
global bubble_img
bubble_img = loadImage('bomb3.png')
boom = loadImage('mid_boom.png')
player['bg'] = bg_img
player['bubbles'] = []
player['bricks'] = []
def limit_player(x,y):
return max( min(x,w-40), 0) , max( min(y,h-60), 0)
def draw():
global bg_img
global bubble_img
#background(255)
image(player['bg'] ,0,0)
global player
global player_index
global player_position
global current_direction
global boom
global brick_img
img = player[current_direction][player_index[current_direction]%4]
if game_state['update']%8 == 0:
bots_move()
game_state['update']+=1
m = {'a':'left','d':'right','w':'up','s':'down'
}
# press once and go forever
if keyPressed:
if key in ['a','d','s','w']:
dir = m[key]
player_index[dir]+= 1
current_direction = dir
if key == 'a':
player_position.x -= 10
if key == 'd':
player_position.x += 10
if key == 'w':
player_position.y -= 10
if key == 's':
player_position.y += 10
player_position.x ,player_position.y = limit_player(player_position.x ,player_position.y )
player['bricks'].sort()
player['bubbles'].sort()
for x,y in player['bricks']:
image(brick_img, x,y)
for x,y in player['bubbles']:
t = bubble_time[(x,y)]
if t+exptime <millis()< t+endtime:
image(boom, x,y)
for i in range(1,10):
x1 = x+ i * 40
p = (x1,y)
if p in player['bricks']:
player['bricks'].remove(p)
print('boom')
break
bots_bubble(p)
image(boom, x1,y)
for i in range(1,10):
x2 = x- i *40
p = (x2,y)
if p in player['bricks']:
player['bricks'].remove(p)
print('boom')
break
bots_bubble(p)
image(boom, x2,y)
for i in range(1,10):
y1 = y+ i * 40
p = (x,y1)
if p in player['bricks']:
player['bricks'].remove(p)
print('boom')
break
bots_bubble(p)
image(boom, x,y1)
for i in range(1,10):
y1 = y- i * 40
p = (x,y1)
if p in player['bricks']:
player['bricks'].remove(p)
print('boom')
break
bots_bubble(p)
image(boom, x,y1)
del bubble_time[(x,y)]
player['bubbles'].remove((x,y))
else:
image(bubble_img, x,y)
image(img, player_position.x, player_position.y)
fill(255)
text('Player1',player_position.x, player_position.y-20)
global p2
for bot in bots:
bot_p = bot['position']
if bot['state'] == 'Dead!':continue
image(p2, bot_p.x, bot_p.y)
fill(0)
text('bot_p'+ bot['state'],bot_p.x, bot_p.y-20)
def img_centre(x,y,img):
return x+0.5*img.width , y+0.5 * img.height
def grid_map(x,y):
x_r = x% 40
x = int(x/40) * 40
if x_r > 0.5:
x+=1
y_r = y% 40
y = int(y/40) * 40
if y_r > 0.5:
y+=1
return x, y
def get_bubble_pos(x,y):
x,y = grid_map(x,y)
if not bubble_log.get((x,y),False):
return x,y
return None,None
def get_brick_pos(x,y):
x,y = grid_map(x,y)
if not brick_log.get((x,y),False):
return x,y
return None,None
def mousePressed():
global player_position
px,py =player_position.x,player_position.y
global player
# dx = mouseX - player_position.x
# dy = mouseY - player_position.y
# if dx > dy:
# if dx > 0:
# current_direction = 'right'
# else:
# current_direction = 'left'
# else:
# if dy > 0:
# current_direction = 'down'
# else:
# current_direction = 'up'
# dx = max(min(dx,10),-10)
# dy = max(min(dy,10),-10)
# player_position.x+= dx
# player_position.y+= dy
# player_index[current_direction] +=1
if True:
px,py = img_centre(px,py,player['down'][0])
x,y = get_bubble_pos(px,py)
if x and y:
bubble_log[(x,y)] = True
player['bubbles'].append((x,y))
bubble_time[(x,y)] =millis()
def keyPressed():
global player_position
global current_direction
global player_index
m = {'a':'left','d':'right','w':'up','s':'down'
}
# if key in ['a','d','s','w']:
# dir = m[key]
# player_index[dir]+= 1
# current_direction = dir
# if key == 'a':
# player_position.x -= 10
# if key == 'd':
# player_position.x += 10
# if key == 'w':
# player_position.y -= 10
# if key == 's':
# player_position.y += 10
if key == 'b':
bot = new_bot()
bots.append(bot)
if key == 'q' or key == 'm':
dx = mouseX - player_position.x
dy = mouseY - player_position.y
if dx > dy:
if dx > 0:
current_direction = 'right'
else:
current_direction = 'left'
else:
if dy > 0:
current_direction = 'down'
else:
current_direction = 'up'
dx = max(min(dx,10),-10)
dy = max(min(dy,10),-10)
player_position.x+= dx
player_position.y+= dy
player_index[current_direction] +=1
if key == 'j':
px,py =player_position.x,player_position.y
global player
px,py = img_centre(px,py,player['down'][0])
x,y = get_bubble_pos(px,py)
if x and y:
bubble_log[(x,y)] = True
player['bubbles'].append((x,y))
bubble_time[(x,y)] =millis()
if key == 'y':
px,py =player_position.x,player_position.y
global player
px,py = img_centre(px,py,player['down'][0])
x,y = get_brick_pos(px,py)
if x and y:
#bubble_log[(x,y)] = True
player['bricks'].append((x,y))