类型错误:参数 1 必须是 pygame.Surface,而不是 str 我该如何解决? [英] TypeError: argument 1 must be pygame.Surface, not str How do I fix?

查看:125
本文介绍了类型错误:参数 1 必须是 pygame.Surface,而不是 str 我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 Youtbe 视频中编码了这个,他似乎做得很好,但是当我尝试时,我在底部收到错误消息,我很困惑,我需要帮助.

I coded this from a youtbe video and he seemed to do it fine but when I tried I got the error message at the bottom I am so confused and I need help please.

#!/usr/bin/env python

import pygame, sys

from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((370, 572), 0, 32)

backgroundfile = "back.png"
mousefile = "mouse.png"

back = pygame.image.load(backgroundfile).convert()
mouse = pygame.image.load(mousefile).convert_alpha()

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    #Now we have initialized everything lets start with the main part

    screen.blit("back.png", (0,0))

    pygame.display.flip()

当我运行程序时出现错误:

When I run the program I get the error:

Traceback (most recent call last):
  File "Tutorial 5 First game.py", line 26, in <module>
  screen.blit("back.png", (0,0))
TypeError: argument 1 must be pygame.Surface, not str 

我不确定这是什么意思或如何解决

I'm not sure what this means or how to fix

这就是修复

你有两个问题.你的第一个是你在周围加上引号back.png,把它变成一个字符串 (str) 而不是一个表面(pygame.Surface).你的第二个是你为第二个放了一个元组参数而不是矩形 (pygame.Rect). 要修复第一个,只需输入背景文件(您之前将表面保存为)而不是"background.png".要修复第二个,请使用 backgroudfile.get_rect() 来获取背景的矩形.您的线路应该是:

You have two problems. Your first is that you put quotes around back.png, making it into a string (str) instead of a surface (pygame.Surface). Your second is that you put a tuple for the second argument instead of a rect (pygame.Rect). To fix the first, simply put backgroundfile (what you previously saved the surface as) instead of "background.png". To fix the second, use backgroudfile.get_rect() to get the background's rect. Your line should be:

screen.blit(backgroundfile, backgroundfile.get_rect() 这本身将不起作用,因为您之前没有将背景文件保存为表面对象.而不是

screen.blit(backgroundfile, backgroundfile.get_rect() This by itself will not work because you didn't previously save backgroundfile as a surface object. Instead of

backgroundfile = "back.png" put

backgroundfile = pygame.image.load("back.png") 这将返回一个表面,如果 "back.png" 保存为同一文件夹中的文件.做与其他加载的图像相同的事情.做所有这些事情和你的程序应该可以工作.

backgroundfile = pygame.image.load("back.png") This will return a surface, if "back.png" is saved as a file in the same folder. Do the same thing with the other loaded image. Do all these things and your program should work.

推荐答案

您有两个问题.首先是在 back.png 周围加上引号,使其成为字符串 (str) 而不是表面 (pygame.Surface).你的第二个是你为第二个参数放置了一个元组,而不是一个矩形(pygame.Rect).要解决第一个问题,只需放置 backgroundfile(您之前保存表面的内容)而不是 "background.png".要修复第二个,请使用 backgroudfile.get_rect() 获取背景的矩形.您的线路应该是:

You have two problems. Your first is that you put quotes around back.png, making it into a string (str) instead of a surface (pygame.Surface). Your second is that you put a tuple for the second argument instead of a rect (pygame.Rect). To fix the first, simply put backgroundfile (what you previously saved the surface as) instead of "background.png". To fix the second, use backgroudfile.get_rect() to get the background's rect. Your line should be:

screen.blit(backgroundfile, backgroundfile.get_rect()

这本身不起作用,因为您之前没有将背景文件保存为表面对象.而不是

This by itself will not work because you didn't previously save backgroundfile as a surface object. Instead of

backgroundfile = "back.png"

backgroundfile = pygame.image.load("back.png")

如果back.png"作为文件保存在同一文件夹中,这将返回一个表面.对另一个加载的图像做同样的事情.做完所有这些事情,你的程序就应该可以运行了.

This will return a surface, if "back.png" is saved as a file in the same folder. Do the same thing with the other loaded image. Do all these things and your program should work.

这篇关于类型错误:参数 1 必须是 pygame.Surface,而不是 str 我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