简体   繁体   English

如何检测网格单元格中的单击并更改其颜色?

[英]How do you detect a click in a cell of a grid and change its color?

Quite new to python and trying to re-create a game I played at school.对 python 非常陌生,并试图重新创建我在学校玩的游戏。 for this game, I need to randomly spawn these bombs that reduce your bank account to zero if hit and change the colour of the bomb square of the square to red (while safe ones go green).对于这个游戏,我需要随机生成这些炸弹,如果被击中,这些炸弹会将你的银行账户减少为零,并将方块的炸弹方块的颜色更改为红色(而安全的 go 为绿色)。 The issues I'm having are:我遇到的问题是:

  • I want to change the colour of a bomb square red, they don't do this though, they just stay white, however, the safe ones do go green我想将炸弹正方形的颜色更改为红色,但他们不这样做,它们只是保持白色,但是,安全的将 go 变为绿色
  • also for the co-ordinates of the bomb, I want to make sure none of those are equal, I want to make it so it re-rolls the duplicate one同样对于炸弹的坐标,我想确保它们都不相等,我想使它重新滚动重复的

Any help with either of those would be greatly appreciated任何对其中任何一个的帮助将不胜感激

#--------------------------------Pirate Game-----------------------------------------------------------------------------------------------------

#initilising
import pygame
import random
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
 

width = 20
height = 20
margin = 5#gap between 

current = 0
bank = 0
turnnumber = 0

grid = []
for row in range(10):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(10):
        grid[row].append(0) 
 

pygame.init()
GameR = [600,600]
gameD = pygame.display.set_mode(GameR)
pygame.display.set_caption("Pirate Game")
 
done = False

clock = pygame.time.Clock()
#-------------------------------------------------------------------------------------------------------
#FUNCTIONS
def bombs():
    for i in range(5):
        row = random.randrange(10)
        column = random.randrange(10)
        grid[row][column] = 2
        print(row,column)



#---------------------------------------------------------------------------
#MAIN LOOP:

done = False

clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bombs()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                print("Dont Press that")
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            # Change the x/y screen coordinates to grid coordinates
            column = pos[0] // (width + margin)
            row = pos[1] // (height + margin)
            if grid[row][column] == 2:
                print('Boom')
            if grid[row][column] != 2:
                grid[row][column] = 1
                print("Click ", pos, "Grid coordinates: ", row, column)

                
    gameD.fill(black)
 
    #Draw the grid
    for row in range(10):
        for column in range(10):
            color = white
            if grid[row][column] == 1:
                color = green
            elif grid[row][column] == 2:
                colour = red
            pygame.draw.rect(gameD,
                             color,
                             [(margin +
                               width) * column + margin,
                              (margin + height) * row + margin,
                              width,
                              height])
 
    
    clock.tick(60)
    pygame.display.flip()
 
pygame.quit()

First of all there is a typo: color = red rather than colour = red .首先有一个错字: color = red而不是colour = red

Anyway you've to change your game logic.无论如何,你必须改变你的游戏逻辑。 Use the values 0 and 2 for cells which are not clicked and the values 1 and 3 for cells that have been clicked.对未单击的单元格使用值 0 和 2,对已单击的单元格使用值 1 和 3。 Initialize all fields of the grid by 0. Put random bombs on the grid by assigning 2 to random cells.将网格的所有字段初始化为 0。通过将 2 分配给随机单元格,将随机炸弹放在网格上。
When a cell with the value 0 is clicked, then change its value to 1. When a cell with a bomb (value 2) is clicked then change it to 3:单击值为 0 的单元格时,将其值更改为 1。单击具有炸弹(值 2)的单元格时,将其更改为 3:

while not done:
    # [...]

    for event in pygame.event.get():  
        # [...]

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Change the x/y screen coordinates to grid coordinates
            column = event.pos[0] // (width + margin)
            row = event.pos[1] // (height + margin)
            if grid[row][column] == 2:
                print('Boom')
                grid[row][column] = 3
            if grid[row][column] == 0:
                grid[row][column] = 1
                print("Click ", event.pos, "Grid coordinates: ", row, column)

When you draw the grid, then all the cells have to be drawn white.绘制网格时,所有单元格都必须绘制为白色。 Except a cell has the value 1. In this case it's color is green.除了单元格的值为 1。在这种情况下,它的颜色是绿色。 If a cell has the value 3, it's color is red:如果单元格的值为 3,则其颜色为红色:

while not done:
    # [...]

    #Draw the grid
    for row in range(10):
        for column in range(10):
            color = white
            if grid[row][column] == 1:
                color = green
            elif grid[row][column] == 3:
                color = red
            cell_rect = ((margin + width) * column + margin, (margin + height) * row + margin, width, height)
            pygame.draw.rect(gameD, color, cell_rect)

Complete example:完整示例:

#initilising
import pygame
import random
black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
 
width = 20
height = 20
margin = 5#gap between 

current = 0
bank = 0
turnnumber = 0

grid = []
for row in range(10):
    # Add an empty array that will hold each cell
    # in this row
    grid.append([])
    for column in range(10):
        grid[row].append(0) 
 
pygame.init()
GameR = [600,600]
gameD = pygame.display.set_mode(GameR)
pygame.display.set_caption("Pirate Game")
 
done = False
clock = pygame.time.Clock()
#-------------------------------------------------------------------------------------------------------
#FUNCTIONS
def bombs():
    for i in range(5):
        row = random.randrange(10)
        column = random.randrange(10)
        grid[row][column] = 2
        print(row,column)

bombs()

#---------------------------------------------------------------------------
#MAIN LOOP:

done = False

clock = pygame.time.Clock()
while not done:
    for event in pygame.event.get():  
        if event.type == pygame.QUIT:  
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                bombs()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                print("Dont Press that")
        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Change the x/y screen coordinates to grid coordinates
            column = event.pos[0] // (width + margin)
            row = event.pos[1] // (height + margin)
            if grid[row][column] == 2:
                print('Boom')
                grid[row][column] = 3
            if grid[row][column] == 0:
                grid[row][column] = 1
                print("Click ", event.pos, "Grid coordinates: ", row, column)
           
    gameD.fill(black)
 
    #Draw the grid
    for row in range(10):
        for column in range(10):
            color = white
            if grid[row][column] == 1:
                color = green
            elif grid[row][column] == 3:
                color = red
            cell_rect = ((margin + width) * column + margin, (margin + height) * row + margin, width, height)
            pygame.draw.rect(gameD, color, cell_rect)
 
    clock.tick(60)
    pygame.display.flip()
 
pygame.quit()

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

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