会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134270个问题

#坦克碰撞方法
def tank_hit_tank(self,tank):
    print('tank   ',tank.live)
    if self.live and tank.live:
        if collide_rect(self,tank):
            self.rect.left = self.old_left
            self.rect.top = self.old_top
#我方坦克
if MainGame.my_tank and MainGame.my_tank.live:
    MainGame.my_tank.display_tank()
else:
    MainGame.my_tank = None
if MainGame.my_tank and MainGame.my_tank.live:
    if MainGame.my_tank.remove:
        MainGame.my_tank.move()
        MainGame.my_tank.tank_hit_wall()
        for enemy in MainGame.enemy_tanks:
            print("检测我方是否碰撞敌方坦克")
            MainGame.my_tank.tank_hit_tank(enemy)
#敌方坦克
def display_enermy_tank(self):
    for enemy_tank in self.enemy_tanks:
        if enemy_tank.live:
            enemy_tank.display_tank()
            enemy_tank.rand_move()
            enemy_tank.tank_hit_wall()
            print("检测敌方是否碰撞我方坦克")
            enemy_tank.tank_hit_tank(MainGame.my_tank)
            enemy_bullet =enemy_tank.shot()
            if enemy_bullet:
                MainGame.enemy_bullet_list.append(enemy_bullet)
        else:
            MainGame.enemy_tanks.remove(enemy_tank)

老师,这节课讲的坦克的碰撞,上面是几段相关代码当我方坦克被击败后,代码报错live不存在,我试了改成if self.live and tank就不报错了,看问题好像出现在我方坦克被击败后,my_tank置为None,所以获取他的live失败,但是不明白,为什么敌方坦克被击败后也删除了,但是敌方坦克都被击败后也不报错。

我模拟写了一段代码:这个结果我也不是很明白,希望帮忙解答

Test:
    (a):
        .a = a
        .live = ():
        (.a)
t_list = []
i ():
    t = Test(i)
    t_list.append(t)
(t_list)
n t_list:
    n.prin()
    n.live:
        ()
    :
        (n)
        t_list.remove(n)
    (n.live)
(t_list)

F:\python_env\tank_env\Scripts\python.exe F:/python_env/tank_env/test.py

[<__main__.Test object at 0x000001FC6A6B3FA0>, <__main__.Test object at 0x000001FC6A6B1C90>, <__main__.Test object at 0x000001FC6A6B1B40>, <__main__.Test object at 0x000001FC6A6B2AD0>]

0

<__main__.Test object at 0x000001FC6A6B3FA0>

False

2

<__main__.Test object at 0x000001FC6A6B1B40>

False

[<__main__.Test object at 0x000001FC6A6B1C90>, <__main__.Test object at 0x000001FC6A6B2AD0>]


Process finished with exit code 0


Python全系列/第二阶段:Python 深入与提高/坦克大战 646楼

import pygame as p
screen_widht = 1800
screen_height = 1000
bg_color = p.Color(0,0,0)
text_color = p.Color(255,0,0)
class MainGame():
    window =None
    my_tank=None
    def __init__(self):
        pass
    def startGame(self):
        p.display.init()
        MainGame.window=p.display.set_mode([screen_widht,screen_height]) #设置高度和宽度,必须是一个列表
        MainGame.my_tank=Tank(850,400)

        p.display.set_caption('坦克大战')
        while True:
            MainGame.window.fill(bg_color)
            self.getEvent()
            MainGame.window.blit(self.getTextSuface('敌方坦克剩余数量:6'),(10,10))
            MainGame.my_tank.displayTank() #让坦克进行显示
            p.display.update()
    def endGame(self):
        print('谢谢使用,欢迎下次再来')
        exit()#这个函数的功能就是关闭窗口
    def getTextSuface(self,text):
        p.font.init()
        font = p.font.SysFont('kaiti',18)
        TextSurface=font.render(text,True,text_color)
        return TextSurface
    def getEvent(self):
        # 读取全部事件
        eventList = p.event.get()
        for event in eventList:
            #判断按下的是关闭还是键盘按键
            #当按键按下的是关闭时,退出
            if event.type == p.QUIT:
                self.endGame()
            #当按下的是键盘按键时
            if event.type == p.KEYDOWN:
                
                if event.key == p.K_LEFT:
                    #让坦克切换方向
                    MainGame.my_tank.direction = 'L'
                    #调用方法, 让坦克移动
                    MainGame.my_tank.move()

                    print('按下左键')
                if event.key == p.K_RIGHT:
                    MainGame.my_tank.direction ='R'
                    MainGame.my_tank.move()
                    print('按下右键')
                if event.key == p.K_UP:
                    MainGame.my_tank.direction ='U'
                    MainGame.my_tank.move()
                    print('按下上键')
                if event.key == p.K_DOWN:
                    MainGame.my_tank.direction ='D'
                    MainGame.my_tank.move()
                    print('按下下键')
class Tank():
    def __init__(self,left,top):
        self.images={'U':p.image.load('img\Pk01-U.jpg'),
                     'D':p.image.load('img\Pk01-D.jpg'),
                     'L':p.image.load('img\Pk01-L.jpg'),
                     'R':p.image.load('img\Pk01-R.jpg')}
        #方向
        self.direction='U' 
        #根据当前图片的方向获取图片 
        self.image=self.images[self.direction]
        #根据图片获取区域 
        self.rect = self.image.get_rect()
        #设置区域的left,top 
        self.rect.left=left
        self.rect.top=top
        #速度  决定移动的快慢
        self.speed = 10
    #移动
    def move(self):
        #判断坦克的方向移动
        #如果向左移动
        if self.direction == 'L':
            self.rect.left -= self.speed
        #如果向上移动
        if self.direction == 'U' :
            self.rect.top -= self.speed  
        #如果向下移动 
        if self.direction == 'D':
            self.rect.top += self.speed
        #r如果向右移动
        if self.direction == 'R':
            self.rect.left += self.speed
    def displayTank(self):
        self.image=self.images[self.direction]
        MainGame.window.blit(self.image,self.rect)


    
MainGame().startGame()
老师我在对坦克左右移动时,无法改变方向,只能朝上来左右移动,而当我按下下键时就可以使坦克朝下


Python全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 647楼
Python全系列/第二阶段:Python 深入与提高/模块 648楼
Python全系列/第二阶段:Python 深入与提高/Python项目环境管理 649楼

二进制文件读取.png

老师,这里为什么是报的语法错误?

Python全系列/第二阶段:Python 深入与提高/文件处理 651楼


image.png

Python全系列/第二阶段:Python 深入与提高/模块 652楼

屏幕截图(6).png

屏幕截图(7).png

a.aa.module_AA fun_AA
fun_AA()


()

():
    ()


导入fun_AA(),为什么会执行函数外的 print 呢

Python全系列/第二阶段:Python 深入与提高/模块 653楼
Python全系列/第二阶段:Python 深入与提高/文件处理 654楼
Python全系列/第二阶段:Python 深入与提高/文件处理 656楼
Python全系列/第二阶段:Python 深入与提高/文件处理 657楼

tank17.rar

tank16.rar

Q1:tank16里当坦克朝向上时,按空格发射子弹坦克会瞬移到窗口上方,其他方向不会

Q2:tank17里报错'Bullet' object has no attribute 'live',但是还看不出问题


Python全系列/第二阶段:Python 深入与提高/坦克大战 658楼
Python全系列/第二阶段:Python 深入与提高/坦克大战 659楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637