您好我使用pygame创建游戏,但我遇到了一些问题。我创建了一些按钮(使用图像),并且根据某些操作,此按钮的图像将会改变。我将向您显示出现错误的代码部分。 抱歉,我是西班牙人,我的英语很差。这是代码:
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')
然后错误就是那个:
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
例如,代码的最后一行:
select_p1 ('default')
如果我将&#39;默认&#39;,那些图片就是那些调用default_ap和default_ip的图片:
default_ap = pygame.image.load('button_menu_ap.png')
default_ip = pygame.image.load('button_menu_ip.png')
如果我放了chewbacca,那些图像将会调用chewbacca_ap和chewbacca_ip。
拜托,有人知道解决问题的方法,请告诉我。
答案 0 :(得分:1)
错误是不言自明的:
gameDisplay.blit(ip, (x,y))
TypeError: argument 1 must be pygame.Surface, not str
对象ip
应该是pygame.Surface
,而不是str
。也许你想创建一个字典:
button_image_dict = {'default_ip': default_ip,
'default_ap': default_ap,
'chewbacca_ip': chewbacca_ip,
'chewbacca_ap': chewbacca_ap}
然后使用此字典从字符串中查找对象:
gameDisplay.blit(button_image_dict[ip], (x,y))
答案 1 :(得分:0)
我有一个字符词典:cheracters = {'default':0,'chewbacca':1,'Stormsoldier':2}
首先,我认为应该是一个清单。你所存储的只是物品位置。
如果我理解你要做什么,你需要一个图像字典
例如
ap = {
'default' : 'button_menu_ap.png',
'chewbacca' : 'button_select_GalaxyWars_chewbacca_ap.png'
}
nombre = 'chewbacca'
chewbacca_ap = pygame.image.load(ap[nombre])