Python 游戏编程之实现飞机大战(含源代码)( 三 )
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet1.png").convert_alpha
self.rect = self.image.get_rect
self.rect.left, self.rect.top = position
self.speed = 12
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
class Bullet2(pygame.sprite.Sprite):
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet2.png").convert_alpha
self.rect = self.image.get_rect
self.rect.left, self.rect.top = position
self.speed = 14
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
五.发放补给包
游戏设计每30秒随机发放一个补给包,可 能是超级子弹,也可能是全屏炸弹 。 补给包有自己的图像和运动轨迹 , 定义一个模块supply.py:
import pygame
from random import *
class Bullet_Supply(pygame.sprite.Sprite):
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bullet_supply.png").convert_alpha
self.rect = self.image.get_rect
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left, self.rect.bottom = \
randint(0, self.width - self.rect.width), -100
self.speed = 5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left, self.rect.bottom = \
randint(0, self.width - self.rect.width), -100
class Bomb_Supply(pygame.sprite.Sprite):
def __init__(self, bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images/bomb_supply.png").convert_alpha
self.rect = self.image.get_rect
self.width, self.height = bg_size[0], bg_size[1]
self.rect.left, self.rect.bottom = \
randint(0, self.width - self.rect.width), -100
self.speed = 5
self.active = False
self.mask = pygame.mask.from_surface(self.image)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.active = False
def reset(self):
self.active = True
self.rect.left, self.rect.bottom = \
randint(0, self.width - self.rect.width), -100
六.主模块所有的模块都到齐了 , 接下来就该实现我们的主模块:
# main.py
import pygame
import sys
import traceback
import myplane
import enemy
import bullet
import supply
from pygame.locals import *
from random import *
pygame.init
pygame.mixer.init
bg_size = width, height = 480, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战 -- FishC Demo")
background = pygame.image.load("images/background.png").convert
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
- 王储|壹周游闻第20期:直播打赏实行实名制;沙特王储收购SNK
- 环境|环境标识认知转盘游戏
- realme X|手机打游戏太卡?行家:那是你还没换这几款
- Play|Google Play公布2020年度最佳应用和游戏排行榜
- 第一款骁龙888游戏手机:红魔6官宣
- 告诉|阿里大佬告诉你如何一分钟利用Python在家告别会员看电影
- Python源码阅读-基础1
- Linux(服务器编程):百万并发服务器系统参数调优
- Python调用时使用*和**
- 如何基于Python实现自动化控制鼠标和键盘操作