【问题标题】:Why is pygame.draw.rect infinitely re-drawing rectangles?为什么 pygame.draw.rect 无限地重新绘制矩形?
【发布时间】:2022-01-17 17:53:33
【问题描述】:

我有这个 pygame 代码,我希望它为一个镶有宝石的闪电战风格游戏绘制一个矩形板。这就是我所拥有的,当你运行它时,它会无限更新向屏幕发送垃圾邮件的新矩形。

import pygame as pg
import sys, random
pg.init()
clock = pg.time.Clock()
cell_size = 40
cell_num = 20
disp = pg.display.set_mode((cell_size * cell_num, cell_size * cell_num))
bg = pg.Surface((cell_size * cell_num, cell_size * cell_num))
bg.fill('white')
gold = pg.Color(221, 238, 81)
sea_foam = pg.Color(81, 238, 143)
pink = pg.Color(238, 145, 251)
gr33n = pg.Color(0, 255, 35)
blu = pg.Color(0, 236, 255)
colors_list = [gold, sea_foam, pink, gr33n, blu]

class Board:
  def __init__(self):
    self.x = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400, 440, 480, 520, 560, 600, 640, 680, 720, 760, 800]
    self.y = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400, 440, 480, 520, 560, 600, 640, 680, 720, 760, 800]

def drawBoard(self):
    for i in range(0,21):
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[0], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[1], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[2], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[3], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[4], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[5], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[6], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[7], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[8], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[9], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[10], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[11], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[12], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[13], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[14], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[15], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[16], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[17], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[18], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[19], cell_size, cell_size))
        pg.draw.rect(disp, random.choice(colors_list), (self.x[i], self.y[20], cell_size, cell_size))


game = Board()
while True:

for event in pg.event.get():

    if event.type == pg.QUIT:
        pg.quit()
        sys.exit()

disp.blit(bg,(0,0))
game.drawBoard()
pg.display.update()
clock.tick(60)

【问题讨论】:

  • 当你运行你的代码时,(你在这里发布的那个),你会得到一个空白屏幕吗?我不知道,但我得到一个黑屏。
  • 欢迎来到 Stack Overflow。请阅读formatting help 并确保您的代码与您实际拥有的完全相同。此处显示的代码将是语法错误,因为while True: 循环中没有任何缩进。

标签: python pygame


【解决方案1】:

您每帧都在生成新颜色。您需要在开始时为每颗宝石生成一次颜色并使用这些颜色来绘制矩形。

class Board:
    def __init__(self):
        self.x = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400, 440, 480, 520, 560, 600, 640, 680, 720, 760, 800]
        self.y = [0, 40, 80, 120, 160, 200, 240, 280, 320, 360, 400, 440, 480, 520, 560, 600, 640, 680, 720, 760, 800]
        self.size = (21, 20)
        #generate random color for every posible jewel on screen once
        self.colors= [random.choice(colors_list) for i in range(self.size[0] * self.size[1])] 

    def drawBoard(self):
        #go through every jwell position on board and draw them. 
        for x in range(self.size[0]):
            for y in range(self.size[1]):
                pg.draw.rect(disp, self.colors[y * self.size[0] + x], (self.x[x], self.y[y], cell_size, cell_size))

【讨论】:

  • 非常感谢,但是我不明白这行发生了什么:pg.draw.rect(disp, self.colors[y * self.size[0] + x], ( self.x[x], self.y[y], cell_size, cell_size)) 和 self.colors[y * self.size[0] + x] 那创造了什么数量?是将 self.size[0] (21) 乘以 y 中的每个值,然后将 x 中的每个值相加吗?
  • 它将二维数组转换为一维数组。我认为this 的解释可能会有所帮助。
  • 我也没有提到它,因为我不知道这是故意的,但你不需要 self.x 和 self.y 列表。您可以使用 i 值,因为列表中的值是 40 的倍数。 pg.draw.rect(disp, self.colors[y * self.size[0] + x], (x * cell_size, y * cell_size, cell_size, cell_size))
【解决方案2】:

试试这个:

n = 1
game = Board()
while True:

for event in pg.event.get():

    if event.type == pg.QUIT:
        pg.quit()
        sys.exit()
   
    clock.tick(60)

    while n == 1:      
        disp.blit(bg,(0,0))
        game.drawBoard()
        pg.display.update()
        n -= 1



【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    相关资源
    最近更新 更多