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

pygametimerandom
_display = pygame.display
COLOR_BLACK = pygame.Color()
COLOR_RED = pygame.Color()
version = MainGame():
    window = SCREEN_HEIGHT = SCREEN_WIDTH = TANK_P1 = EnemyTank_list = []
    EnemTank_count = Bullet_list = []
    Enemy_bullet_list = []
    ():
        ():
        _display.init()
        MainGame.window = _display.set_mode([MainGame.SCREEN_WIDTHMainGame.SCREEN_HEIGHT])
        MainGame.TANK_P1 = Tank()
        .creatEnemyTank()
        _display.set_caption(+version)
        :
            MainGame.window.fill(COLOR_BLACK)
            .getEvent()
            MainGame.window.blit(.getTextSurface(%(MainGame.EnemyTank_list))())
            MainGame.TANK_P1.displayTank()
            .blitEnemyTank()
            MainGame.TANK_P1 MainGame.TANK_P1.stop:
                MainGame.TANK_P1.move()
            .blitBullet()
            .blitEnemyBullet()
            time.sleep()
            _display.update()
    ():

        top = speed = random.randint()
        i (MainGame.EnemTank_count):
            left = random.randint()
            eTank = EnemyTank(left*topspeed)
            MainGame.EnemyTank_list.append(eTank)
    ():
        eTank MainGame.EnemyTank_list:
            eTank.displayTank()
            eTank.randMove()
            eBullet = eTank.shot()
            MainGame.Enemy_bullet_list.append(eBullet)
    ():
        bullet MainGame.Bullet_list:
            bullet.live:
                bullet.displayBullet()
                bullet.bulletMove()
            :
                MainGame.Bullet_list.remove(bullet)
    ():
        eBullet MainGame.Enemy_bullet_list:
            eBullet.live:
                eBullet.displayBullet()
                eBullet.bulletMove()
            :
                MainGame.Bullet_list.remove(eBullet)

    ():
        eventList = pygame.event.get()
        event eventList:
            event.type == pygame.QUIT:
                .endGame()
            event.type == pygame.KEYDOWN:
                event.key == pygame.K_LEFT:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_RIGHT:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_UP:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_DOWN:
                    ()
                    MainGame.TANK_P1.direction = MainGame.TANK_P1.stop = event.key == pygame.K_SPACE:
                    ()
                    (MainGame.Bullet_list) < :
                        m = Bullet(MainGame.TANK_P1)
                        MainGame.Bullet_list.append(m)
                    :
                        ()
                    (%(MainGame.Bullet_list))
            event.type == pygame.KEYUP:
                event.key == pygame.K_LEFT event.key == pygame.K_RIGHT event.key == pygame.K_UP event.key == pygame.K_DOWN:
                    MainGame.TANK_P1.stop = (text):
        pygame.font.init()
        font = pygame.font.SysFont()
        textSurface = font.render(textCOLOR_RED)
        textSurface
    ():
        ()
        ()
Tank():
    (lefttop):
        .images = {
            :pygame.image.load():pygame.image.load():pygame.image.load():pygame.image.load()
        }
        .direction = .image = .images[.direction]
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top
        .speed = .stop = ():
        .direction == :
            .rect.left > :
                .rect.left -= .speed
        .direction == :
            .rect.left + .rect.height < MainGame.SCREEN_WIDTH:
                .rect.left += .speed
        .direction == :
            .rect.top > :
                .rect.top -= .speed
        .direction == :
            .rect.top + .rect.height < MainGame.SCREEN_HEIGHT:
                .rect.top += .speed
    ():
        Bullet()
    ():
        .image = .images[.direction]
        MainGame.window.blit(.image.rect)
