简体   繁体   English

TypeError:参数1必须是pygame.Surface,而不是str [使用按钮的Pygame]

[英]TypeError: argument 1 must be pygame.Surface, not str [Pygame using buttons]

Hello I'm creating a game using pygame and I had some problems. 您好,我正在使用pygame创建游戏,但遇到了一些问题。 I create some buttons (using images)and depending in some actions the Image of this buttons will change.I will show you the the part of the code where there is the Error. 我创建了一些按钮(使用图像),并根据某些操作改变了该按钮的图像,我将向您展示代码中存在错误的部分。 And sorry, but I'm Spanish, and my English is bad. 抱歉,我是西班牙人,我的英语不好。 This is the code: 这是代码:

import pygame
from pygame.locals import *
import time
pygame.init()

display_weight = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_weight, display_height), pygame.RESIZABLE)
pygame.display.set_caption("I NEED HELP :d")
clock = pygame.time.Clock()

#BUTTONS IMAGE
default_ap = pygame.image.load('button_menu_ap.png')
default_ip = pygame.image.load('button_menu_ip.png')
chewbacca_ip = pygame.image.load('button_select_GalaxyWars_chewbacca_ip.png')
chewbacca_ap = pygame.image.load('button_select_GalaxyWars_chewbacca_ap.png')

def button_img(x,y,w,h,ip,ap,action=None,especific1=None,especific2=None,especific3=None,especific4=None,especific5=None,especific6=None\
               ,especific7=None,especific8=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse [1] > y:
        gameDisplay.blit(ap, (x,y))
        if click [0] == 1 and action != None:
            if especific1 != None:
                if especific2 != None:
                    if especific2 != None and especific3 != None and especific4 != None and especific5 != None and especific6 != None \
                       and especific7 != None and especific8 != None:
                        action(especific1, especific2, especific3, especific4, especific5, especific6, especific7, especific8)
                    else:
                        action(especific1, especific2)
                else:
                    action(especific1)
            else:
                action()
    else:
        gameDisplay.blit(ip, (x,y))

    pygame.display.update()

def select_p1(nombre):
    nombre_ip = (nombre + '_ip')
    nombre_ap = (nombre + '_ap')
    return button_img(display_weight/2-300,display_height/2-250,100,100,nombre_ip,nombre_ap,select_categoria,1,nombre)

    pygame.display.update()

def select_categoria(num):
    global gameDisplay, display_weight,display_height
    select_categoria = True
    gameDisplay.fill((255,255,255))
    while select_categoria:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                quit()
            # Este es el código que redimensiona la ventana
            if event.type == VIDEORESIZE:
                # Recrea la ventana con el nuevo tamaño
                display_weight = event.w
                display_height = event.h
                gameDisplay = pygame.display.set_mode((display_weight, display_height),
                                                  pygame.RESIZABLE)

        button_img(display_weight/2-125,display_weight/2-125,250,60,button_select_categoria_ip,button_select_categoria_ap, select_GalaxyWars, num)
        pygame.display.update()

        clock.tick(15)

    clock.tick(15)
select_p1 ('default')

Then the Error is that one: 那么错误就是那个:

Traceback (most recent call last):
  File "C:/Users/win/Desktop/MARCOS/PYTHON/PROYECTO HGA/HELP.py", line 73, 
in <module>
select_p1 ('default')
  File "C:/Users/win/Desktop/MARCOS/PYTHON/PROYECTO HGA/HELP.py", line 46, 
in select_p1
    return button_img(display_weight/2-300,display_height/2-
250,100,100,nombre_ip,nombre_ap,select_categoria,1,nombre)
  File "C:/Users/win/Desktop/MARCOS/PYTHON/PROYECTO HGA/HELP.py", line 39, 
in button_img
    gameDisplay.blit(ip, (x,y))
TypeError: argument 1 must be pygame.Surface, not str

For Example in the last line of the code: 例如,在代码的最后一行:

select_p1 ('default')

If I put 'default', the images be the ones call default_ap and default_ip, those ones: 如果我设置“默认”,则这些图像分别是称为default_ap和default_ip的图像:

default_ap = pygame.image.load('button_menu_ap.png')
default_ip = pygame.image.load('button_menu_ip.png')

And if I put chewbacca, the images will be the ones call chewbacca_ap and chewbacca_ip. 如果我放入chewbacca,则图像将分别称为chewbacca_ap和chewbacca_ip。

Please, is someone knows a way to solve my problem, please tell me. 拜托,是有人知道解决我的问题的方法,请告诉我。

The error is self explanatory: 该错误是不言自明的:

    gameDisplay.blit(ip, (x,y))
TypeError: argument 1 must be pygame.Surface, not str

The object ip should be a pygame.Surface , instead it is an str . ip对象应该是pygame.Surface ,而是str Perhaps you want to create a dictionary: 也许您想创建一个字典:

button_image_dict = {'default_ip': default_ip,
                     'default_ap': default_ap,
                     'chewbacca_ip': chewbacca_ip,
                     'chewbacca_ap': chewbacca_ap}

And then use this dictionary to find the object from the string: 然后使用此字典从字符串中查找对象:

gameDisplay.blit(button_image_dict[ip], (x,y))

I have a dictionary of characters: cheracters = {'default':0,'chewbacca':1,'Stormsoldier':2} 我有一个字符字典:cheracters = {'默认':0,'chewbacca':1,'Stormsoldier':2}

First, I think that should be a list. 首先,我认为这应该是清单。 All you're storing is item positions. 您只存储项目位置。

If I understand what you're trying to do, you want a dictionary for the images 如果我了解您要执行的操作,则需要一个词典来存储图像

For example 例如

ap = {
    'default' : 'button_menu_ap.png', 
    'chewbacca' : 'button_select_GalaxyWars_chewbacca_ap.png'
} 

nombre = 'chewbacca'
chewbacca_ap = pygame.image.load(ap[nombre])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM