【发布时间】:2023-04-08 15:09:01
【问题描述】:
我刚刚在我的代码中添加了一个函数,该函数应该显示目录中的图像。它需要一个参数来指定它显示在哪个窗口中。当我尝试将它传递进来时,我收到以下错误:
Traceback (most recent call last):
File "Pygame.py", line 122, in <module>
Player.load()
File "Pygame.py", line 74, in load
screen.blit(self.path, (self.x, self.y))
TypeError: argument 1 must be pygame.Surface, not str
我的代码:
import pygame
#init the pygame and create a screen
pygame.init()
screen = pygame.display.set_mode((1080,720))
done = False
#colours
blue = (0,0,255)
red = (255,0,0)
green = (0,255,0)
black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)
#path to the background
bg_path = "Racing.png"
#path to the car image
car_path = "car.png"
#starts the game clock
clock = pygame.time.Clock()
#opening bg image
background_image = pygame.image.load(bg_path).convert()
#class for all of the objects on the screen
class shape():
def __init__(self, place, x, y):
self.place = place
self.x = x
self.y = y
class rectangle(shape):
def __init__(self, place, colour, x, y, length, width):
super().__init__(place,x, y)
self.colour = colour
self.length = length
self.width = width
def draw(self):
pygame.draw.rect(screen, self.colour, pygame.Rect(self.x, self.y,
self.length, self.width))
def move_up(self):
self.y = self.y - 10
def move_down(self):
self.y = self.y + 10
def move_right(self):
self.x = self.x + 10
def move_left(self):
self.x = self.x - 10
class player(shape):
def __init__(self, place, x, y, length, width, path):
super().__init__(place,x, y)
self.length = length
self.width = width
self.path = path
def load(self):
screen.blit(self.path, (self.x, self.y))
Rectangle = rectangle(screen, yellow, 540, 660, 60, 60)
Player = player(screen, 540, 600, 60, 60, car_path)
Player.load()
这不是所有的代码,但其余的与问题无关(我认为)。请告诉我是否需要更多代码。
【问题讨论】:
-
欢迎来到 StackOverflow。见minimal, reproducible example。在您发布 MRE 代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您指定的问题。展示你做过的事情,然后将其倒退到问题点。删除多余的代码;你已经为一个 10 行问题发布了大约 75 行代码。
-
car_path是一个字符串,所以path也是一个字符串。您必须加载图像pygame.image.load(car_path)
标签: python python-3.x types pygame typeerror