MyTank(Tank):
    ():
        EnemyTank(Tank):
    (lefttopspeed):
        .images = {
            : pygame.image.load(): pygame.image.load(): pygame.image.load(): pygame.image.load()
        }
        .direction = .randDirection()
        .image = .images[.direction]
        .rect = .image.get_rect()
        .rect.left = left
        .rect.top = top
        .speed = speed
        .stop = .step = ():
        num = random.randint()
        num == :
            num == :
            num == :
            num == :
            ():
        .step <= :
            .direction = .randDirection()
            .step = :
            .move()
            .step -= Bullet():
    (tank):
        .image = pygame.image.load()
        .direction = tank.direction
        .rect = .image.get_rect()
        .direction == :
            .rect.left = tank.rect.left + tank.rect.width/- .rect.width/.rect.top = tank.rect.top - .rect.height
        .direction == :
            .rect.left = tank.rect.left + tank.rect.width / - .rect.width / .rect.top = tank.rect.top + tank.rect.height
        .direction == :
            .rect.left = tank.rect.left - .rect.width / - .rect.width / .rect.top = tank.rect.top + tank.rect.width / - .rect.width / .direction == :
            .rect.left = tank.rect.left + tank.rect.width
            .rect.top = tank.rect.top + tank.rect.width / - .rect.width / .speed = .live = ():
        .direction == :
            .rect.top > :
                .rect.top -= .speed
            :
                .live = .direction == :
            .rect.top < MainGame.SCREEN_HEIGHT - .rect.height:
                .rect.top += .speed
            :
                .live = .direction == :
            .rect.left > :
                .rect.left -= .speed
            :
                .live = .direction == :
            .rect.left < MainGame.SCREEN_WIDTH - .rect.width:
                .rect.left += .speed
            :
                .live = ():
        MainGame.window.blit(.image.rect)
Explode():
    ():
        ():
        Wall():
    ():
        ():
        Music():
    ():
        ():
        MainGame().startGame()

检查了好几次,每次到了8分38秒此时效果时候就弹出下面的提示,该怎么处理呢?

image.png

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2823楼

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
        print("坦克向左调头,移动")
        #修改坦克方向
        MainGame.TANK_P1.direction = 'L'
        #同时改变坦克开关状态
        MainGame.TANK_P1.stop = False
        #完成移动操作(调用坦克的移动方法)
        #MainGame.TANK_P1.move()      #在主循环中持续调用
    elif event.key == pygame.K_RIGHT:
        print("坦克向右调头,移动")
        #修改坦克方向
        MainGame.TANK_P1.direction = 'R'
        # 同时改变坦克开关状态
        MainGame.TANK_P1.stop = False
        #完成移动操作(调用坦克的移动方法)
        #MainGame.TANK_P1.move()
    elif event.key == pygame.K_UP:
        print("坦克向上调头,移动")
        #修改坦克方向
        MainGame.TANK_P1.direction = 'U'
        # 同时改变坦克开关状态
        MainGame.TANK_P1.stop = False
        #完成移动操作(调用坦克的移动方法)
        #MainGame.TANK_P1.move()
    elif event.key == pygame.K_DOWN:
        print("坦克向下调头,移动")
        #修改坦克方向
        MainGame.TANK_P1.direction = 'D'
        # 同时改变坦克开关状态
        MainGame.TANK_P1.stop = False
        #完成移动操作(调用坦克的移动方法)
        #MainGame.TANK_P1.move()
    elif event.key == pygame.K_SPACE:
        print("发射子弹")

老师,为什么这个条件语句,在我按下方向键后,再按空格键,能同时进行两个条件判断呢,循环不应该一直识别我按下的方向键吗?我想知道这个过程是如果进行的,是先判断射击还是前进?

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2825楼

3tank24.rar

上面提问报错的,谢谢老师帮忙看下

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2826楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2830楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2833楼

import pygame,time
COLOR_BLACK = pygame.Color(0,0,0)
COLOR_RED = pygame.Color(255,0,0)
version = "v1.09"

