我正在制作游戏,我首先制作了一些框架供我学习和测试游戏中的不同内容。在这个游戏中,我有一个随机生成的图块地图,其中包含了土,草,水和煤炭的资源。在代码中,还有一些其他不相关的资源,但是我想做的是找到游戏中特定资源的坐标。我打算为此做的是说资源是一堵墙,在它下面有一堵墙,在它旁边,中间墙的纹理将变成一个角块。我的问题是我不知道如何找到坐标。有人可以帮我吗,老实说,我不确定自己在做什么,但我想学习。 这是我的代码:
import pygame, sys
import Sprites
import random
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
cloudx = -200
cloudy = 0
infoObject = pygame.display.Info()
DIRT = 0
GRASS = 1
WATER = 2
COAL = 3
CLOUD = 4
WALL = 5
controls = {
DIRT : 49,
GRASS: 50,
WATER: 51,
COAL : 52,
WALL : 53
}
infoObject = pygame.display.Info()
w = infoObject.current_w
h = infoObject.current_h
TILESIZE = 40
MAPWIDTH = 15
MAPHEIGHT = 15
WHITE = (255,255,255)
BLACK = (0,0,0)
resources = [DIRT, GRASS, WATER, COAL]
textures = {
DIRT : pygame.image.load('Sprites/Dirt.png'),
GRASS : pygame.image.load('Sprites/tile130.png'),
WATER : pygame.image.load('Sprites/Water.png'),
COAL : pygame.image.load('Sprites/Coal.png'),
CLOUD : pygame.image.load('Sprites/Cloud.png'),
WALL : pygame.image.load('Sprites/Swall.png')
}
playerPos = [0,0]
inventory = {
DIRT : 0,
GRASS : 0,
WATER : 0,
COAL : 0,
WALL : 10,
}
tilemap = [[DIRT for w in range(MAPWIDTH)] for h in range(MAPHEIGHT)]
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE + 50))
pygame.display.set_caption('M I N E C R A F T -- 2D')
pygame.display.set_icon(pygame.image.load('Sprites/player.png'))
PLAYER = pygame.image.load('Sprites/Player.png').convert_alpha()
for rw in range(MAPHEIGHT):
for cl in range(MAPWIDTH):
randomNumber = random.randint(0,15)
if randomNumber == 0:
tile = COAL
elif randomNumber == 1 or randomNumber == 2:
tile = WATER
elif randomNumber >= 3 and randomNumber <=7:
tile = GRASS
else:
tile = DIRT
tilemap[rw][cl] = tile
INVFONT = pygame.font.Font('freesansbold.ttf', 18)
print(tilemap)
while True:
currentTile = tilemap[playerPos[1]][playerPos[0]]
DISPLAYSURF.fill(BLACK)
for event in pygame.event.get():
# print(event)
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
for key in controls:
if (event.key == controls[key]):
if inventory[key] > 0:
inventory[key] -=1
inventory[currentTile] += 1
tilemap[playerPos[1]][playerPos[0]] = key
if(event.key == K_RIGHT) and playerPos[0] < MAPWIDTH - 1:
playerPos[0]+=1
elif(event.key == K_LEFT) and playerPos[0] > 0:
playerPos[0]-=1
elif(event.key == K_DOWN) and playerPos[1] < MAPHEIGHT - 1:
playerPos[1]+=1
elif(event.key == K_UP) and playerPos[1] > 0:
playerPos[1]-=1
if event.key == K_SPACE:
currentTile = tilemap[playerPos[1]][playerPos[0]]
inventory[currentTile] += 1
tilemap[playerPos[1]][playerPos[0]] = DIRT
for row in range(MAPHEIGHT):
for column in range(MAPWIDTH):
DISPLAYSURF.blit(textures[tilemap[row][column]],(column*TILESIZE, row*TILESIZE, TILESIZE, TILESIZE))
DISPLAYSURF.blit(PLAYER,(playerPos[0]*TILESIZE,playerPos[1]*TILESIZE))
DISPLAYSURF.blit(textures[CLOUD].convert_alpha(),(cloudx,cloudy))
cloudx +=1
if cloudx > MAPWIDTH*TILESIZE:
cloudy = random.randint(0, MAPHEIGHT*TILESIZE)
cloudx = -200
placePosition = 10
for item in resources:
DISPLAYSURF.blit(textures[item],(placePosition, MAPHEIGHT*TILESIZE+10))
placePosition+=50
textObj = INVFONT.render(str(inventory[item]), True, WHITE, BLACK)
DISPLAYSURF.blit(textObj,(placePosition, MAPHEIGHT*TILESIZE+20))
placePosition += 50
pygame.display.update()
fpsClock.tick(24)
for x in range(MAPWIDTH):
for y in range(MAPHEIGHT):
if tilemap[x][y] == WALL:
pos = tilemap[x][y]
if tilemap[pos[1]][pos[0]-1] == WALL:
print('I DID IT')
pygame.display.update()
编辑:我当前的代码已更新,添加的部分在最后
答案 0 :(得分:0)
如果要查找特定图块的坐标,则需要进行一些工作。即,您的程序将必须搜索每个图块并进行比较以查看它是否为WALL
图块。
即
for x in range(MAPWIDTH):
for y in range(MAPHEIGHT):
if tilemap[y][x] == WALL:
# Do something here...
但是,这极其效率低下。如果您的地图只有15 x 15的比例,这可能不是问题,但是如果您扩大地图的显示范围,它将开始吞噬您的性能。
只检查屏幕上显示的区域可能更明智。毕竟,您只是试图确定应如何显示显示图块,否则就没关系了。
for x in range(30): # Replace with whatever number of tiles is displayed horizontally on the screen, times two
for y in range(30): # Do the same here
if tilemap[y][x] == WALL:
# Do something here
不过,请注意-如果仅用于显示,则更好的解决方案是定义多个WALL
磁贴。您可以通过制作诸如WALL_UPPER_RIGHT
之类的常量来做到这一点,但是以列表的形式使其成为一个更好的主意。
WALL_TILES = [[5, 6, 7],
[8, 9, 10],
[11, 12, 13]]
# WALL_TILES is a 2D list in the format:
# upper left upper middle upper right
# middle left middle middle middle right
# lower left lower middle lower right
然后,您可以使用WALL_UPPER_RIGHT
解决类似WALL_TILES[0][2]
的问题。当您有多个墙面瓷砖时,只要更改瓷砖或其旁边的瓷砖的类型,就可以更改其类型。
添加:
要检查周围的瓷砖是否也是墙砖,可以检查x位置和y位置都在中心瓷砖1之内的瓷砖。例如,您可以检查tilemap[pos[1]][pos[0]-1]
。