python - 使用 pygame 的基于图 block 的游戏问题/问题

标签 python pygame

感谢您花时间阅读本文。我正在尝试使用 pygame 创建一个非常基本的瓷砖游戏系统。我不是最擅长 pygame,所以我可能会遗漏一些相当明显的东西。到目前为止,我将所有内容都保存在一个文件中。我现在所拥有的看起来真的很草率,可能有一种更有效的方法来做到这一点,但现在我只是使用二维数组,其数字等于特定类型的瓷砖(草,水等)。为此,我使用 numpy,因为这是有人向我推荐的。虽然我不知道我是否喜欢这种方法,因为如果将来我有一些不仅仅是图形的图 block ,并且具有更具体的属性怎么办?例如,像一个宝箱或一个陷阱?您将如何构建这个?

但尽管如此,我的问题是现在屏幕只是黑色的,并且没有绘制草 block 。

代码如下:

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!"

最佳答案

当您添加更多图 block 类型时,一种可以更好地扩展的解决方案是使用字典从数组中的数字获取图像,例如:

tile_dict = {1 : pygame.image.load('pictures\(Grass\grass1.png'),
             2 : pygame.image.load('pictures\rock.png')
            }

然后只需在绘图函数中从字典中绘制相关条目即可

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))

关于python - 使用 pygame 的基于图 block 的游戏问题/问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11874952/

相关文章:

python - pygame中移动 Sprite 之间的碰撞

python - 执行事件时 PyGame 代码无法正确执行

python - 您可以同时拥有两个具有不同 FPS 值的函数吗?

python - 如何分别移动列表中多个相同的矩形?

python - Pandas 数据帧 : to_dict() poor performance

python - 在 while 循环中将变量从 int 转换为 str?

python - 按值属性对字典进行排序

python - 商品 future 分层数据结构

python - 如何像计算机科学家一样思考

python - 从列表中绘制的对象不会停留在屏幕上(pygame)