class MainGame():
    window = None
    SCREEN_WIDTH = 800
    SCREEN_HEIGHT = 500
    TANK_P1 = None

    def __init__(self):
        pass

    def startGame(self):
        pygame.display.init()
        MainGame.window = pygame.display.set_mode([MainGame.SCREEN_WIDTH,MainGame.SCREEN_HEIGHT])
        MainGame.TANK_P1 = Tank(400,400)
        pygame.display.set_caption("坦克大战"+version)
        #self.getTextSurface("haha")
        while True:
            MainGame.window.fill(COLOR_BLACK)
            self.getEvent()
            MainGame.window.blit(self.getTextSurface("剩余敌方坦克{0}辆".format(5)), (5,5))    #将新建好的小Surface放入到大的Surface中
            #MainGame.window.blit(Tank.displayTank("U"), (250,500))
            #MainGame.window.blit(pygame.image.load("p1tankU.gif"),(50,50))
            MainGame.TANK_P1.displayTank()
            #MainGame.TANK_P1.tankselber()
            if MainGame.TANK_P1 and not MainGame.TANK_P1.stop:
                MainGame.TANK_P1.move()    #使得坦克可以持续移动,但是坦克只有顶在边界才能停止
            time.sleep(0.02)
            pygame.display.update()
            #MainGame.TANK_P1.tankselber()

    def getEvent(self):
        #1.获取所有事件
        #2.对事件进行判断处理(1.点击关闭按钮,2.按下键盘上的某个按钮)
        eventList = pygame.event.get()
        for event in eventList:
            if event.type == pygame.QUIT:
                self.endGame()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    #Tank.direction = "L"
                    MainGame.TANK_P1.direction = "L"
                    MainGame.TANK_P1.stop = False
                    #MainGame.TANK_P1.move()
                    print("坦克向左调头移动")
                elif event.key == pygame.K_RIGHT:
                    #Tank.direction = "R"
                    MainGame.TANK_P1.direction = "R"
                    MainGame.TANK_P1.stop = False
                    MainGame.TANK_P1.displayTank()
                    #MainGame.TANK_P1.move()
                    print("坦克向右调头移动")
                elif event.key == pygame.K_UP:
                    MainGame.TANK_P1.direction = "U"
                    MainGame.TANK_P1.stop = False
                    #Tank.direction = "U"
                    MainGame.TANK_P1.displayTank()
                    #MainGame.TANK_P1.move()
                    print("坦克向上调头移动")
                elif event.key == pygame.K_DOWN:
                    MainGame.TANK_P1.direction = "D"
                    MainGame.TANK_P1.stop = False
                    #Tank.direction = "D"
                    MainGame.TANK_P1.displayTank()
                    #MainGame.TANK_P1.move()
                    print("坦克向下调头移动")
                elif event.key == pygame.K_SPACE:
                    print("发射子弹")

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or pygame.K_RIGHT or pygame.K_DOWN or pygame.K_LEFT:
                    MainGame.TANK_P1.stop = True



    def getTextSurface(self,text):
        pygame.font.init()
        font = pygame.font.SysFont('kaiti',18)    #对字体格式进行了设置
        #fontlist = pygame.font.get_fonts()
        textSurface = font.render(text,True,COLOR_RED)    #建立了一个新的小Surface,里面有红色的Text(draw text on a new Surface)
        return textSurface

        #print(fontlist)





    def endGame(self):
        print("谢谢使用!")
        exit()


class Tank():
    def __init__(self,left,top):
        self.images = {
            "U":pygame.image.load('p1tankU.gif'),
            "D":pygame.image.load('p1tankD.gif'),
            "L":pygame.image.load('p1tankL.gif'),
            "R":pygame.image.load('p1tankR.gif')
        }

        self.direction = "U"
        self.image = self.images[self.direction]
        self.rect = self.image.get_rect()
        self.rect.left = left
        self.rect.top = top
        self.speed = 5
        self.stop = True



    def move(self):

        if MainGame.TANK_P1.direction == "U":
            if self.rect.top > 0:
                self.rect.top = self.rect.top - self.speed
        elif MainGame.TANK_P1.direction == "D":
            if self.rect.top < 440:
                self.rect.top = self.rect.top + self.speed
        elif MainGame.TANK_P1.direction == "L":
            if self.rect.left > 0:
                self.rect.left = self.rect.left - self.speed
        elif MainGame.TANK_P1.direction == "R":
            if self.rect.left < 740:
                self.rect.left = self.rect.left + self.speed
                #print(self.image.get_size())



    def shot(self):
        pass

    def displayTank(self):
        self.image = self.images[self.direction]
        MainGame.window.blit(self.image,self.rect)
    def tankselber(self):
        attr = dir()
        print(attr)
class MyTank(Tank):
    def __init__(self):
        pass
class EnemyTank(Tank):
    def __init__(self):
        pass
class Bullet():
    def __init__(self):
        pass

    def move(self):
        pass

    def displayBullet(self):
        pass
class Explode():
    def __init__(self):
        pass

    def displayExplode(self):
        pass
class Wall():
    def __init__(self):
        pass

    def displayWall(self):
        pass
class Music():
    def __init__(self):
        pass

    def play(self):
        pass
MainGame().startGame()

老师您好,我自己试了一下,代码和视频里面一致,但是我的坦克移动的时候一旦摁下空格键就会停下,请您帮我看一下是咋回事,谢谢您!

Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2834楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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