参数 1 必须是 pygame.surface,而不是 list

2023-12-25

我正在编写一个小型 Python 游戏,但我的一些代码似乎不起作用。看一看:

import pygame
import sys
import pygame.sprite as sprite
import time
pygame.init()

pygame.display.set_caption("Uni Mario")


m_x = 100
m_y = 350
width = 40
height = 60
vel = 5

left=False
right=False
walk_count = 0
walkRight = []
walkLeft = []


myfont = pygame.font.SysFont("monospace", 25)
screen_over = pygame.font.SysFont("monospace", 100)
hitcount = 0
score=0

clock = pygame.time.Clock()
FPS = 30

background_image = pygame.image.load('Sprites/bg2.png')
background_size = background_image.get_size()
background_rect = background_image.get_rect()
sw = 837
sh = 464
win = pygame.display.set_mode((sw, sh))
w,h = background_size

t_x = sw
t_y = 350
g_x = sw-100

bg_x1 = 0
bg_y1 = 0

bg_x2 = w
bg_y2 = 0

d2 = 9999

def load_img(file_name): # loads the image, makes the pure white background transparent
    img = pygame.image.load(file_name).convert()
    img.set_colorkey((255,255,255))

    return img

for i in range(1,7):
    walkLeft.append( load_img("Sprites/L" + str(i) + ".png" ) ) #loads in lists of images
    walkRight.append( load_img("Sprites/R" + str(i) + ".png") ) 

player_image = walkRight[0]

turtle_image = [pygame.image.load('Sprites/Goomba1.png'), pygame.image.load('Sprites/Goomba2.png'), pygame.image.load('Sprites/Goomba3.png')]
#turtle_small = pygame.transform.scale(turtle_image, (100, 60))

pygame.mixer.music.load('Music/Bros.mp3')
pygame.mixer.music.play(-1)

isJump = False
jumpCount = 10

left_idx=0
right_idx=0

#images = []
#images.append(pygame.image.load('Sprites/Goomba1.png'))
#images.append(pygame.image.load('Sprites/Goomba2.png'))
#images.append(pygame.image.load('Sprites/Goomba3.png'))

#index = 0
# 
#image = images[index]

#def update(images):
 #   index += 1

#if index >= len(images):
#    index = 0

#image = images[self.index]

run = True #main loop
while run:
    clock.tick(FPS)

    pygame.time.delay(50)

    win.blit(background_image,(sw, sh)) #makes a scrolling background 
    pygame.display.update()

    bg_x2 -= 5
    bg_x1 -= 5
    if bg_x1 < sw - 2*w:
       bg_x1 = sw
    if bg_x2 < sw - 2*w:
          bg_x2 = sw

    t_x -=20
    if t_x < 0:
        t_x = sw

    #g_x -= 20
    #if g_x < 0:
     #   g_x = sw+500



    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and m_x > vel: 
        m_x -= vel
        if not isJump:
            player_image = walkLeft[left_idx]
            left_idx += 1
            if left_idx >= len(walkLeft):
                left_idx=0

    if keys[pygame.K_RIGHT] and m_x < sw - width - vel:
        m_x += vel
        if not isJump:
            player_image = walkRight[right_idx]
            right_idx += 1
            if right_idx >= len(walkRight):
                right_idx=0

    if not(isJump):      
        if keys[pygame.K_UP]:
            isJump = True
        if keys[pygame.K_SPACE]:
            isJump = True

    else:
        if jumpCount >= -10:
            neg = 1
            if jumpCount < 0:
                neg = -1
            m_y -= (jumpCount ** 2)* 0.25 * neg
            jumpCount -= 1

        else:
            isJump = False
            jumpCount = 10

    if bg_x1 > -w:        
        win.blit(background_image,(bg_x1,bg_y1))
    if bg_x2 > -w:
        win.blit(background_image,(bg_x2,bg_y2))
    win.blit(player_image, (m_x,m_y))

    win.blit(turtle_image, (t_x, t_y)) #22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222



    label = myfont.render("Hit Count = "+ str(hitcount), 1, (0, 0, 0))
    win.blit(label, ((sw-200), 420))

    label3 = myfont.render("If your hit count gets",1, (0, 0, 0))
    win.blit(label3, (270, 10))

    label4= myfont.render("to 3, you lose.", 1, (0 , 0, 0))
    win.blit(label4, (270, 60))

    label5 = myfont.render("Score: "+ str(score), 1, (0, 0, 0))
    win.blit(label5, ((sw-180), 50))


    pygame.display.update() 


    d2 = (t_x - m_x)**2 + (t_y - m_y)**2 #represents the distance between the two character
    if d2 < 142: #keep it at 142, it seems to be a good distance for the hitcount
        hitcount += 1
    else:
        score += 1/10
    if hitcount >= 3:
        label2 = screen_over.render("Game Over", 1, (255, 0, 0))
        win.blit(label2, (230, 200))
        pygame.display.update()
        time.sleep(2)
        run= False
    if score >= 100:
        label6 = screen_over.render("YOU WIN", 1, (255, 0, 0))
        win.blit(label6, (230,200))
        pygame.display.update()
        time.sleep(5)
        run=False

