gpt4 book ai didi

python - Pygame : Is color readable from a class variable in function pygame. 绘制.矩形?

转载 作者:行者123 更新时间:2023-12-01 07:50:13 24 4
gpt4 key购买 nike

我想知道是否可以在类中使用 pygame 中的 pygame.draw.rect() 函数和颜色变量。

这是我的代码,其中包含详细的工作说明:(请注意,我只选取了有用的部分)

# consider pygame as 'pg' as I set with
# the line 'import pygame as pg'

class icon:
def __init__(self, picture, position, key):
self.icon = picture
self.position = position
self.p_x, self.p_y = position
self.size = 50
self.unlocked = False
self.key = key
self.status = 'off'
self.pulse_value = 0
self.pulse = 'down'
self.pulse_type = 'red'
self.c_icon = self.icon.get_rect()
self.c_icon.center = ( (self.p_x + 25), (self.p_y + 25))

def unlock(self):
self.unlocked = True
self.status = 'pulse'
self.pulse_type = 'grey'

def draw(self):
if self.unlocked == True :
if self.status == 'off':
pg.draw.rect(screen, color_passive, (*self.position, 50, 48))

elif self.status == 'on':
pg.draw.rect(screen, color_active, (*self.position, 50, 55))

elif self.status == 'pulse':
if self.pulse == 'down' :
self.pulse_value = self.pulse_value + 1
if self.pulse_value == 255 :
self.pulse = 'up'

elif self.pulse == 'up' :
self.pulse_value = self.pulse_value - 1
if self.pulse == 0 :
self.pulse = 'down'

if self.pulse_type == 'red' :
self.color_pulse = (self.pulse_value, 0, 0)
elif self.pulse_type == 'grey' :
self.color_pulse = (self.pulse_value, self.pulse_value, self.pulse_value )
pg.draw.rect(screen, *self.color_pulse, (*self.position, *self.size))

screen.blit(self.icon, self.c_icon)

world_2 = pg.image.load('ice_mountains.png').convert_alpha()
icon_1 = icon('world_2', (60, 60), K_1)

这是类图标,用于显示屏幕上的任何选项卡图标并定义选项卡是否解锁。

如你所见,我在这里使用了一个函数pygame.draw.rect()

但在这个函数中,颜色变量是一个“类变量”(self.color_pulse)

变量self.color_pulse由上行的(self.pulse_value, self.pulse_value, self.pulse_value)定义。

self.pulse_value是一个类变量,每次icon_2.draw()都会以每秒30次的速度增加5。

基本上,我在主循环中使用函数 icon_2.draw()

    elif exp >= 1000:
n_unlock = n_unlock + 1
icon_2.unlock()
print('world 2 unlocked')

这是解锁图标的代码。当主循环在解锁后执行 icon_2.draw() 时,预期的结果是图标应该出现在屏幕上,默认..ulse直到用户单击它。

但是,不幸的是,我得到了这个:

#----------*={ _MAD_ }=*----------#
May Avoid Destruction
by Edhyjox
#----------*={ _MAD_ }=*----------#

Loading...
========== Ok

custom setting succesfully imported
Window main not existing, redirect to game

World 2 unlocked
Traceback (most recent call last):
File "************************/___MAD___/script/MAD - The game.py", line 266, in <module>
icon_2.draw()
File "************************/___MAD___/script/MAD - The game.py", line 190, in draw
pg.draw.rect(screen, *self.color_pulse, (*self.position, *self.size))
TypeError: 'int' object is not iterable

我真的不知道为什么会出现这个错误。这意味着我在函数 pg.draw.rect() 中使用“int”,但变量 self.color_pulse 在程序的此时为 (5, 5, 5)。

它似乎适用于不在类中的变量......有人对这个问题有线索吗?

最佳答案

pygame.draw.rect()的第二个参数必须是一种颜色。在 PyGame 中,颜色可以是 3 个整数值的元组。
因此 *- 运算符(解包运算符)是多余的。

请注意

self.color_pulse = (self.pulse_value, 0, 0)
pg.draw.rect(screen, *self.color_pulse, (*self.position, *self.size))

相同
pg.draw.rect(screen, self.color_pulse, 0, 0, (*self.position, *self.size))

考虑一下 self.pulse_value 在构造函数中由 0 初始化。

self.pulse_value = 0

如果 self.pulse_type 既不是 'red' 也不是 'grey',则 *self.color_pulse无论如何都不会工作并导致错误。

<小时/>

此外,第三个参数必须是矩形。矩形是 4 个值的元组,但 self.size 是单个整数值。

self.size = 50

因此 *self.size 会导致错误。

<小时/>

跳过self.color_puls之前的*-运算符,并将*self.size更改为self.size, self.size :

pg.draw.rect(screen, self.color_pulse, (*self.position, self.size, self.size))

关于python - Pygame : Is color readable from a class variable in function pygame. 绘制.矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56267961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com