首页 文章

使用pygame问题/问题的基于平铺的游戏

提问于
浏览
2

感谢您抽时间阅读 . 我正在尝试用pygame创建一个非常基本的平铺游戏系统 . 我不是pygame中最好的,所以我可能会遗漏一些相当明显的东西 . 到目前为止,我把所有东西都放在一个档我现在看起来真的很草率,可能有一种更有效的方法,但是现在我只使用2d Arrays,其数字相当于特定类型的瓷砖(草,水等) . 为此,我使用numpy,因为这是有人推荐给我的 . 虽然我不知道我是否喜欢这种方法,因为如果将来我有一些不仅仅是图形化的瓷砖,并且具有更多特定的属性呢?比如宝箱或者陷阱?你会如何构建这个?

但无论如何,我的问题是现在屏幕只是黑色,并没有绘制草砖 .

这是代码:

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

2 回答

  • 1

    添加更多图块类型时可能更好地扩展的一种解决方案是使用字典从数组中的数字到图像,例如:

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

    你的绘制方法是错误的 .

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

    第一个错误是 for x in range(mapWi,1) .

    看看range函数 . 你正在使用两个参数,所以你从 mapWi 循环到 1 ,这不是你想要的 .

    你想从 0 循环到 mapWi ,所以你必须使用

    for x in range(mapWi):
        for y in range(mapHi):
    

    (使用 xrange 会更好,但这只是一个非常小的改进)

    否则,屏幕上不会显示任何内容 .


    第二个错误是这一行:

    if maparray[y,x] == 1:
    

    你会得到一个 IndexError 因为你混淆了数组的初始化 . 它实际上是 mapWi 高和 mapHi 宽 . 所以,你应该使用它来进行初始化

    groundArray = numpy.ones((mapHi,mapWi))
    

    代替

    groundArray = numpy.ones((mapWi,mapHi))
    

    为了说明它,只需要一点点测试:

    >>> numpy.ones((10,5))
    array([[ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.],
           [ 1.,  1.,  1.,  1.,  1.]])
    >>>
    

    你会看到使用 (10, 5) 给我们一个 height = 10width = 5 的数组 .


    图片的 Headers 说明:

    for event in pygame.event.get():
        if event.type == "QUIT":
    

    什么都不做 event.type 永远不是字符串 "QUIT" . 退出事件的类型是 12 ,或更好: pygame.QUIT ;所以它应该是:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
    

    像这样重写你的主循环:

    while gameRunning:
        drawMapArray(groundArray)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameRunning = False
                break
    
        #Updates display and then sets FPS to 30 FPS. 
        pygame.display.update()
        fpsClock.tick(30)
    
    pygame.quit()
    

    避免调用 sys.exit .

    更好:将主循环分成通常在主循环中执行的三个步骤 .

    while gameRunning:
        draw()         # do all the drawing stuff in this function
        handle_input() # handle all input stuff in this function
        update()       # update the game state in this function
    

相关问题