飞机大战系列 Space War 2.0(开源)

为了增强 Space War 系列游戏的趣味性,这里有两种主要途径。一是提高敌机的智能性,比如能有更不可预测的移动轨迹和发射子弹的功能以增加难度和挑战性;二是开发成双人小游戏,双人对战的模式。显然,在 Space War 1.0 的基础上,开发双人版本更为简单(实际上也不简单,很多底层逻辑都要大改,只有一些实现基本功能的代码得以保留),所以我就先开发双人版。

而经过我为期一天的开发与完善,现在我正式宣布Space War2.0已经完成了(在开发过程中也同时对 Space War 1.0 进行了升级,这就有了 Space War 1.1 的诞生)飞机大战系列 Space War 1.1(开源)

在 Space War 2.0 中,plane1通过WSAD键进行移动,JK键进行旋转,SPACE(空格)键进行射击。plane2通过上下左右键进行移动,鼠标进行旋转,左键进行射击。为了维持游戏平衡(用鼠标的肯定简单一点)我设置了plane1血量100,plane2血量50;plane1弹夹容量200,plane2弹夹容量100;plane1装弹时间3秒,plane2装弹时间6秒。下面是打斗示例。

Space War 2.0 2024-07-16

下面给出代码。Space War 2.0 与 Space War 1.0 系列最大的不同就是删去了Enemy_list 和 Enemy_out 列表,因为对plane1和plane2而言都只有一个敌机对象,所以在两个飞机类的方法中,也是将这些设计敌机列表的语句给换掉了。资源就比1.0版多了一个红色的子弹(在最下面),原先的黄色子弹改名为bullet1.png

# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 11:41:27 2024

@author: yq1215
"""

import pygame
import sys
import math
import time
import random
from pygame.locals import *
import winsound

#定义Bullet(子弹)类
class Bullet(pygame.sprite.Sprite):
    def __init__(self, screen, image, x, y, angle, damage):
        pygame.sprite.Sprite.__init__(self)
        #self.image = self.image1 = pygame.Surface((10, 1))  # 用一个简单的矩形代替子弹图片  
        #self.image.fill((255, 255, 0))  # 设置为黄色  
        #self.image = self.image1 = pygame.image.load("bullet.png")
        self.image = self.image1 =  image
        self.rect = self.image.get_rect()  
        self.rect.center = (x, y)  
        self.screen = screen
        self.angle = angle
        self.image = pygame.transform.rotate(self.image1,self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)
        self.vel = 15
        self.x =  x
        self.y =  y
        self.damage = damage
    def display(self):  #显示子弹图片的方法
        self.screen.blit(self.image, (self.x, self.y))  #将创建的子弹图片按设定的坐标贴到窗口中
    def move(self): #子弹移动方法
        self.x += self.vel*math.cos(math.radians(self.angle))
        self.y -= self.vel*math.sin(math.radians(self.angle))
    def judge(self):    #判断子弹是否出界
        if self.y<0 or self.y>1200 or self.x<0 or self.x>1800:  
            return True
        else:
            return False
    def judge1(self, enemy):   #判断子弹是否击中敌机
        flag = False
        if (self.x>enemy.rect.centerx-10 and      
            self.x<enemy.rect.centerx+10 and
            self.y>enemy.rect.centery-10 and
            self.y<enemy.rect.centery+10):  
            winsound.Beep(500,1)
            enemy.health -= self.damage
            flag = True
        return flag
    
#定义Plane1(飞机)类
class Plane1(pygame.sprite.Sprite):  
    def __init__(self, screen, image, bullet_image, x, y, max_health, max_bullet):  
        pygame.sprite.Sprite.__init__(self)
        self.image = image
        self.image1 = self.image  #保留原图
        self.rect = self.image.get_rect()  
        self.rect.center = (x, y)  
        self.screen = screen
        self.score = 0       #当前得分
        
        self.acceleration = 0.2  
        self.max_speed = 8  
        self.vel_x = 0  
        self.vel_y = 0 
        self.angular_speed = 1  #角速度
        self.current_angle = 0
        
        self.max_health = max_health  
        self.health = max_health  # 初始生命值等于最大生命值
        self.health_bar = HealthBar(screen, 300, 80, 200, 20, max_health)  # 创建血条对象
        
        self.bullet_image = bullet_image
        self.bullet_list = []   #存储发射出去的子弹对象引用
        self.bullet_clip = max_bullet  # 弹夹容量  
        self.bullets_in_clip = self.bullet_clip  # 当前弹夹内子弹数量  
        self.reloading = False  # 是否正在填充子弹  
        self.reload_start_time = 0  # 开始填充子弹的时间点  
        self.reload_time = 6  # 填充子弹所需时间 
        self.bullet_clip_bar = HealthBar(screen, 300, 105, 200, 20, max_bullet)  #创建血条对象显示弹夹条
  
    def move(self, keys):   #移动
        if keys[K_w] and self.rect.top <= 0:
            self.vel_y = 0
        elif keys[K_w] and self.rect.top > 0:  
            if self.vel_y > -self.max_speed:  
                self.vel_y -= self.acceleration  
        elif self.vel_y < 0:  
            self.vel_y += self.acceleration  
            if self.vel_y 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值