飞机大战代码-plane_sprites

import random
import pygame

# 设置常量

# 设置屏幕大小
SCREEN_RECT = pygame.Rect(0,0,400,700)
# 刷新帧率
FRAME_SEC = 60
# 创建敌机的定时器常量
ENEMY_EVENT = pygame.USEREVENT
# 英雄发射子弹事件
HERO_FIRE_EVENT = pygame.USEREVENT + 1

# 基类精灵

class GameSprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed=1):

初始化父类,并向父类中传参
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed

def update(self):
self.rect.y += self.speed

#背景精灵

class Background(GameSprite):

#is_alt属性用来区分第一张背景图和第二张背景图
def __init__(self, is_alt = False):

#初始化父类,并向父类中传参
super().__init__("./images/background.png")
if is_alt:
#第二张背景图在屏幕上方
self.rect.y = - self.rect.height
def update(self):
super().update()
#当背景背景图完全运动到屏幕下方时,将图片移到屏幕上方
if self.rect.y >= SCREEN_RECT.height:
self.rect.y = - self.rect.height

#敌机精灵

class Enemy(GameSprite):

def __init__(self):

super().__init__("./images/enemy1.png")
#设置敌机的随机初始速度
self.speed = random.randint(1,3)
设置敌机的随机初始位置
self.rect.bottom = 0
max_x = SCREEN_RECT.width - self.rect.width
self.rect.x = random.randint(0, max_x)

def update(self):

super().update()
#若敌机飞出屏幕,则从精灵组删除
if self.rect.y >= SCREEN_RECT.height:
self.kill()

#英雄精灵

class Hero(GameSprite):

def __init__(self):

super().__init__("./images/me1.png",0)
#设置英雄的初始化位置
self.rect.centerx = SCREEN_RECT.centerx
self.rect.bottom = SCREEN_RECT.height - 120
#创建子弹精灵组
self.bullets = pygame.sprite.Group()

def update(self):

#英雄在水平方向移动
self.rect.x += self.speed
#控制英雄不能移出屏幕
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.right > SCREEN_RECT.right:
self.rect.right = SCREEN_RECT.right

def fire(self):

#创建子弹精灵
bullet = Bullet()
#设置精灵位置
bullet.rect.bottom = self.rect.y - 0 * 20
bullet.rect.centerx = self.rect.centerx
#将精灵添加到精灵组
self.bullets.add(bullet)

#设置子弹精灵

class Bullet(GameSprite):
def __init__(self):

#调用父类方法 设置图片和初始化速度
super().__init__("./images/bullet1.png", -1)

def update(self):

#调用父类方法,让子弹在垂直方向运动
super().update()
#判断子弹飞出屏幕,如果是,需要将敌机从精灵组删除
if self.rect.bottom <= 0:
self.kill()

转载于:https://www.cnblogs.com/aliceleemuzi/p/11066117.html

package cn.feike.shoot; import java.awt.Graphics; import java.awt.image.BufferedImage; public abstract class FlyingObject { protected double x;//物体的x坐标 protected double y;//物体的y坐标 protected double width;//物体的宽 protected double heigth;//物体的高 protected BufferedImage image;//当前正在显示的图片 protected int index = 0;//图片数组下标序号,子类中使用 protected double step;//飞行物每次(1/24秒)移动的距离 protected int life;//命 protected int state;//飞行物的状态 public static final int ACTIVE=0;//活着状态 public static final int DEAD=1;//死亡状态 public static final int REMOVE=2;//回收状态 //默认构造器 public FlyingObject() { life = 1; state = ACTIVE; } //有参构造器 public FlyingObject(double width,double heigth){ this();//调用无参数的构造器,必须写在第一行. this.x = (int)(Math.random()*(480-width)); this.y = -heigth; this.width = width; this.heigth = heigth; step = Math.random()*3+0.8;//初始化step为[0.8,3.8)之间的数 } //重写toString方法 public String toString() { return x+","+y+","+width+","+heigth+","+image; } //重写paint,方便子类对象的使用 public void paint(Graphics g) { g.drawImage(image, (int)x, (int)y, null);//绘制图片 } //飞行物移动的move方法 /** * 重构了move,方法实现播放销毁动画功能 */ public void move(){ if(state == ACTIVE){ y += step; return ; } if(state == DEAD){ //从子类对象中获取下一张照片 BufferedImage img = nextImage(); if(img == null){ state = REMOVE;//没有照片则回收 }else{ image = img;//否则把子类的图片传给image } //越界则销毁 if(y>=825){ state = REMOVE; } } } /** * 子类中必须有的方法,返回下一个要播放的照片引用, * 如果返回null表示没有可播放的照片了. */ protected abstract BufferedImage nextImage(); /** * 飞行物被打了一下 */ public void hit(){ if(life>0){ life--; } if(life==0){ state = DEAD; } } /** * 碰撞检测的方法 * 检测物体的位置是否在碰撞的范围内. * (子弹是否在飞行物的碰撞范围内) */ public boolean duang(FlyingObject obj){ //this(x,y,w,h) //obj(x,y,w,h) double x1 = this.x - obj.width; double x2 = this.x + this.width; double y1 = this.y - obj.width; double y2 = this.y + this.heigth; return x1<obj.x&&obj;.x<x2&&y1;<obj.y&&obj;.y<y2; } /** 重构FlyingObject,添加了状态检查方法 */ /** 检查飞行物死了吗 */ public boolean isDead(){ return state == DEAD; } /** 检查飞行物是否活动的 */ public boolean isActive(){ return state == ACTIVE; } /** 检查飞行是否可以被删除*/ public boolean canRemove(){ return state == REMOVE; } /** 飞行物添加"去死"方法*/ public void goDead(){ if(isActive()){ state = DEAD; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值