简体   繁体   English

在 Python/Pygame 中构建 Tilemap 并测试鼠标位置

[英]Building a Tilemap in Python/Pygame and testing for mouse position

Hey i appreciate any help you can provide嘿,我感谢您提供的任何帮助

I am creating a tile-map for a test of a possible project.我正在为一个可能的项目的测试创建一个平铺地图。 I have found a tutorial which produced the tile-map effectively.我找到了一个可以有效生成瓷砖地图的教程。 I then tried to implement my own code by making it loop through each X and Y coordinate testing if the mouse is in the position of the block.然后,如果鼠标位于块的位置,我尝试通过循环测试每个 X 和 Y 坐标来实现我自己的代码。 If the mouse was positioned on top of the tile a box would be drawn on it to create a visual for where the mouse was.如果鼠标位于图块的顶部,则会在其上绘制一个框以创建鼠标所在位置的视觉效果。 The problem i have is that the grid was made to look like this:我遇到的问题是网格看起来像这样:

                                    ####
                                    ####
                                    ####
                                    ####

But the mouse detection only works diagonally on these tiles:但鼠标检测仅适用于这些瓷砖对角线:

                                    ####
                                     ###
                                      ##
                                       #

The code is below:代码如下:

    from pygame.locals import *
import pygame, sys

green = (40,255,30)
brown = (40,60,90)
red = (155,20,30)
yellow = (0,155,155)

grass = 0
dirt = 1
lava = 2

colours = {
    grass: green,
    dirt: brown,
    lava: red,
    }

tilemap = [
        [grass,dirt,dirt,dirt, lava],
        [dirt,lava,dirt,dirt, dirt],
        [lava, grass,dirt,dirt, lava],
        [lava, grass,dirt,dirt, grass],
        [dirt,dirt,dirt,dirt,grass]

        ]

TILESIZE = 50
MAPWIDTH =  5
MAPHEIGHT = 5

pygame.init()
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))

while True:


    mouse_x = pygame.mouse.get_pos()[0]
    mouse_y = pygame.mouse.get_pos()[1]


    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit
        for row in range(MAPWIDTH):
            print
            for column in range(MAPHEIGHT):
                pygame.draw.rect(DISPLAYSURF, colours[tilemap[row][column]], (column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE))
                if mouse_x >= (row * TILESIZE) and mouse_x <= (row* TILESIZE) + TILESIZE:
                    if mouse_y >= (column * TILESIZE) and mouse_y <= (column* TILESIZE) + TILESIZE:
                        print (str(row) + " " + str(column))
                        pygame.draw.rect(DISPLAYSURF, yellow, (row * TILESIZE, column*TILESIZE, TILESIZE, TILESIZE))







    pygame.display.update()

First you're not clearing the screen.首先,您没有清除屏幕。 Next your draw code is wrong (wrong if checks) The correct is x for columns and y for rows.接下来你的绘图代码是错误的(如果检查是错误的)正确的是 x 代表列,y 代表行。

I hope that this could help you!我希望这可以帮助你! :) :)

from pygame.locals import *
import pygame, sys

green = (40,255,30)
brown = (40,60,90)
red =  (155,20,30)
yellow = (0,155,155)

grass = 0
dirt = 1
lava = 2

colours = {
    grass: green,
    dirt: brown,
    lava: red,
    }

tilemap = [
        [grass,dirt,dirt,dirt, lava],
        [dirt,lava,dirt,dirt, dirt],
        [lava, grass,dirt,dirt, lava],
        [lava, grass,dirt,dirt, grass],
        [dirt,dirt,dirt,dirt,grass]

        ]

TILESIZE = 50
MAPWIDTH =  5
MAPHEIGHT = 5

pygame.init()
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE))

while True:为真:

    mouse_x = pygame.mouse.get_pos()[0]
    mouse_y = pygame.mouse.get_pos()[1]

    print mouse_x, mouse_y

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

        DISPLAYSURF.fill((0,0,0));
        for row in range(MAPWIDTH):
            print            
            for column in range(MAPHEIGHT):
                color = colours[tilemap[row][column]];
                if mouse_x >= (column * TILESIZE) and mouse_x <= (column* TILESIZE) + TILESIZE:
                    if mouse_y >= (row * TILESIZE) and mouse_y <= (row* TILESIZE) + TILESIZE:
                        print (str(row) + " " + str(column))
                        color = yellow;                                                                                                            

                pygame.draw.rect(DISPLAYSURF, color, (column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE))


    pygame.display.update()

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

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