You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pygame报错TypeError:参数3必须是pygame.Surface而非int,求修复

修复Pygame中的TypeError错误

报错TypeError: argument 3 must be pygame.Surface, not int的根源是**pygame.transform.scale()**函数的参数使用错误。该函数的第二个参数要求是一个包含宽和高的元组(width, height),你在代码第15行把宽度和高度作为两个独立的参数传递,导致函数误将高度识别为第三个参数(原本第三个参数是可选的Surface对象),从而触发类型错误。

修复步骤

  • 修改Button类__init__方法中pygame.transform.scale的调用,将计算后的宽高打包成元组传入

修复后的完整代码

import pygame
pygame.init()
screen = pygame.display.set_mode((3840,2160))
running = True
mouse = pygame.mouse.get_pos()

pygame.display.set_caption("GermanBall")
bg = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\Tan.jpg")
icon = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\box.png")
button1 = pygame.image.load(r"C:\Users\tomarj\OneDrive - Tata Advanced Systems Limited\Desktop\War Crime\shirt.png").convert_alpha()

class Button():
    def __init__(self,x,y,image, scale):
        width = image.get_width()
        height = image.get_height()
        # 修复:将宽高打包为元组传递给scale函数
        self.image = pygame.transform.scale(image, (int(width * scale ), int(height * scale)))
        self.rect = self.image.get_rect()
        self.rect.topleft = (x,y)

    def draw(self):
        screen.blit(self.image,(self.rect.x,self.rect.y)) 

start = Button(1200,300,button1,0.5)

pygame.display.set_icon(icon)
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.blit(bg,(0,0))
    start.draw()
    pygame.display.update()

内容的提问来源于stack exchange,提问作者vivaan tomar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 06:01:45