def check_events(screen, view_manager, x, y, press, p1): ''' 响应按键和鼠标事件 ''' for event in pygame.event.get(): # 处理游戏退出 if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEMOTION: if press[0]: print('hit') if 120 <= x < 120 + view_manager.cards.get_rect( ).width and 15 <= y <= 15 + view_manager.cards.get_rect( ).height: p = plant.plant() p1.append(p)
columns_move = 1 if 0 <= j < games.map_columns and 0 <= i < games.map_rows: obj.grid(row=i, column=j) games.after( obj.bullet_speed, lambda: moving(games, obj, target, columns_move, rows_move)) else: obj.destroy() return else: games.moving_bullets.append(obj) return 香蒲 = plant(name='香蒲', img='香蒲.png', price=225, hp=5, cooling_time=30, attack_interval=1.4, bullet_img='thron.png', bullet_speed=200, bullet_attack=1, bullet_sound=('sounds/throw.ogg', ), func=cat_check, bullet_func=moving, plant_range=[pool_check], plant_func=cat_plant, other_img=[[f'thron{k}.png', 3, True] for k in [4, 2, 6, 1, 3, 7, 5]])
each.button.grid_forget() each.rows = trans[0] each.columns = trans[1] each.button.grid(row=each.rows, column=each.columns) each.stop = False def transport_bullet(self, obj): if len(self.places) == 2: trans = self.places[1] obj.rows = trans[0] obj.columns = trans[1] def portal_away(self, games): if len(self.places) == 2: trans = self.places[1] games.blocks[trans[0]][trans[1]].configure(image=games.lawn_photo) 传送门 = plant(name='传送门', img='传送门.png', price=200, hp=5, cooling_time=7.5, func=transport_set, away_func=portal_away, information=info, other_img=[['Portal2.jpg', 1]], effects={'bullet': transport_bullet})
random.choice(self.bullet_sound).play() goto_another_row(self, obj, games) def goto_another_row(self, obj, games): if obj.hp <= 0: return if games.current_time - self.start_time >= 1.5: available_rows = [obj.rows - 1, obj.rows + 1] available_rows = [x for x in available_rows if 0 <= x < games.map_rows] obj.stop = False obj.rows = random.choice(available_rows) obj.button.grid(row=obj.rows, column=obj.columns) obj.start_func(obj, games) return else: games.after(10, lambda: goto_another_row(self, obj, games)) return 大蒜 = plant(name='大蒜', img='大蒜.png', price=50, hp=21, cooling_time=7.5, attack_interval=1.5, bullet_attack=0, bullet_sound=('sounds/yuck.ogg', 'sounds/yuck2.ogg'), sound_volume=(0.5, 0.5), effects={'zombies': garlic_check}) 大蒜.used = 0
if 0 <= j < games.map_columns and 0 <= i < games.map_rows: obj.grid(row=i, column=j) games.after(obj.bullet_speed, lambda: moving_cat(games, obj, target, columns_move, rows_move)) else: obj.destroy() return else: games.moving_bullets.append(obj) return 豌豆射手_枪林弹雨 = plant(name='豌豆射手', img='豌豆射手.png', price=100, hp=5, cooling_time=7.5, attack_interval=1.4, bullet_img='pea.png', bullet_speed=200, bullet_attack=1, bullet_sound=('sounds/throw.ogg', ), func=cat_check, bullet_func=moving, other_img=[['thron.png', 3, True]] + [[f'thron{k}.png', 3, True] for k in [4, 2, 6, 1, 3, 7, 5]])
mean_traj_line.set_data(mean_trajectory[0, :], mean_trajectory[1, :]) mean_velocity = np.diff(mean_trajectory)/dt mean_velocity = np.hstack([mean_velocity, np.array([[0], [0]])]) if i < n_t_steps: # Determine current goal and plot it current_goal = np.array([mean_trajectory[0, i], mean_trajectory[1, i], mean_velocity[0, i], mean_velocity[1, i]]) if i == 0: current_goal_line, = plt.plot(current_goal[0], current_goal[1], 'k*', markersize=18, mew=2) else: current_goal_line.set_data(current_goal[0], current_goal[1]) # Move the point particle and plot it acceleration = pd.PD_controller(point, current_goal) point = plant.plant(acceleration, point, dt) if i == 0: point_line, = plt.plot(point[0], point[1], 'mo', markersize=18, mew=2) else: point_line.set_data(point[0], point[1]) if i == 199: plt.title('Frame {}'.format(i), fontsize=18) plt.tight_layout() plt.savefig("Images/frame_{}.pdf".format(i)) plt.show() # plt.ion() # plt.show()
def run_game(): # 初始化游戏 pygame.init() # 创建ViewManager对象 screen = pygame.display.set_mode((1024, 768)) view_manager = ViewManager() block = pygame.time.Clock() # 设置显示屏幕,返回Surface对象 p1 = [] # 设置标题 pygame.display.set_caption('pvz') p2 = [] #是否进入点击状态 is_pick = False while 1: block.tick(30) screen.blit(view_manager.background, (0, 0)) screen.blit(view_manager.cards, (120, 15)) press = pygame.mouse.get_pressed() x, y = pygame.mouse.get_pos() # 处理游戏事件 for event in pygame.event.get(): # 处理游戏退出 if event.type == pygame.QUIT: sys.exit() if event.type == pygame.MOUSEMOTION: if not is_pick: if press[0]: if 120 <= x < 120 + view_manager.cards.get_rect( ).width and 15 <= y <= 15 + view_manager.cards.get_rect( ).height: p = plant(view_manager) p1.append(p) is_pick = True if is_pick: if not (120 <= x < 120 + view_manager.cards.get_rect().width and 15 <= y <= 15 + view_manager.cards.get_rect().height): if press[2]: p1.clear() is_pick = False if press[0]: p.x = x - 50 p.y = y - 80 p.motivated = True p2.append(p) p1.clear() is_pick = False for i in p1: screen.blit(i.images, (x - 50, y - 80)) for i in p2: if i.motivated and i.shoot_time <= 0: i.add_bullet(view_manager) i.draw_bullet(screen) screen.blit(i.images, (i.x, i.y)) # print((str(len(i.bullet_list))), str(i.shoot_time)) if press[1]: print(len(p2)) del_monster = [] for i in mm.monster_list: for j in p2: for k in j.bullet_list: if(i.is_hurt(k.x+50,k.y+26)): j.bullet_list.remove(k) i.life -= 1 if i.life <= 0: del_monster.append(i) mm.monster_list.remove(del_monster) del_monster.clear() update_screen(screen, view_manager, mm)
fire_ls_row[each.columns - 1 - each.adjust_col].destroy() games.after(3000, lambda t=each: t.button.destroy()) for each in attack_zombies_col: each.hp -= self.bullet_attack if each.hp <= 0: each.status = 0 games.killed_zombies += 1 games.current_killed_zombies += 1 games.killed_zombies_text.set(f'杀死僵尸数: {games.killed_zombies}') each.button.configure(image=games.zombie_explode_img) fire_ls_col[each.rows].destroy() games.after(3000, lambda t=each: t.button.destroy()) jalapeno_blocks = games.blocks[i][j] jalapeno_blocks.configure( image=games.map_img_dict[jalapeno_blocks.types]) jalapeno_blocks.plants = None 十字火爆辣椒 = plant(name='十字火爆辣椒', img='十字火爆辣椒.png', price=150, hp=5, cooling_time=50, attack_interval=2, bullet_img='Fire1_1.png', bullet_attack=90, bullet_sound=('sounds/jalapeno.ogg', ), is_bullet=False, func=jalapeno_check, information=text)
from plant import plant 坚果墙 = plant(name='坚果墙', img='坚果墙.png', price=50, hp=72, cooling_time=30, hp_img=((2 / 3, 'Wallnut_cracked1.png'), (1 / 3, 'Wallnut_cracked2.png')))
each.button.destroy() else: hit_zombies_left = [ x for x in hit_zombies if x.columns - j == -1 ] if len(hit_zombies_left) != 0: for each in hit_zombies_left: each.hp -= self.bullet_attack if each.hp <= 0: each.status = 0 games.killed_zombies += 1 games.current_killed_zombies += 1 games.killed_zombies_text.set( f'杀死僵尸数: {games.killed_zombies}') each.button.destroy() squash_block = games.blocks[i][j] squash_block.configure(image=games.map_img_dict[squash_block.types]) squash_block.plants = None 窝瓜 = plant(name='窝瓜', img='窝瓜.png', price=50, hp=5, cooling_time=30, attack_interval=1.5, bullet_attack=90, bullet_sound=(['sounds/squash_hmm.ogg', 'sounds/squash_hmm2.ogg'], 'sounds/gargantuar_thump.ogg'), func=squash_check)
from plant import plant def sunflower_check(self, games): i, j = self.rows, self.columns if games.current_time - self.time >= self.attack_interval: self.time = games.current_time flower_sunshine = games.make_button(games.maps, image=games.flower_sunshine_img) flower_sunshine.configure( command=lambda: games.flower_get_sunshine(flower_sunshine, self)) flower_sunshine.image = games.fall_sunshine_img flower_sunshine.grid(row=i, column=j) 向日葵 = plant(name='向日葵', img='向日葵.png', price=50, hp=5, cooling_time=7.5, attack_interval=10, bullet_img='sun.png', bullet_attack=25, no_cooling_start=True, func=sunflower_check, is_bullet=False)
obj.destroy() return else: games.after(obj.bullet_speed, lambda: moving(games, obj, 1)) else: obj.destroy() return else: games.moving_bullets.append(obj) return text = ''' 特性:当随机豌豆射手的右边任意一行出现僵尸的时候,从随机的一行发射一个攻击力随机的豌豆, 攻击力从1, 2, 5, 10, 50里面按照出现概率0.6, 0.25, 0.1, 0.04, 0.01里面随机选择。 ''' 随机豌豆射手 = plant(name='随机豌豆射手', img='随机豌豆射手.png', price=200, hp=5, cooling_time=7.5, attack_interval=1.4, bullet_img='pea.png', bullet_speed=200, bullet_attack=1, bullet_sound=('sounds/throw.ogg', ), func=peashooter_check, bullet_func=moving, information=text)
moving(games, obj) else: return else: return else: games.after(obj.bullet_speed, lambda: moving(games, obj, 1)) else: obj.destroy() return else: games.moving_bullets.append(obj) return 寒冰射手 = plant(name='寒冰射手', img='寒冰射手.png', price=175, hp=5, cooling_time=7.5, attack_interval=1.4, bullet_img='snow pea.png', bullet_speed=200, bullet_attack=1, bullet_sound=('sounds/throw.ogg', 'sounds/frozen.ogg'), func=snow_pea_check, bullet_func=moving, other_img=[['pea.png', 3]])
each.stop = False each.time = games.current_time each.runs(games) if each.button.winfo_exists(): each.button.grid(row=i, column=each.columns) def attract(self, games): i, j = self.rows, self.columns adjacent_rows = [i - 1, i + 1] adjacent_rows = [k for k in adjacent_rows if 0 <= k < games.map_rows] adjacent_zombies = [ x for x in games.whole_zombies if x.status == 1 and x.columns - 1 - x.adjust_col == j + 1 and x.rows in adjacent_rows ] for each in adjacent_zombies: if not (hasattr(each, 'stick_butter') and each.stick_butter > 0): each.stop = True each.rows = i games.after(2000, lambda each=each: move_here(each, i, games)) 番薯 = plant(name='番薯', img='番薯.png', price=150, hp=40, cooling_time=20, img_transparent=True, func=attract, information=text)
if self.contain_plants.away_func: self.contain_plants.away_func(self.contain_plants, games) if self.contain_plants.away_self_func: self.contain_plants.away_self_func(self.contain_plants, games, block) else: block.configure(image=self.img) games.unset_plants_sound.play() games.action_text.set( f'你铲除了第{j+1}行,第{k+1}列的植物{self.contain_plants.name}') self.contain_plants.status = 0 self.contain_plants = None else: block.configure(image=games.map_img_dict['pool']) games.unset_plants_sound.play() games.action_text.set(f'你铲除了第{j+1}行,第{k+1}列的植物{self.name}') block.plants.status = 0 block.plants = None 荷叶 = plant(name='荷叶', img='荷叶.png', price=25, hp=5, cooling_time=7.5, plant_range=[lily_check], func=lily_func, plant_func=None, away_self_func=lily_away) 荷叶.contain_plants = None
from plant import plant 火炬树桩 = plant(name='火炬树桩', img='火炬树桩.png', price=175, hp=5, cooling_time=7.5, bullet_img='FirePea.png') def fire_update(self, obj): if obj.attributes == 0: obj.attack *= 2 obj.configure(image=self.bullet_img) obj.attributes = 1 def normal_fire_update(self, obj): if hasattr(obj, 'name') and obj.name == 'snow pea': obj.configure(image=obj.change_img) obj.melt = 1 else: if obj.attributes == 0: obj.attack *= 2 obj.configure(image=self.bullet_img) obj.attributes = 1 火炬树桩.effects = {'bullet': fire_update}
diff = self.pre_hp - self.hp self.share_hp[0] -= diff self.pre_hp = deepcopy(self.hp) for each in self.whole_wallnuts: if each != self: each.hp -= diff each.pre_hp -= diff def away(self, games): i, j = self.rows, self.columns other_rows = list(range(games.map_rows)) other_rows.remove(i) for each in other_rows: current = games.blocks[each][j] current.plants = None current.configure(image=games.lawn_photo) 坚果小队 = plant(name='坚果小队', img='坚果小队.png', price=250, hp=400, cooling_time=30, func=start_plant_wallnut, away_func=away, dead_normal=False, information=info, hp_img=((2 / 3, 'long_wallnut_crack1.png'), (1 / 3, 'long_wallnut_crack2.png')))
if len(attack_zombies) != 0: self.bullet_sound[1].play() for each in attack_zombies: each.hp -= self.bullet_attack if each.hp <= 0: each.status = 0 games.killed_zombies += 1 games.current_killed_zombies += 1 games.killed_zombies_text.set(f'杀死僵尸数: {games.killed_zombies}') each.button.configure(image=games.zombie_explode_img) games.after(3000, lambda t=each: t.button.destroy()) potato_block = games.blocks[i][j] potato_block.configure(image=games.lawn_photo) potato_block.plants = None return games.after(50, lambda: potato_detect(self, games)) 土豆雷 = plant(name='土豆雷', img='土豆雷.png', price=25, hp=5, cooling_time=30, attack_interval=3, bullet_img='UnarmedPotatoMine.png', bullet_attack=90, bullet_sound=('sounds/dirt_rise.ogg', 'sounds/potato_mine.ogg'), func=potato_checking, is_bullet=False, use_bullet_img_first=True)
if 0 <= k[0] < games.map_rows and 0 <= k[1] < games.map_columns ] around_zombies = [ q for q in games.whole_zombies if q.status == 1 and [q.rows, q.columns] in around ] for each in around_zombies: each.hp -= self.bullet_attack if each.hp <= 0: each.status = 0 games.killed_zombies += 1 games.current_killed_zombies += 1 games.killed_zombies_text.set(f'杀死僵尸数: {games.killed_zombies}') each.button.configure(image=games.zombie_explode_img) games.after(3000, lambda t=each: t.button.destroy()) cherry_block = games.blocks[i][j] cherry_block.configure(image=games.lawn_photo) cherry_block.plants = None 樱桃炸弹 = plant(name='樱桃炸弹', img='樱桃炸弹.png', price=150, hp=5, cooling_time=30, attack_interval=2, bullet_attack=90, bullet_sound=('sounds/cherrybomb.ogg', ), sound_volume=(0.5, ), func=cherry_check)
around_zombies = [ q for q in games.whole_zombies if q.status == 1 and [q.rows, q.columns] in around ] for each in around_zombies: each.hp -= self.bullet_attack if each.hp <= 0: each.status = 0 games.killed_zombies += 1 games.current_killed_zombies += 1 games.killed_zombies_text.set(f'杀死僵尸数: {games.killed_zombies}') each.button.configure(image=games.zombie_explode_img) games.after(3000, lambda t=each: t.button.destroy()) wallnut_block = games.blocks[i][j] wallnut_block.configure(image=games.lawn_photo) wallnut_block.plants = None 爆炸坚果 = plant(name='爆炸坚果', img='爆炸坚果.png', price=100, hp=72, bullet_attack=90, cooling_time=30, bullet_sound=('sounds/cherrybomb.ogg', ), func=wallnut_explode, dead_normal=False, information=text, hp_img=((2 / 3, 'Explode-o-nut2.png'), (1 / 3, 'Explode-o-nut3.png')))
hitted_zombies.remain_time = games.current_time - hitted_zombies.time hitted_zombies.stick_butter += 1 hitted_zombies.butter_obj.append(obj) obj.hit_zombies = hitted_zombies obj.time = games.current_time games.after(10, lambda: moving(games, obj)) else: games.after(obj.bullet_speed, lambda: moving(games, obj, 1)) else: obj.destroy() return else: games.moving_bullets.append(obj) return 玉米投手 = plant(name='玉米投手', img='玉米投手.png', price=100, hp=5, cooling_time=7.5, attack_interval=3, bullet_img='Cornpult_kernal.png', bullet_speed=200, bullet_attack=1, bullet_sound=('sounds/throw.ogg', ), func=corn_check, other_img=[['Cornpult_butter.png', 3, True]], bullet_func=moving)
hitted_zombies.hit_sound.play() if obj.attributes == 1: sputter = obj.attack / len(affect_zombies) for sputter_zombies in affect_zombies[1:]: sputter_zombies.hp -= sputter obj.destroy() return else: games.after(obj.bullet_speed, lambda: moving(games, obj, 1)) else: obj.destroy() return else: games.moving_bullets.append(obj) return 豌豆射手 = plant(name='豌豆射手', img='豌豆射手.png', price=100, hp=5, cooling_time=7.5, attack_interval=1.4, bullet_img='pea.png', bullet_speed=200, bullet_attack=1, bullet_sound=('sounds/throw.ogg', ), func=peashooter_check, bullet_func=moving, img_transparent=True)
def run_game(): worldtime = t.timing(ticks=0, days=0, eons=0) population = 10 vegetation = 10 xlist = np.random.uniform(0, 1920, population) ylist = np.random.uniform(0, 1080, population) xplist = np.random.uniform(0, 1920, vegetation) yplist = np.random.uniform(0, 1080, vegetation) animals = [] plants = [] for i in range(population): animals.append(a.animal(int(xlist[i]), int(ylist[i]), worldtime)) for i in range(vegetation): plants.append(p.plant(int(xplist[i]), int(yplist[i]), 0.01)) clock = pg.time.Clock() pg.init() screen = pg.display.set_mode((1920, 1080)) #comment out for fast sim pg.display.set_caption("first screen") #comment out for fast sim while True: for event in pg.event.get(): if event.type == pg.QUIT: sys.exit() screen.fill((0, 0, 0)) #comment out for fast sim color = (255, 100, 0) for i in range(vegetation): plants[i].grow() for j in range(population): dist = np.sqrt((plants[i].x - animals[j].px)**2 + (plants[i].y - animals[j].py)**2) if dist <= (plants[i].size + animals[j].size): plants[i].eaten(animals[j].erate) animals[j].grow() pg.draw.circle(screen, (100, 255, 0), [int(round(plants[i].x)), int(round(plants[i].y))], int(plants[i].size), 0) #comment out for fast sim kill_plant_list = [] kill_animal_list = [] for i in range(vegetation): if (plants[i].size < 1): kill_plant_list.append(i) for plnt in kill_plant_list: plants.pop(plnt) newanimlist = [] for i in range(population): x = (animals[i].px) y = (animals[i].py) if (x > 1920 - animals[i].size or x < animals[i].size): animals[i].velx = -int(0.5 * animals[i].velx) if (x > 1920 - animals[i].size): animals[i].px = 1920 - animals[i].size else: animals[i].px = animals[i].size if (y > 1080 - animals[i].size or y < animals[i].size): animals[i].vely = -int(0.5 * animals[i].vely) if (y > 1080 - animals[i].size): animals[i].py = 1080 - animals[i].size else: animals[i].py = animals[i].size #animals do something here for now random motion newanim = animals[i].animal_action(worldtime) if newanim is not None: newanimlist.append(newanim) animals[i].impulse(np.random.normal(0.0, 0.1), np.random.normal(0.0, 0.1)) if (worldtime > animals[i].dtime): kill_animal_list.append(i) pg.draw.circle( screen, color, [int(round(animals[i].px)), int(round(animals[i].py))], int(animals[i].size), 0) #comment out for fast sim animals = animals + newanimlist for anml in kill_animal_list: animals.pop(anml) vegetation = len(plants) population = len(animals) pg.display.flip() #comment out for fast sim worldtime.next() clock.tick(30)