使用 pygame 问题/问题的基于磁贴的游戏 [英] Tile based game using pygame questions / problems
问题描述
感谢您花时间阅读本文.我正在尝试使用 pygame 创建一个非常基本的瓷砖游戏系统.我不是 pygame 中的佼佼者,所以我可能会遗漏一些相当明显的东西.到目前为止,我在一个文件中拥有所有内容.我现在所拥有的似乎真的很草率,可能有一种更有效的方法来做到这一点,但现在我只是使用二维数组,其中的数字等于特定类型的瓷砖(草、水等).为此,我使用 numpy,因为这是有人向我推荐的.虽然我不知道我是否喜欢这种方法,因为如果将来我有一些不仅仅是图形的图块,并且有更具体的属性呢?例如宝箱或陷阱?你会如何构建它?
Thanks for taking the time to read this. I am trying to create a very basic tile game system with pygame. I am not the best at pygame, so I may be missing something fairly obvious. So far I have everything in one file. What I have right now seems really sloppy and there is probably a more efficient way of doing it, but right now I am just using 2d Arrays with a number that equates to a specific type of tile, (grass, water, etc). For that I am using numpy because that is what someone recommended to me. Though I don't know if I like this method, because what if in the future I had some tile that was not simply graphical, and had more specific attributes to it? Like a treasure chest for example or a trap? How would you structure this?
但无论如何,我的问题是现在屏幕只是黑色,并且没有绘制草图块.
But none the less, my problem is right now the screen is simply black, and isn't drawing the grass tiles.
代码如下:
import numpy
import pygame
import sys
from pygame.locals import *
pygame.init()
fpsClock = pygame.time.Clock()
windowWi = 800
windowHi = 608
mapWi = 50 # *16 = 800, etc
mapHi = 38
# ----- all of the images ------------------------------
grass1 = pygame.image.load('pictures\(Grass\grass1.png')
#-------------------------------------------------------
screen = pygame.display.set_mode((windowWi, windowHi))
pygame.display.set_caption("Tile Testing!")
gameRunning = True
groundArray = numpy.ones((mapWi,mapHi))
def drawMapArray(maparray):
for x in range(mapWi,1):
for y in range(mapHi,1):
#Determines tile type.
if maparray[y,x] == 1:
screen.blit(grass1, (x*16, y*16))
else:
print "Nothing is here!"
while gameRunning:
drawMapArray(groundArray)
for event in pygame.event.get():
if event.type == "QUIT":
pygame.quit()
sys.exit()
#Updates display and then sets FPS to 30 FPS.
pygame.display.update()
fpsClock.tick(30)
请随时引导我朝着更好的结构方向发展,因为我对游戏设计还很陌生,希望得到任何反馈!
Please feel free to steer me in a better structural direction as I am very new to game design and want any feedback!
谢谢,赖安
我已经尝试过这个,这在逻辑上是有道理的,但我得到了一个索引超出范围的错误.
I have tried this, which makes sense logically but I am getting an index out of range error.
def drawMapArray(maparray):
for x in range(0,mapWi,1):
for y in range(0,mapHi,1):
#Determines tile type.
if maparray[y,x] == 1:
screen.blit(grass1, (x*16, y*16))
else:
print "Nothing is here!"
推荐答案
当您添加更多 tile 类型时可能会更好地扩展的一种解决方案是使用字典从数组中的数字获取图像,例如:
One solution that might scale better as you add more tile types is using a dictionary to get from the numbers in your array to the images, e.g.:
tile_dict = {1 : pygame.image.load('pictures\(Grass\grass1.png'),
2 : pygame.image.load('pictures\rock.png')
}
然后在绘制函数中从字典中绘制相关条目
And then just draw the relevant entry from the dictionary in your draw function
def drawMapArray(maparray):
for x in range(0, mapWi):
for y in range(0, mapHi):
#Determines tile type.
current_tile = tile_dict[maparray[x, y]]
screen.blit(current_tile, (x*16, y*16))
这篇关于使用 pygame 问题/问题的基于磁贴的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!