基于Python实现射击小游戏的制作

这篇文章主要介绍了如何利用Python制作一个自己专属的第一人称射击小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手试一试

1.游戏画面

1.1开始

1.2射击怪物

2.涉及知识点

1.sprites

2.pygame混音器

3.图章   

4.python基础语法

3.代码

3.1发射声

from sprites import * try: import pygame pygame.mixer.init() fire_sound = pygame.mixer.Sound("audio/发射声.wav") cricket_sound = pygame.mixer.Sound('audio/cricket.wav') except: import sys input("本程序需要pygame混音器支持以便配音,请先在cmd下用pip install pygame安装此模块。") 

3.2背景

width,height = 480,360 screen = Screen() screen.bgpic('res/ghosthouse.jpg-600') screen.setup(width,height) batimages = ['res/bat1.png-600','res/bat2.png-600'] batindex = 0 bat = Sprite(visible=False,pos=(-50-width//2,100)) bat.dx = 3 bat.dy = 0 bat.alive = True bat.show()

3.3射击效果

def bat_alt_costume(): global batindex batindex = 1 - batindex bat.shape(batimages[batindex]) screen.ontimer(bat_alt_costume,90) bat_alt_costume() hole = Sprite(shape='res/Bullet_Hole.png-600',visible=False) m1 = Mouse(1)           # 鼠标左键 m3 = Mouse(3)           # 鼠标右键 clock = Clock()         # 时钟对象 start_stamp = False while True: bat.move(bat.dx,bat.dy) # 掉到地面就盖图章,留下尸体 if bat.ycor()  width//2 : bat.reborn(-500-width//2,100,3,0,delay=2) bat.alive = True bat.setheading(0) hole.goto(mouse_position()) # 发射子弹,用盖图章留下弹洞,为防连续发射用了start_stamp变量 if m1.down() and not start_stamp: hole.stamp() start_stamp = True try: fire_sound.play() except: pass # 松开按键后 if not m1.down():start_stamp = False clock.tick(60) 

4.经验总结

利用python各种游戏库可以做任何小游戏

到此这篇关于基于Python实现射击小游戏的制作的文章就介绍到这了,更多相关Python射击游戏内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是基于Python实现射击小游戏的制作的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python