会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2343楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2344楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 2346楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 2349楼

我的代码报类型不对,请老师看下哪里不对

"""
新增功能:
    加载我方坦克
    pygame官方网址:www.pygame.org
"""

import pygame

SCREEN_WIDTH = 700
SCREEN_HEIGHT = 500
BG_COLOR = pygame.Color(0,0,0)
TEXT_COLOR = pygame.Color(255,0,0)


class MainGame():
    window = None
    my_tank = None

    def __init__(self):
        pass

    def startGame(self):
        # 加载主窗口
        pygame.display.init()

        # 设置窗口的大小及显示
        MainGame.window = pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])

        # 初始化我方坦克
        MainGame.my_tank = Tank(350, 250)

        # 设置窗口的标题
        pygame.display.set_caption("坦克大战1.03")

        while True:
            # 给窗口设置填充色
            MainGame.window.fill(BG_COLOR)

            # 获取事件
            self.getEvent()

            # 绘制文字
            MainGame.window.blit(self.getTextSuface("敌方坦克剩余数量%d"%6),(0,0))

            # 调用我方坦克的显示方法
            MainGame.my_tank.displayTank()

            # 更新画面
            pygame.display.update()

    def endGame(self):
        print("谢谢使用,欢迎再次使用!")
        exit()

    def getTextSuface(self,text):
        # 初始化字体模块
        pygame.font.init()

        #查看所有的字体
        # fontList = pygame.font.get_fonts()

        # 获取字体对象
        font = pygame.font.SysFont("kaiti",18)

        #绘制文字信息
        textSurface = font.render(text, True, TEXT_COLOR)
        return textSurface


    def getEvent(self):
        # 获取所有事件
        eventList = pygame.event.get()

        # 遍历事件
        for event in eventList:
            # 判断按下单键
            if event.type == pygame.QUIT:
                self.endGame()
            elif event.type == pygame.KEYDOWN:
                # 判断按下的键是哪个方向键
                if event.key == pygame.K_LEFT:
                    print("按下左键,坦克向左移动")
                elif event.key == pygame.K_RIGHT:
                    print("按下右键,坦克向右移动")
                elif event.key == pygame.K_UP:
                    print("按下上键,坦克向上移动")
                elif event.key == pygame.K_DOWN:
                    print("按下下键,坦克向下移动")


#坦克类
class Tank():
    def __init__(self, left, top):
        # 保存加载的图片
        self.images={"U": pygame.image.load("../img/p1tankU.gif"),
                     "D": pygame.image.load("../img/p1tankD.gif"),
                     "L": pygame.image.load("../img/p1tankL.gif"),
                     "R": pygame.image.load("../img/p1tankR.gif")}

        # 方向
        self.direction="U"

        # 根据当前方向获取图片
        self.image = self.images[self.direction]

        # 根据图片获取区域
        self.rect = self.image.get_rect()
        print(type(self.rect))

        # 设置区域的left和top
        self.rect.left = left
        self.rect.top = top



    def move(self):
        pass

    def shot(self):
        pass

    def displayTank(self):
        # 获取展示的对象
        self.image = self.images[self.direction]

        # 获取blit方法展示
        MainGame.window.blit(self.images, self.rect)
        
if __name__ == '__main__':
    MainGame().startGame()

报错如下:

<class 'pygame.Rect'>
Traceback (most recent call last):
  File "G:/workspy/tankGame/test/tank06.py", line 172, in <module>
    MainGame().startGame()
  File "G:/workspy/tankGame/test/tank06.py", line 46, in startGame
    MainGame.my_tank.displayTank()
  File "G:/workspy/tankGame/test/tank06.py", line 127, in displayTank
    MainGame.window.blit(self.images, self.rect)
TypeError: argument 1 must be pygame.Surface, not dict

Process finished with exit code 1


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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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