感謝您花時間閱讀本文。我正在嘗試用pygame創建一個非常基本的遊戲系統。我不是pygame最好的,所以我可能會錯過一些相當明顯的東西。到目前爲止,我已將所有內容都放在一個文件中我現在所看到的似乎很渺茫,可能有更高效的方法,但現在我只是使用2d數組,其數字等同於特定類型的瓦片(草,水等)。爲此,我正在使用numpy,因爲這是有人推薦給我的。雖然我不知道我是否喜歡這種方法,因爲如果將來我有一些不是簡單的圖形,而且具有更多特定的屬性呢?像寶箱一樣或陷阱?你將如何構造這個?使用pygame問題/問題的基於Tile的遊戲
但無論如何,我的問題是現在的屏幕只是黑色的,並沒有繪製草地磚。
下面是代碼:
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)
請隨時引導我,因爲我很新的遊戲設計,並希望任何反饋,更好的結構方向!
感謝, 瑞安
編輯: 我曾經嘗試這樣做,這是有道理的邏輯,但我得到一個索引超出範圍的錯誤。可能會擴展爲您增加更多的瓷磚類型更好
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!"
你的意思是有一個「(」文件名'圖片\(草\ grass1.png'? – 2012-08-09 00:02:01
我想你'範圍()'調用可能是走錯了路周圍,試圖爲' x在範圍內(1,mapWi):'。 – Marius 2012-08-09 00:04:43
這就是如何命名文件哈哈,這些只是一些開源的16位瓦片圖像,我用於測試目的,我很快就會改變它,因爲它看起來很混亂 – Ryan 2012-08-09 00:07:02