无法通过变量传递rgb值

时间:2019-10-17 12:58:10

标签: python pygame

不用多说了,我创建了一个变量

colour = (int, int, int)

然后我尝试以这种方式通过

self.img = self.my_font.render(self.unit_type, 1, self.colour)

我得到的错误是

Traceback (most recent call last):
  File "C:/Users/oricc/PycharmProjects/designAChessboardChallange/display.py", line 137, in <module>
    pw1 = Pikeman()
  File "C:/Users/oricc/PycharmProjects/designAChessboardChallange/display.py", line 90, in __init__
    self.img = self.my_font.render(self.unit_type, 1, self.colour)
TypeError: Invalid foreground RGBA argument

完整的代码如下

import pygame
import sys
from coordinator import coordinator

# Sets up the display

pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')


# Defines classes and related methods

class WhiteSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.white_square = pygame.Surface((self.height, self.width))
        self.white_square.fill((255, 255, 255))


class BlackSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.black_square = pygame.Surface((self.height, self.width))
        self.black_square.fill((0, 0, 0))


class ChessBoard:
    def __init__(self):
        self.ws = ws
        self.bs = bs
        self.white_columns = white_columns
        self.black_columns = black_columns

    def draw(self):
        for w_columns in self.white_columns:
            game_window.blit(self.ws.white_square, w_columns)

        for b_columns in self.black_columns:
            game_window.blit(self.bs.black_square, b_columns)


# class SquareNames:
#     letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
#     numbers = ['1', '2', '3', '4', '5', '6', '7', '8']
#     square_names = []
#     for letter in letters:
#         for number in numbers:
#             square_name = letter + number
#             square_names.append(square_name)
#     print((square_names))
#     # for coordinate in
#     # coordinates = (square_name : coordinate)

# TBC..........

# Background for units

class CircleSurface:
    def __init__(self):
        self.circle_surface = pygame.Surface((100, 100), flags=pygame.SRCALPHA)
        pygame.draw.circle(self.circle_surface, (0, 255, 0), (50, 50), 45)


class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 100)
        self.cs = cs

    def draw_circle(self, surface):
        pygame.draw.circle(self.cs.circle_surface, (0, 255, 0), (200, 200), 50)
        surface.blit(self.cs.circle_surface, (200, 200))


class Pikeman(Unit):
    unit_type = 'P'
    destination = (int, int)
    colour = (int, int, int)

    def __init__(self):
        self.r = int
        self.g = int
        self.b = int
        self.colour = (self.r, self.g, self.b)
        super().__init__()
        self.img = self.my_font.render(self.unit_type, 1, self.colour)

    def set_colour(self, colour):
        self.colour = colour

    def draw(self, surface):
        surface.blit(self.img, self.destination)


class Archer(Unit):
    unit_type = 'A'
    destination = (525, 525)

    def __init__(self):
        super().__init__()
        self.img = self.my_font.render(self.unit_type, 1, (255, 0, 0))

    def draw(self, surface):
        surface.blit(self.img, self.destination)


class Knight(Unit):
    unit_type = 'K'
    destination = (325, 525)

    def __init__(self):
        super().__init__()
        self.img = self.my_font.render(self.unit_type, 1, (255, 0, 0))

    def draw(self, surface):
        surface.blit(self.img, self.destination)

# Sets and gets the coordinates for black and white squares


coordinator = coordinator()

black_columns = coordinator[2] + coordinator[3]
white_columns = coordinator[0] + coordinator[1]

# Creates needed objects

ws = WhiteSquare()
bs = BlackSquare()
cb = ChessBoard()
cs = CircleSurface()

pw1 = Pikeman()
pw1.destination = (225, 625)
# pw1.colour = (0, 0, 255)
# print('pw1 colour' + str(pw1.colour))
pw1.set_colour((0, 0, 255))
print('colour' + str(pw1.colour))

pw2 = Pikeman()
pw2.destination = (325, 625)
pw3 = Pikeman()
pw3.destination = (425, 625)
pw4 = Pikeman()
pw4.destination = (525, 625)
kw1 = Knight()
kw1.destination = (125, 625)

kw2 = Knight()
kw2.destination = (625, 625)

aw1 = Archer()
aw1.destination = (225, 725)
aw2 = Archer()
aw2.destination = (325, 725)
aw3 = Archer()
aw3.destination = (425, 725)
aw4 = Archer()
aw4.destination = (525, 725)


# Event loop (outer)

while 1:
    # Event loop (inner)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
    # Draws needed objects and updates display

    cb.draw()

    # Draws white pieces in their initial position
    pw1.draw(game_window)
    pw1.draw_circle(game_window)
    pw2.draw(game_window)
    pw2.draw_circle(game_window)
    pw3.draw(game_window)
    pw3.draw_circle(game_window)
    pw4.draw(game_window)
    pw4.draw_circle(game_window)
    kw1.draw(game_window)
    kw1.draw_circle(game_window)
    kw2.draw(game_window)
    kw2.draw_circle(game_window)
    aw1.draw(game_window)
    aw1.draw_circle(game_window)
    aw2.draw(game_window)
    aw2.draw_circle(game_window)
    aw3.draw(game_window)
    aw3.draw_circle(game_window)
    aw4.draw(game_window)
    aw4.draw_circle(game_window)

    pygame.display.update()

从这段代码以及从我在Pikeman()中创建的所有变量中可以看到,我首先尝试直接设置color变量的值,但是随后我尝试创建一个setter,我无法使其工作。

pw1 = Pikeman()
pw1.destination = (225, 625)
# pw1.colour = (0, 0, 255)
# print('pw1 colour' + str(pw1.colour))
pw1.set_colour((0, 0, 255))
print('colour' + str(pw1.colour))

2 个答案:

答案 0 :(得分:2)

对不起,您的假设是错误的。当然,如果更改了self.color的值,

  
def set_colour(self, colour):
   self.colour = colour

然后使用self.colour绘制的每个对象将使用新的颜色集进行绘制。

可悲的是self.colour再也没有用于呈现任何东西。注意,在对象的构造函数中使用self.colour来渲染Solution Stackblitz example

  
def __init__(self):
   # [...]

   self.colour = (self.r, self.g, self.b)
   self.img = self.my_font.render(self.unit_type, 1, self.colour)

如果更改用于渲染Surface(图像)的颜色,则图像不会发生神奇的变化。

更改颜色后,必须再次渲染Surface

class Pikeman(Unit):
    unit_type = 'P'
    destination = (int, int)
    colour = (int, int, int)

    def __init__(self):
        super().__init__()
        self.set_colour((0, 0, 0))

    def set_colour(self, colour):
        self.colour = colour
        self.img = self.my_font.render(self.unit_type, 1, self.colour)

    def draw(self, surface):
        surface.blit(self.img, self.destination)

答案 1 :(得分:0)

问题是初始化rgb变量的类的类型int。即如果将Alla零赋予初始值,则它将是便捷的类型,因此您需要以不同的方式处理它。

def __init__(self):
    self.r = 0
    self.g = 0
    self.b = 0
    self.colour = (self.r, self.g, self.b)
    super().__init__()
    self.img = self.my_font.render(self.unit_type, 1, self.colour)

我不是说这是解决方案,但这是您的问题。