简体   繁体   English

改变 pygame 中方块的颜色

[英]changing color of squares in pygame

I want to make simple program in which you can press squares on screen and they will change color, but i can't find in documentation how to do it with pygame.draw.rect .我想制作一个简单的程序,您可以在其中按下屏幕上的方块,它们会改变颜色,但我在文档中找不到如何使用pygame.draw.rect来做到这一点。

    import pygame
pygame.init()


screen = pygame.display.set_mode([501, 501])

siatka = [[[0] for i in range(10) ]for i in range(10)]

def rysowanie():
    for i,el in enumerate(siatka):
        for j,ele in enumerate(el):
            pygame.draw.rect(screen,(0,0,0),pygame.Rect(50*j +1,50*i+1,49,49))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((0, 255, 0))

    rysowanie()

    pygame.display.flip()
    
pygame.quit()

I don't even have an idea what is the name of the thing i want to do, so if this is very easy I'am sorry.我什至不知道我想做的事情的名称是什么,所以如果这很容易,我很抱歉。

Make your siatka a matrix of colors, and for each square, draw its corresponding color.将您的 siatka 设为siatka的矩阵,并为每个正方形绘制相应的颜色。 When the screen is clicked, change the color in siatka for that mouse position.单击屏幕时,更改siatka中该鼠标 position 的颜色。

import pygame

pygame.init()
screen = pygame.display.set_mode([501, 501])
siatka = [[(0, 0, 0) for i in range(10)] for i in range(10)]

def rysowanie():
    for i, el in enumerate(siatka):
        for j, ele in enumerate(el):
            pygame.draw.rect(screen, ele, pygame.Rect(50 * j + 1, 50 * i + 1, 49, 49))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            siatka[event.pos[1] // 50][event.pos[0] // 50] = (255, 0, 0)
    screen.fill((0, 255, 0))
    rysowanie()
    pygame.display.flip()
pygame.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM