python cocos2d_Python-使用cocos2d实现的捕鱼游戏

1 #!/usr/bin/env python

2 #-*- coding: utf-8 -*-

3

4 importcocos5 importrandom6 importpyglet7 importos8 importmath9 importtime10 from cocos.director importdirector11 from cocos importlayer12 from cocos importscene13 from cocos importsprite14 from cocos importcollision_model as collision15 from cocos.scenes.transitions import *

16 from cocos.audio.pygame importmusic17 from cocos.audio.effect importEffect18 from cocos.cocosnode importCocosNode19

20

21 director.init(width=800, height=600, caption="wang and miao", audio_backend="sdl")22 working_dir = os.path.dirname(os.path.realpath(__file__))23 pyglet.resource.path = [os.path.join(".")]24 pyglet.resource.reindex()25 image_convert =pyglet.resource.image26 WIN_WIDTH, WIN_HEIGHT =director.get_window_size()27 score =028 las_bit = 1

29

30

31 classCannon(sprite.Sprite):32 def __init__(self):33 super(Cannon, self).__init__('textures/pao5.png')34 self.position = WIN_WIDTH//2, 035 self.scale = 0.8

36 self.angle =037

38 deffire(self, x, y):39 tanX = abs(float(y)/(x - self.position[0]+0.001))40 radian =math.atan(tanX)41 self.angle = 90 - radian*180/math.pi42 if x < WIN_WIDTH//2:43 self.angle = -self.angle44 duration = abs(self.angle) / 200.0

45 self.do(cocos.actions.RotateTo(self.angle, duration))46

47

48 classNet(sprite.Sprite):49 def __init__(self):50 super(Net, self).__init__('textures/B5.png')51 WIN_WIDTH, WIN_HEIGHT =director.get_window_size()52 self.position = WIN_WIDTH//2, 053 self.cshape = collision.AARectShape(cocos.euclid.Vector2(0, 0), self.width//2, self.height//2)54 self.schedule(self._update_cshape)55

56 def_update_cshape(self, dt):57 self.cshape.center = collision.eu.Vector2(*self.position)58

59 defshoot(self, x, y, angle):60 tan_corner =float(600)/(400+0.001)61 corner_radian =math.atan(tan_corner)62 corner_angle = 90 - corner_radian*180/math.pi63 if abs(angle) >corner_angle:64 mx = WIN_WIDTH + 10 if angle > 0 else -10

