在 Python/Pygame 中构建 Tilemap 并测试鼠标位置
Building a Tilemap in Python/Pygame and testing for mouse position
嘿,感谢您提供的任何帮助
我正在创建一个瓷砖地图来测试一个可能的项目。我找到了一个有效生成瓷砖地图的教程。然后,如果鼠标位于块的位置,我会尝试通过循环遍历每个 X 和 Y 坐标测试来实现我自己的代码。如果鼠标位于图块的顶部,则会在其上绘制一个框以创建鼠标所在位置的视觉效果。我遇到的问题是网格看起来像这样:
####
####
####
####
但是鼠标检测只对这些方块进行对角线检测:
####
###
##
#
代码如下:
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()
首先你没有清屏。
接下来你的绘制代码是错误的(如果检查是错误的)
正确的是 x 表示列,y 表示行。
希望对您有所帮助! :)
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))
当为真时:
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()
嘿,感谢您提供的任何帮助
我正在创建一个瓷砖地图来测试一个可能的项目。我找到了一个有效生成瓷砖地图的教程。然后,如果鼠标位于块的位置,我会尝试通过循环遍历每个 X 和 Y 坐标测试来实现我自己的代码。如果鼠标位于图块的顶部,则会在其上绘制一个框以创建鼠标所在位置的视觉效果。我遇到的问题是网格看起来像这样:
####
####
####
####
但是鼠标检测只对这些方块进行对角线检测:
####
###
##
#
代码如下:
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()
首先你没有清屏。 接下来你的绘制代码是错误的(如果检查是错误的) 正确的是 x 表示列,y 表示行。
希望对您有所帮助! :)
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))
当为真时:
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()