pygame 中对象列表的颜色渐变

问题描述 投票:0回答:1

我正在尝试在 pygame 中重新创建游戏蛇作为介绍,但是我正在努力解决如何在蛇移动时为蛇的每个方块创建渐变。我的其余代码都是有效的。

我写了以下代码:

import colorsys

class Snake:
  def __init__(self):
    self.body = [Vector2(5,no_cell/2), Vector2(4,no_cell/2), Vector2(3,no_cell/2)]
    self.direction = Vector2(1,0)
    self.new_block = False
    self.start_hue = 0.0  
    self.end_hue = 0.7    
    self.num_steps = 20  

  def generate_gradient(self, start_hue, end_hue, num_steps):
    hues = []
    for i in range(num_steps):
      hue = start_hue + (end_hue - start_hue) * (i / (num_steps - 1))
      hues.append(hue % 1.0)  
    colours = [colorsys.hsv_to_rgb(hue, 1.0, 1.0) for hue in hues]

    return colours

  def draw_snake(self):
    colours = self.generate_gradient(self.start_hue, self.end_hue, 
  self.num_steps)
    for rgb in colours:   
     for block in self.body:
       x_pos = int(block.x * cell_size)
       y_pos = int(block.y * cell_size)
       block_rect = pygame.Rect(x_pos + grid_border, y_pos + 
  heading_border + grid_border, cell_size, cell_size)
       rgb = tuple(int(value * 255) for value in rgb)
       pygame.draw.rect(screen, rgb, block_rect)

当我运行此命令时,出现以下错误: “pygame.draw.rect(屏幕,rgb,block_rect) ValueError:无效的颜色参数”

python colors pygame
1个回答
0
投票

您遇到的问题可能是由于您尝试在其范围之外使用 rgb 元组。 rgb 变量在蛇身体的每个块的内部循环中重新定义,但应该为渐变中的每种颜色定义一次,然后用于与该颜色对应的所有块。

这是您的draw_snake方法的修订版本,应该可以解决问题:

import pygame
import colorsys

class SnakeGame:
    def __init__(self, start_hue, end_hue, num_steps, body, cell_size, grid_border, heading_border, screen):
        self.start_hue = start_hue
        self.end_hue = end_hue
        self.num_steps = num_steps
        self.body = body
        self.cell_size = cell_size
        self.grid_border = grid_border
        self.heading_border = heading_border
        self.screen = screen

    def generate_gradient(self, start_hue, end_hue, num_steps):
        hues = []
        for i in range(num_steps):
            hue = start_hue + (end_hue - start_hue) * (i / (num_steps - 1))
            hues.append(hue % 1.0)
        colours = [colorsys.hsv_to_rgb(hue, 1.0, 1.0) for hue in hues]

        return colours

    def draw_snake(self):
        colours = self.generate_gradient(self.start_hue, self.end_hue, len(self.body))
        for i, block in enumerate(self.body):
            x_pos = int(block.x * self.cell_size)
            y_pos = int(block.y * self.cell_size)
            block_rect = pygame.Rect(x_pos + self.grid_border, y_pos + self.heading_border + self.grid_border, self.cell_size, self.cell_size)
            rgb = colours[i]
            rgb = tuple(int(value * 255) for value in rgb)
            pygame.draw.rect(self.screen, rgb, block_rect)

# Example usage:
# Assuming you have the following variables defined:
# start_hue, end_hue, num_steps, body, cell_size, grid_border, heading_border, screen
game = SnakeGame(start_hue, end_hue, num_steps, body, cell_size, grid_border, heading_border, screen)
game.draw_snake()

以下是我所做的更改:

将代码封装在一个名为 SnakeGame 的类中以更好地组织。 将所有必需的变量传递给类的构造函数并将它们存储为实例变量。 更新了generate_gradient方法以返回与蛇的身体长度相同的RGB颜色列表。 在draw_snake方法中,我使用enumerate从蛇的身体中获取索引和块。该索引用于从颜色列表中获取相应的颜色。 rgb 元组现在在外循环内正确确定范围,确保每个块获得正确的颜色。 确保将占位符变量(start_hue、end_hue、num_steps、body、cell_size、grid_border、heading_border、screen)替换为游戏中的实际值。

当你想绘制具有渐变效果的蛇时,请记住使用正确的参数初始化SnakeGame类并调用draw_snake方法。

© www.soinside.com 2019 - 2024. All rights reserved.