pygame雪地打蝙蝠射击游戏.py

pygame雪地打蝙蝠射击游戏.py

pygame shoot bat in snow ground雪地打蝙蝠
pygame shoot bat in snow ground雪地打蝙蝠

pygame shoot bat in snow ground雪地打蝙蝠

精心配音的一个pygame射击小作品。以下是部分代码预览:

"""pygame雪地打蝙蝠射击游戏.py,在冰天雪地中,蝙蝠竟然不冬眠全部飞出来了,这是不合情理的,请按左右方向键头操作大炮朝天空打死这些不冬眠的奇怪蝙蝠."""

__author__ = "李兴球"
__date__ = "2018年6月"
__company__ = "风火轮编程"

import time
import pygame
from random import randint
from pygame.locals import *

class Sprite():
    def __init__(self,framesRight,framesLeft,x,y,w,h,sound = None):
        self.frames_right = framesRight
        self.frames_left = framesLeft
        self.costume_amounts = len(self.frames_left) # 造型数量
        self.costume_index=0                         # 造型索引号
        pass
        self.sound = sound                           # 声音
        
    def next_costume(self):
        """切换造型"""

    def move(self):
       """移动更新"""
        
   def draw(self):
        if self.xspeed>0:
            screen.blit(self.frames_right[self.costume_index],self.rect)
        else:            
            screen.blit(self.frames_left[self.costume_index],self.rect)

"""大炮类的建立"""
class Canon():
    pass     # 大炮类的代码最简单,如果看懂了全部代码,相信读者可以自行写出代码

"""炮弹类"""
class Bomb():
    pass
        
pygame.init()
screen_width,screen_height=480,360
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("雪地打蝙蝠--作者:李兴球_风火轮少儿编程")
背景 = pygame.image.load("slopes.png")
frame0 = pygame.image.load("bat1-a.png")
frame1 = pygame.image.load("bat1-b.png")
framesRight = [frame0,frame1]
framesLeft = [pygame.transform.flip(f,True,False) for f in framesRight]

font = pygame.font.Font("c:/windows/fonts/msyh.ttf",30)
textImage =font.render(" ",True,(0,25,255))
(tx,ty,tw,th) = textImage.get_rect()
textpos=(screen_width//2 - tw //2,screen_height//2 - th/2 -100)

pygame.mixer.init()
吱声  = pygame.mixer.Sound("Cricket.wav")
炮声  = pygame.mixer.Sound("榴弹炮.wav")
pygame.mixer.music.load("Melee- Menu.wav")
pygame.mixer.music.play(-1,0)

# 新建一些蝙蝠bat 
bat0 = Sprite(framesRight,framesLeft,100,20,40,40,吱声)
bat1 = Sprite(framesRight,framesLeft,130,40,40,40,吱声)
bat2 = Sprite(framesRight,framesLeft,40,80,40,40,吱声)
bat3 = Sprite(framesRight,framesLeft,160,120,40,40,吱声)
bat4 = Sprite(framesRight,framesLeft,320,160,40,40,吱声)
bats = [bat0,bat1,bat2,bat3,bat4 ]

大炮 = Canon("大炮.png",(200,300))        
炮弹发射中= False
发射间隔 = 100
running = True
while running:
    # 时不时地生成一只蝙蝠
    if randint(0,1000) ==0:
        randx = randint(0,screen_width)
        randy = randint(0,100)
        bats.append(Sprite(framesRight,framesLeft,randx,randy,40,40,吱声))
        
    for event in pygame.event.get():
        if event.type==QUIT:running = False        
 
    pass      
 
    screen.blit(背景,(0,0))
    [bat.draw() for bat in bats if bat.delete==0]
    大炮.draw()
    if '炮弹' in locals():  炮弹.draw()
    
    screen.blit(textImage,textpos)
    pygame.display.update()
    [bats.remove(bat) for bat in bats if bat.delete==1]

pygame.quit()
print("游戏结束,谢谢")
    

 

下载完整源代码与素材,请

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

李兴球

李兴球的博客是Python创意编程原创博客