?导读
最近读到一位大佬的游戏文章之后,手痒难耐就想自己也写一个小游戏。苦于没有游戏素材在网上搜也都是付费的,我就随便写了一点点来给自己解解馋。😗 好了废话不多说我们现在先试一下游戏效果。不好玩也不要说出来,嘻嘻嘻。后面我们在慢慢的推出好玩的来。
?代码和详解
?主要部分
对python多线程还不理解的大佬可以看看我的这篇文章,包教包会有需要的私信我,给你开后门
.
?源码加详解
.
?对坦克设定进行初始化
pygame.init()
size=(width,heiht)=(800,600)
screen=pygame.display.set_mode(size)
pygame.display.set_caption("坦克大战")
fps=30
fpsclock=pygame.time.Clock()
color_back=(0,0,0)
my_tank_speed=[4,4]
tank_img=pygame.image.load("tanke.bmp")
bullet_img=pygame.image.load("zidan.bmp")
new_tank_img=tank_img
tank_rect=tank_img.get_rect()
tank_rect.topleft=[400,300]
left_count=right_count=top_count=bottom_count=0
my_rotate=0
my_bullet=0
my_value=0;
font=pygame.font.Font(None,32)
text=font.render("value:"+str(my_value),True,(255,0,0))
text_rect=text.get_rect()
text_rect.left=10
text_rect.top=10
.
?定义坦克类
class Tank(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
self.speed_top=1
self.speed_left = 1
def update(self, *args):
self.rect.top=self.rect.top+self.speed_top
self.rect.left = self.rect.left + self.speed_left
if (self.rect.left < 0) or (self.rect.right > width):
self.speed_left=-self.speed_left
if (self.rect.top < 0) or (self.rect.bottom > heiht):
self.speed_top = -self.speed_top
.
?定义子弹类
class bullet(pygame.sprite.Sprite):
def __init__(self,filename,my_tank_rect,my_rotate):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.speed_top=5
self.speed_left = 5
self.my_rotate=my_rotate
self.rect.left = my_tank_rect.left + (my_tank_rect.right - my_tank_rect.left) / 2
self.rect.right = self.rect.left + 12.5
self.rect.top = my_tank_rect.top + (my_tank_rect.bottom - my_tank_rect.top) / 2
self.rect.bottom = self.rect.top + 12.5
self.speed=[0,0]
def update(self, *args):
pass
speed_size=self.speed_top
if self.my_rotate == 0:
self.speed = [0, -speed_size]
elif self.my_rotate == 180 :
self.speed = [0, speed_size]
elif self.my_rotate == 90 :
self.speed = [-speed_size, 0]
elif self.my_rotate == -90 :
self.speed = [speed_size, 0]
self.rect.top = self.rect.top + self.speed[1]
self.rect.left = self.rect.left + self.speed[0]
if (self.rect.left < 0) or (self.rect.right > width):
my_bullet = 0
self.kill()
if (self.rect.top < 0) or (self.rect.bottom > heiht):
my_bullet = 0
self.kill()
.
?剩下的就是主程序了
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
pass
bullet_sprite1 = bullet("zidan.bmp", tank_rect, my_rotate)
bullet_group.add(bullet_sprite1)
my_bullet = my_bullet + 1
tank_speed=my_tank_speed[0]
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
tank_rect = tank_rect.move(-tank_speed,0)
left_count=left_count+1
if left_count==1:
my_rotate=90
new_tank_img=pygame.transform.rotate(tank_img,90)
right_count=top_count=bottom_count=0
elif event.key==pygame.K_RIGHT:
tank_rect = tank_rect.move(tank_speed, 0)
right_count = right_count + 1
if right_count == 1:
my_rotate=-90
new_tank_img = pygame.transform.rotate(tank_img, -90)
left_count = top_count = bottom_count = 0
elif event.key == pygame.K_UP:
tank_rect = tank_rect.move(0, -tank_speed)
top_count = top_count + 1
if top_count == 1:
my_rotate=0
new_tank_img = pygame.transform.rotate(tank_img, 0)
left_count = right_count = bottom_count = 0
elif event.key == pygame.K_DOWN:
tank_rect = tank_rect.move(0, tank_speed)
bottom_count = bottom_count + 1
if bottom_count == 1:
my_rotate=180
new_tank_img = pygame.transform.rotate(tank_img, 180)
left_count = right_count = top_count = 0
if tank_rect.left<0:
tank_rect =tank_rect.move(1,0)
elif tank_rect.right>width:
tank_rect =tank_rect.move(-1, 0)
elif tank_rect.top<0:
tank_rect =tank_rect.move(0, 1)
elif tank_rect.bottom>heiht:
tank_rect =tank_rect.move(0, -1)
if pygame.sprite.groupcollide(bullet_group,tank_group,True,True):
my_value=my_value+1
text = font.render("value:" + str(my_value), True, (255, 0, 0))
print("value:",my_value)
my_bullet = len(bullet_group)
print("bullet:",my_bullet)
if len(tank_group)==0:
retank1 = Tank('tanke2.bmp', [np.random.random_integers(80,600), np.random.random_integers(80,600)])
retank2 = Tank('tanke3.bmp', [np.random.random_integers(80,600), np.random.random_integers(80,600)])
retank3 = Tank('tanke4.bmp', [np.random.random_integers(80,600), np.random.random_integers(80,600)])
retank4 = Tank('tanke5.bmp', [np.random.random_integers(80,600), np.random.random_integers(80,600)])
tank_group.add(retank1)
tank_group.add(retank2)
tank_group.add(retank3)
tank_group.add(retank4)
screen.fill(color_back)
screen.blit(new_tank_img,tank_rect)
screen.blit(text,text_rect)
tank_group.update()
tank_group.draw(screen)
bullet_group.update()
bullet_group.draw(screen)
pygame.display.update()
fpsclock.tick(fps)
.
?播放MP3
这里用的使用非常方便的 playsound 模块
def tanke_mp3():
playsound('tanke.mp3')
?最终要的事
有什么问题大家可以私信我,另外给大家推荐几个不错的专栏
📣小白练手专栏,适合刚入手的新人欢迎订阅编程小白进阶
📣python有趣练手项目里面包括了像《机器人尬聊》《恶搞程序》这样的有趣文章,可以让你快乐学python练手项目专栏
📣另外想学JavaWeb进厂的同学可以看看这个专栏:传送们
📣这是个面试和考研的算法练习我们一起加油上岸之路
点赞关注不迷路。
|