gpt4 book ai didi

python - 每次用户单击时如何用颜色填充矩形?小游戏

转载 作者:行者123 更新时间:2023-12-04 15:21:14 29 4
gpt4 key购买 nike

我知道网上有几个类似的问题,但没有一个真正对我有帮助。我只是想绘制一个网格,并为用户提供点击这些网格单元格的选项。每次用户单击时,单元格的颜色/填充应从黑色变为白色。
我目前正在做的事情如下:

BLACK = (0, 0, 0)
WHITE = (200, 200, 200)

def drawGrid(h, w, blocksize):
for x in range(w):
for y in range(h):
rect = pygame.Rect(x*blocksize, y*blocksize,
blocksize, blocksize)
pygame.draw.rect(SCREEN, WHITE, rect, 1)


def handle_events():
col = WHITE

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
col = WHITE
# determine mouse position
mpos_x, mpos_y = event.pos
# determine cell number
coord = mpos_x // blocksize, mpos_y // blocksize

rect = pygame.Rect(coord[0]*blocksize, coord[1]*blocksize,
blocksize, blocksize)
pygame.draw.rect(SCREEN, col, rect, 1)
pygame.display.update()

def main():

global SCREEN, CLOCK, blocksize

w = int( sys.argv[1] )
h = int( sys.argv[2] )
blocksize = int( sys.argv[3] )

pygame.init()
SCREEN = pygame.display.set_mode((h, w))
CLOCK = pygame.time.Clock()
SCREEN.fill(BLACK)

drawGrid(h,w,blocksize)
handle_events()




if __name__ == "__main__":
main()
程序正在打印网格。但是,当我点击某个地方时,什么也没有发生。我知道这不是最好的代码,所以我很感激任何建议。

最佳答案

我稍微更改了代码,它运行正常,pygame.draw.rect(SCREEN, col, rect, 1)你画同样的东西,你看不到变化。您应该使用 pygame.draw.rect(SCREEN, col, rect) :

import pygame
import sys

BLACK = (0, 0, 0)
WHITE = (200, 200, 200)
# WINDOW_HEIGHT = 400
# WINDOW_WIDTH = 400


def drawGrid(h, w, blocksize):
#blockSize = 20 #Set the size of the grid block
for x in range(w):
for y in range(h):
rect = pygame.Rect(x*blocksize, y*blocksize,
blocksize, blocksize)
pygame.draw.rect(SCREEN, WHITE, rect, 1)


def handle_events():
#coords_list = []
col = WHITE

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
col = WHITE
# determine mouse position
mpos_x, mpos_y = event.pos
# determine cell number
coord = mpos_x // 32, mpos_y // 32

rect = pygame.Rect(coord[0]*32, coord[1]*32,
32, 32)
pygame.draw.rect(SCREEN, col, rect)
#coords_list.append(coord)

pygame.display.update()

#return coords_list

def main():

global SCREEN, CLOCK, blocksize



pygame.init()
SCREEN = pygame.display.set_mode((480, 640))
CLOCK = pygame.time.Clock()
SCREEN.fill(BLACK)

drawGrid(480,640,32)
handle_events()




if __name__ == "__main__":
main()

作为建议,我认为您应该为每个单元格使用 Sprite 。例如像这样:
class Cell(pygame.sprite.Sprite):
def __init__(self, sprite_group, x, y, cell_dimension, color=BLACK):
self.groups = sprite_group
pygame.sprite.Sprite.__init__(self, self.groups)
self.image = pygame.Surface((cell_dimension, cell_dimension))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x * cell_dimension
self.rect.y = y * cell_dimension

self.clicked = False

def update(self):
if self.clicked:
self.image.fill(WHITE)

关于python - 每次用户单击时如何用颜色填充矩形?小游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63260578/

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