飞机大战子弹传参问题

项目场景:

本人初学python,想通过实现飞机大战游戏来巩固学习成果,目前已实现飞机和敌机的移动及子弹发射,现在在实现爆炸功能时遇到一些问题。发帖求助

问题描述:

我在enemyplane类中定义了enemyplaneisboom方法来判断敌机是否被我方飞机子弹击中,若击中返回self.isboom=True,否则返回False;通过返回值isboom判断是否显示敌机爆炸的图片。
    def enemyplaneisboom(self):  # 判断子弹是否击中敌机
    #此处不知道怎么拿到我方飞机的子弹坐标,故用汉字进行描述
        if self.战机子弹x坐标 > self.x and self.战机子弹x坐标 < self.x+20:
            if self.战机子弹y坐标 > self.y and self.战机子弹y坐标 < self.y+20:
                print('敌机被战机的子弹击中')
                self.isboom = True
        else:
            self.isboom = False

    def display(self):
        if self.isboom:  # 如果isboom=True显示爆炸图片
            self.image = pygame.image.load(
                r'..\images\dbz.png')
            self.screen.blit(self.image, (self.x, self.y))  # 在战机位置展示爆炸图片
        else:  # 如果没爆炸,正常显示敌方飞机
            self.image = pygame.image.load(
                r'..\images\dj.png')
            self.screen.blit(self.image, (self.x, self.y))
在myplane类中定义子弹发射方法myplaneshoot(按空格键调用):
def myplaneshoot(self):  # 飞机的射击方法
        # 我机发射子弹
        newbullet = mybullet(self.x, self.y, self.screen, self.image)
        self.mybulletlist.append(newbullet)
        
class mybullet(bullet):#我方子弹类
    def __init__(self, x, y, screen, image):
        super().__init__(x, y, screen, image)

    def display(self):
        self.wzdimage = pygame.image.load(
            r'..\images\wzd.png')
        self.screen.blit(self.wzdimage, (self.x, self.y))
    def move(self):
        self.y -= 10
        pass
    def judgemybullet(self):#判断是否越界
        if self.y < 0:
            return True
        else:
            return False

困惑:不清楚怎么拿到我方飞机子弹发射后的坐标,希望大牛或者有此项目经验的老哥帮忙看下,不胜感激!下面是完整代码:


项目完整代码块:

import pygame
import random
from pygame.constants import KEYDOWN, K_RIGHT, K_SPACE, K_a
from pygame.locals import *

class plane(object):#飞机父类
    def __init__(self,screen, image):
        # 初始化函数,默认被调用,每个飞机对象都有的参数
        self.screen = screen
        self.image = image
    pass

class myplane(plane):#我方飞机
    def __init__(self, screen, image):
        super().__init__( screen, image)
        self.x = 135  # 飞机起始位置
        self.y = 450
        self.mybulletlist = []  # 我方子弹列表
        self.image = pygame.image.load(
            r'..\images\wj.png')  # 我方战机图片
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
        # 我方子弹回收机制
        delmybulletlist = []
        for item1 in self.mybulletlist:
            if item1.judgemybullet():
                delmybulletlist.append(item1)
                pass
            pass

        for item2 in delmybulletlist:
            self.mybulletlist.remove(item2)
            pass

        for item3 in self.mybulletlist:
            item3.display()
            item3.move()

    def movel(self):  # 如果按下左键,调用这个方法
        if self.x > 10:
            self.x -= 10
        pass

    def mover(self):  # 按下右键时,调用这个方法
        if self.x < 250:
            self.x += 10
        pass

    def myplaneshoot(self):  # 飞机的射击方法
        # 我机发射子弹
        newbullet = mybullet(self.x, self.y, self.screen, self.image)
        self.mybulletlist.append(newbullet)

class enemyplane(plane):#敌机
    def __init__(self, screen, image):
        super().__init__( screen, image)
        self.x = 132  # 敌机位置
        self.y = 0
        self.isboom = False
        self.direction = 'right'
        self.enemybulletlist = []  # 敌机子弹列表

    def enemyplanemove(self):  # 随机移动
        if self.x < 0:
            self.direction = 'right'
        elif self.x > 250:
            self.direction = 'left'
        if self.direction == 'right':
            self.x += 5
        elif self.direction == 'left':
            self.x -= 5
        pass

    def enemyplaneisboom(self):  # 判断子弹是否击中敌机
      
        if self.战机子弹x坐标 > self.x and self.战机子弹x坐标 < self.x+20:
            if self.战机子弹y坐标 > self.y and self.战机子弹y坐标 < self.y+20:
                print('敌机被我方子弹击中')
                self.isboom = True
        else:
            self.isboom = False

    def display(self):
        if self.isboom:  # 如果爆炸了显示爆炸图片
            self.image = pygame.image.load(
                r'..\images\dbz.png')
            self.screen.blit(self.image, (self.x, self.y))  # 在战机位置展示爆炸图片
        else:  # 如果没爆炸,正常显示 敌方飞机
            self.image = pygame.image.load(
                r'..\images\dj.png')
            self.screen.blit(self.image, (self.x, self.y))
            # 敌机子弹回收机制
            delenemybulletlist = []
            for item1 in self.enemybulletlist:
                if item1.judgeenemybullet():
                    delenemybulletlist.append(item1)
                    pass
                pass

            for item2 in delenemybulletlist:
                self.enemybulletlist.remove(item2)
                pass

            for item3 in self.enemybulletlist:
                item3.display()
                item3.move()

    def enemyplaneshoot(self):
        # 随机发射子弹
        t = random.randint(1, 10)
        if t == 5:
            newbullet = enemybullet(self.x, self.y, self.screen, self.image)
            self.enemybulletlist.append(newbullet)
        pass

class bullet(object):#子弹父类
    def __init__(self, x, y, screen, image):
        # 初始化函数,默认被调用,每个子弹对象都有的参数
        self.x = x
        self.y = y
        self.screen = screen
        self.image = image
    pass

class mybullet(bullet):#我方子弹类
    def __init__(self, x, y, screen, image):
        super().__init__(x, y, screen, image)

    def display(self):
        self.wzdimage = pygame.image.load(
            r'..\images\wzd.png')
        self.screen.blit(self.wzdimage, (self.x, self.y))
    def move(self):
        self.y -= 10
        pass
    def judgemybullet(self):#判断是否越界
        if self.y < 0:
            return True
        else:
            return False

class enemybullet(bullet):#敌机子弹类
    def __init__(self, x, y, screen, image):
        super().__init__(x, y, screen, image)

    def display(self):
        self.dzdimage = pygame.image.load(
            r'..\images\dzd.png')
        self.screen.blit(self.dzdimage, (self.x, self.y))

    def move(self):
        self.y += 10
        pass

    def judgeenemybullet(self):#判断是否越界
        if self.y > 500:
            return True
        else:
            return False

def keycontrol(myplane):#键盘控制
    eventlist = pygame.event.get()
    pygame.key.set_repeat(400, 90)
    # pygame.key.set_repeat(40090)多次按一个键时的处理方法,第一次按键事件等待400毫秒再发出,后续事件间隔90毫秒。
    for event in eventlist:
        if event.type == QUIT:
            print('退出')
            exit()
        elif event.type == KEYDOWN:
            if event.key == K_a or event.key == K_LEFT:
                print('left')
                myplane.movel()
            elif event.key == K_d or event.key == K_RIGHT:
                print('right')
                myplane.mover()
            elif event.key == K_SPACE:
                print('space')
                myplane.myplaneshoot()
    pass

def main():#主程序
    pygame.display.set_caption('飞机游戏')
    screen = pygame.display.set_mode((300, 500), depth=32)
    bgd = pygame.image.load(r'..\images\bg.png')
    myplaneimage = pygame.image.load(
        r'..\images\wj.png')
    enemyplaneimage = pygame.image.load(
        r'..\images\dj.png')
    mypl = myplane(screen, myplaneimage)
    enemypl = enemyplane(screen, enemyplaneimage)
    while True:
        screen.blit(bgd, (0, 0))
        mypl.display()
        enemypl.display()
        enemypl.enemyplanemove()
        enemypl.enemyplaneshoot()
        enemypl.enemyplaneisboom()#判断敌机是否爆炸
        keycontrol(mypl)#键盘控制
        pygame.display.update()#刷新
        pygame.time.Clock().tick(15)

if __name__ == '__main__':
    main()
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值