65 my = (WIN_WIDTH//2+10)/(x-WIN_WIDTH//2)*y if angle > 0 else (WIN_WIDTH//2+10)/(WIN_WIDTH//2-x)*y66 else:67 my = WIN_HEIGHT + 10

68 mx = WIN_WIDTH//2 + my/y*(x-WIN_WIDTH//2) if angle > 0 else WIN_WIDTH//2 - my/y*(WIN_WIDTH//2-x)69 distance = math.sqrt(abs(mx - WIN_WIDTH//2)**2+abs(my)**2)70 self.scale = 0.8

71 times = round(distance / 200)72 times = times if times else 1

73 self.do(cocos.actions.RotateTo(angle, 0)|cocos.actions.MoveTo((mx, my), 0.5*times)+cocos.actions.Delay(0.2)+cocos.actions.CallFunc(self.explode))74

75 defexplode(self):76 self.stop()77 self.kill()78

79

80 classFish(sprite.Sprite):81 def __init__(self, ft):82 self.name =ft83 WIN_WIDTH, WIN_HEIGHT =director.get_window_size()84 textures =[]85 for x in range(1, 10):86 if ft == 'ballfish':87 fish_path_str = 'textures/ballfish/clownfish_%s.png'

88 elif ft == 'greenfish':89 x = str(0) +str(x)90 fish_path_str = 'textures/greenfish/fish03_%s.png'

91 elif ft == 'redfish':92 x = str(0) +str(x)93 fish_path_str = 'textures/redfish/fish02_%s.png'

94 elif ft == 'swimming' and 0 < x < 5:95 x = 'r' +str(x)96 fish_path_str = 'textures/swimming/shark_%s.png'

97 else:98 continue

99 texture = pyglet.resource.image(fish_path_str %x)100 textures.append(texture)101 if ft == 'swimming':102 animation = pyglet.image.Animation.from_image_sequence(textures, 0.5)103 super(Fish, self).__init__(animation)104 self.position = -100, random.randint(120, WIN_HEIGHT-320)105 self.scale = 0.75

106 else:107 self.ds = random.randint(0, 10) * 30

108 animation = pyglet.image.Animation.from_image_sequence(textures, 0.1)109 super(Fish, self).__init__(animation)110 self.position = WIN_WIDTH+100+self.ds, random.randint(100, WIN_HEIGHT-self.height-20)111 self.scale = 0.5

112 self.cshape = collision.AARectShape(collision.eu.Vector2(0,0), self.width//2, self.height//2)113 self.schedule(self._update_cshape)114

115 def_update_cshape(self, dt):116 self.cshape.center = collision.eu.Vector2(*self.position)117

118 defswim(self, ft):119 if ft == 'swimming':120 self.do(cocos.actions.MoveTo((WIN_WIDTH+100, self.position[1]), 18)+cocos.actions.CallFunc(self.shark_btb_action))121 else:122 self.do(cocos.actions.MoveTo((-100-self.ds, self.position[1]), 12)+cocos.actions.CallFunc(self.small_fish_btb_action))123

124 defshark_btb_action(self):125 self.do(cocos.actions.RotateTo(180, 0))126 self.do(cocos.actions.MoveTo((-100, self.position[1]+random.choice([-50, 50])), 18)+cocos.actions.CallFunc(self.explode))127

128 defsmall_fish_btb_action(self):129 self.do(cocos.actions.RotateTo(180, 0))130 self.do(cocos.actions.MoveTo((WIN_WIDTH+100, self.position[1]+random.choice([-100, 100])), 12)+cocos.actions.CallFunc(self.explode))131

132 defexplode(self):133 self.stop()134 self.kill()135

136

137 classMenuBackgound(cocos.layer.Layer):138 def __init__(self):139 super(MenuBackgound, self).__init__()140 d_width, d_height =director.get_window_size()141 background = cocos.sprite.Sprite("textures/menu.png")142 background.position = d_width//2, d_height//2

143 logo_layer = cocos.sprite.Sprite("textures/logo.png")144 logo_layer.position = d_width//2 , d_height//2+180

145 self.add(background)146 self.add(logo_layer)147

148

149 classMainMenu(cocos.menu.Menu):150 def __init__(self):151 super(MainMenu, self).__init__()152 self.font_item['font_size'] = 50

153 self.font_item_selected['font_size'] = 50

154 self.font_item['color'] = (255, 255, 255, 25)155 self.font_item_selected['color'] = (215, 255, 255, 255)156

157 menu_start=cocos.menu.ImageMenuItem('textures/start.png', self.menu_start_callback)158 menu_setting = cocos.menu.ImageMenuItem('textures/set.png', self.menu_setting_callback)159 menu_help = cocos.menu.ImageMenuItem('textures/help.png', self.menu_help_callback)160 self.create_menu([menu_start, menu_setting, menu_help],161 layout_strategy = cocos.menu.fixedPositionMenuLayout([(420, 339), (420, 220), (420, 100)]),162 selected_effect=cocos.menu.zoom_in(),163 unselected_effect=cocos.menu.zoom_out())164

165 defmenu_start_callback(self):166 yinxiao=Effect('textures/biu.wav')167 yinxiao.play()168 layer =GameLayer()169 gamemenu =GameMenu()170 game_scene =scene.Scene(layer)171 game_scene.add(gamemenu)172 donghua = TurnOffTilesTransition(game_scene, 1)173 director.push(donghua)174

175 defmenu_help_callback(self):176 yinxiao=Effect('textures/biu.wav')177 yinxiao.play()178

179 defmenu_setting_callback(self):180 yinxiao=Effect('textures/biu.wav')181 yinxiao.play()182

183

184 classGameMenu(cocos.menu.Menu):185 def __init__(self):186 super(GameMenu, self).__init__()187 self.font_item['font_size'] = 50

188 self.font_item_selected['font_size'] = 50

189 self.font_item['color'] = (255, 255, 255, 25)190 self.font_item_selected['color'] = (215, 255, 255, 255)191

192 exit = cocos.menu.ImageMenuItem("textures/exit.png", self.exit_callback)193 self.create_menu([exit, ],194 layout_strategy=cocos.menu.fixedPositionMenuLayout([(80, 532),]),195 selected_effect=cocos.menu.zoom_in(),196 unselected_effect=cocos.menu.zoom_out())197

198 defexit_callback(self):199 director.pop()200 music.stop()201

202

203 classGameLayer(layer.Layer):204 is_event_handler =True205 def __init__(self):206 self.nets =layer.Layer()207 self.fishs =layer.Layer()208 super(GameLayer, self).__init__()209 self.bg = sprite.Sprite('textures/seaworld.jpg')210 WIN_WIDTH, WIN_HEIGHT =director.get_window_size()211 self.bg.position = WIN_WIDTH // 2, WIN_HEIGHT // 2

212 self.add(self.bg)213 self.cannon =Cannon()214 self.add(self.cannon)215

216 self.create_label()217 self.create_fish()218 self.play_bgm()219 self.adjust_las()220

221 self.add(self.nets)222 self.add(self.fishs)223 self.schedule(collision_handle, self.nets, self.fishs, self.score_value, self)224

225 defcreate_label(self):226 pyglet.font.add_file("CroissantD.ttf")227 self.score_label = cocos.text.Label('Score:',228 font_name="Times New Roman",229 font_size=32,230 anchor_x='center', anchor_y='center')231 self.score_label.position = WIN_WIDTH - 150, WIN_HEIGHT -50

232 self.score_value = cocos.text.Label('0',233 font_name="CroissantD",234 font_size=32,235 anchor_x='center', anchor_y='center')236 self.score_value.position = WIN_WIDTH - 60, WIN_HEIGHT - 50

237 self.add(self.score_label)238 self.add(self.score_value)239

240 defcreate_fish(self):241 add_small_fish(0, self.fishs)242 self.schedule_interval(add_small_fish, 3, self.fishs)243 self.schedule_interval(add_shark, 8, self.fishs)244

245 defadjust_las(self):246 self.do(cocos.actions.Delay(1.5)+cocos.actions.CallFunc(lambda: light_and_shade(0, self.bg)))247 self.schedule_interval(light_and_shade, 5, self.bg)248

249 defplay_bgm(self):250 music.load("textures/bgm.wav".encode())251 music.play(loops=-1)252 music.set_volume(2)253

254 defopen_net(self, x, y):255 onet = cocos.sprite.Sprite("textures/net.png")256 onet.position =x, y257 onet.scale = 0.5

258 self.add(onet)259 onet.do(cocos.actions.Delay(0.5)+cocos.actions.CallFunc(onet.kill))260

261 defon_mouse_motion(self, x, y, dx, dy):262 self.cannon.fire(x, y)263

264 defon_mouse_press(self, x, y, button, modiffiers):265 self.cannon.fire(x, y)266 if button ==pyglet.window.mouse.LEFT:267 if len(self.nets.children) >= 3:268 return

269 self.net =Net()270 self.nets.add(self.net)271 self.net.shoot(x, y, self.cannon.angle)272

273

274 defcollision_handle(dt, nets, fishs, label, game_layer):275 globalscore276 for i innets.children:277 for j infishs.children:278 if i[1].cshape.distance(j[1].cshape) ==0:279 i[1].explode()280 j[1].stop()281 j[1].do(cocos.actions.Delay(0.5)+cocos.actions.CallFunc(j[1].explode))282 game_layer.open_net(i[1].position[0], i[1].position[1])283 if j[1].name == "swimming":284 score = score + 3

285 else:286 score = score + 1

287 if score > 999:288 score = 999

289 label.element.text =str(score)290 break

291

292

293 defadd_small_fish(dt, fish_layer):294 if len(fish_layer.children) > 12:295 return

296 randnum = random.randrange(1, 6)297 for i inrange(randnum):298 ft = random.choice(['ballfish', 'greenfish', 'redfish'])299 fish =Fish(ft)300 fish_layer.add(fish)301 fish.swim(ft)302

303

304 defadd_shark(dt, fish_layer):305 if len(fish_layer.children) > 12:306 return

307 ft = "swimming"

308 fish =Fish(ft)309 fish_layer.add(fish)310 fish.swim(ft)311

312

313 deflight_and_shade(dt, bg):314 globallas_bit315 iflas_bit:316 bg.do(cocos.actions.FadeTo(127, 3.5))317 las_bit = las_bit - 1

318 else:319 bg.do(cocos.actions.FadeTo(255, 3.5))320 las_bit = las_bit + 1

321

322

323 if __name__ == '__main__':324 menu_bg=MenuBackgound()325 main_scence =scene.Scene(menu_bg)326 main_menu =MainMenu()327 main_scence.add(main_menu)328 director.run(main_scence)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值