pygame.quit()

是的......所以错误如下:

Traceback (most recent call last):
  File "C:\Users\aryaat\Desktop\SPython Gaming Project\Uni Mario(A).py", line 163, in <module>
    win.blit(turtle_image, (t_x, t_y)) #22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
TypeError: argument 1 must be pygame.Surface, not list

turtle_image不是一个pygame.Surface对象,但它是一个列表pygame.Surface对象:

turtle_image = [pygame.image.load('Sprites/Goomba1.png'), pygame.image.load('Sprites/Goomba2.png'), pygame.image.load('Sprites/Goomba3.png')] 

您必须使用索引运算符从列表中选择一个对象(例如turtle_image[0])您想要将其“blit”到窗口表面:

win.blit(turtle_image[0], (t_x, t_y))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

参数 1 必须是 pygame.surface,而不是 list 的相关文章

  • Gunicorn 工作人员无论如何都会超时

    我正在尝试通过gunicorn运行一个简单的烧瓶应用程序 但是无论我做什么 我的工作人员都会超时 无论是否有针对应用程序的活动 工作人员在我设置任何内容后总是会超时timeout值到 是什么导致它们超时 当我发出请求时 请求成功通过 但工作
  • VSCode Settings.json 丢失

    我正在遵循教程 并尝试将 vscode 指向我为 Scrapy 设置的虚拟工作区 但是当我在 VSCode 中打开设置时 工作区设置 选项卡不在 用户设置 选项卡旁边 我还尝试通过以下方式手动转到文件 APPDATA Code User s
  • Django Rest Framework 是否有第三方应用程序来自动生成 swagger.yaml 文件?

    我有大量的 API 端点编写在django rest framework并且不断增加和更新 如何创建和维护最新的 API 文档 我当前的版本是 Create swagger yaml文件并以某种方式在每次端点更改时自动生成 然后使用此文件作
  • 嵌套列表的重叠会产生不必要的间隙

    我有一个包含三个列表的嵌套 这些列表由 for 循环填充 并且填充由 if 条件控制 第一次迭代后 它可能类似于以下示例 a 1 2 0 0 0 0 0 0 4 5 0 0 0 0 0 0 6 7 根据条件 它们不重叠 在第二次迭代之后 新
  • MongoEngine 查询具有以列表中指定的前缀开头的属性的对象的列表

    我需要在 Mongo 数据库中查询具有以列表中任何前缀开头的特定属性的元素 现在我有一段这样的代码 query mymodel terms term in query terms 并且这会匹配在列表 term 上有一个项目的对象 该列表中的
  • 矩形函数的数值傅里叶变换

    本文的目的是通过一个众所周知的分析傅里叶变换示例来正确理解 Python 或 Matlab 上的数值傅里叶变换 为此 我选择矩形函数 这里报告了它的解析表达式及其傅立叶变换https en wikipedia org wiki Rectan
  • 打印包含字符串和其他 2 个变量的变量

    var a 8 var b 3 var c hello my name is var a and var b bye print var c 当我运行程序时 var c 会像这样打印出来 hello my name is 8 and 3 b
  • Python 3:将字符串转换为变量[重复]

    这个问题在这里已经有答案了 我正在从 txt 文件读取文本 并且需要使用我读取的数据之一作为类实例的变量 class Sports def init self players 0 location name self players pla
  • 使用 python/numpy 重塑数组

    我想重塑以下数组 gt gt gt test array 11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44 为了得到 gt gt gt test2 array 11 12 21 22 13 14
  • 导入错误:没有名为flask.ext.login的模块

    我的flask login 模块有问题 我已经成功安装了flask login模块 另外 从命令提示符我可以轻松运行此脚本 不会出现错误 Python 2 7 r27 82525 Jul 4 2010 07 43 08 MSC v 1500
  • Python - 如何确定解析的 XML 元素的层次结构级别?

    我正在尝试使用 Python 解析 XML 文件中具有特定标记的元素并生成输出 excel 文档 该文档将包含元素并保留其层次结构 我的问题是我无法弄清楚每个元素 解析器在其上迭代 的嵌套深度 XML 示例摘录 3 个元素 它们可以任意嵌套
  • 如何使用 Python 3 检查目录是否包含文件

    我到处寻找这个答案但找不到 我正在尝试编写一个脚本来搜索特定的子文件夹 然后检查它是否包含任何文件 如果包含 则写出该文件夹的路径 我已经弄清楚了子文件夹搜索部分 但检查文件却难倒了我 我发现了有关如何检查文件夹是否为空的多个建议 并且我尝
  • 无效的选择器:使用 Selenium 时不允许出现复合类名错误

    我正在尝试通过 Web Whatsapp 打印聊天中的一条消息 我可以通过 控制台 选项卡中的 Javascript 来完成此操作 我就是这样做的 recived msg document getElementsByClassName XE
  • 带有 LSTM 的 GridSearchCV/RandomizedSearchCV

    我一直在尝试通过 RandomizedSearchCV 调整 LSTM 的超参数 我的代码如下 X train X train reshape X train shape 0 1 X train shape 1 X test X test
  • 为什么 csv.DictReader 给我一个无属性错误?

    我的 CSV 文件是 200 Service 我放入解释器的代码是 snav csv DictReader open screennavigation csv delimiter print snav fieldnames 200 for
  • python 中的“槽包装器”是什么?

    object dict 和其他地方的隐藏方法设置为这样的
  • 重新分配唯一值 - pandas DataFrame

    我在尝试着assign unique值在pandas df给特定的个人 For the df below Area and Place 会一起弥补unique不同的价值观jobs 这些值将分配给个人 总体目标是使用尽可能少的个人 诀窍在于这
  • 等待子进程使用 os.system

    我用了很多os system在 for 循环内调用创建后台进程 如何等待所有后台进程结束 os wait告诉我没有子进程 ps 我使用的是Solaris 这是我的代码 usr bin python import subprocess imp
  • JSON:TypeError:Decimal('34.3')不是JSON可序列化的[重复]

    这个问题在这里已经有答案了 我正在运行一个 SQL 查询 它返回一个小数列表 当我尝试将其转换为 JSON 时 出现类型错误 查询 res db execute SELECT CAST SUM r SalesVolume 1000 0 AS
  • NLTK:查找单词大小为 2k 的上下文

    我有一个语料库 我有一个词 对于语料库中该单词的每次出现 我想获取一个包含该单词之前的 k 个单词和该单词之后的 k 个单词的列表 我在算法上做得很好 见下文 但我想知道 NLTK 是否提供了一些我错过的功能来满足我的需求 def size

随机推荐