一、
前置:python3.5、pycharm、pygame(pycharm内安装)
二、工程下载链接
点击下载代码(源码+资源文件)
https://download.csdn.net/download/m0_38078987/10420779
三、描述
1.运行截图
自己把敌机换成灭霸了,还挺有趣
四、源码
import pygame
from pygame.locals import*
import random
#是指游戏屏幕带下
SCREEN_WIDTH=480
SCREEN_HEIGHT=800
#子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self,bullet_img,init_pos):
pygame.sprite.Sprite.__init__(self)
self.image =bullet_img
self.rect =self.image.get_rect()
self.rect.midbottom =init_pos
self.speed =10
def move(self):
self.rect.top -= self.speed
#2定义一个飞机类
class Player(pygame.sprite.Sprite):
def __init__(self,plane_img,player_rect,init_pos):
pygame.sprite.Sprite.__init__(self)
self.image =[]
for i in range(len(player_rect)):
self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
self.bullets = pygame.sprite.Group()
self.rect =player_rect[0]#为图片的矩形初始化赋值
self.rect.topleft=init_pos#初始化位置
self.speed=10#初始化飞机的速度
self.img_index=1#初始化图片的索引 1代表第二张图片
#发射子弹前提是没被击中
self.is_hit =False
#4.发射子弹
def shoot(self,bullet_img):
bullet =Bullet(bullet_img,self.rect.midtop)
self.bullets.add(bullet)
def moveUp(self):
if self.rect.top <= 0: # rect.top<=0 飞机不能出去
self.rect.top = 0
else:
sel