python的pygame制作的2D我的世界

这是本人去年按照一个英文教程制作的一个2D简单版本的Minecraft。代码非本人风格。今天看来,这代码可读性不好。仅以发表出来,以供有需要的人阅读。

"""游戏规则:
按空格键捡东西,按数字键放东西,按数字键同时单击鼠标左键合成物质
"""

import random
import pygame,sys
from pygame.locals import *

BLACK = (0,0,0)
BROWN = (153,76,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
WHITE = (255,255,255)

cloudx = -200
cloudy = 0

DIRT = 0
GRASS = 1
WATER = 2
COAL = 3
CLOUD = 4
WOOD = 5
FIRE = 6
SAND = 7
GLASS = 8
ROCK  = 9
STONE = 10
BRICK = 11
DIAMOND = 12

textures = {
           DIRT : pygame.image.load('dirt.png'),
           GRASS: pygame.image.load('grass.png'),
           WATER: pygame.image.load('water.png'),
           COAL : pygame.image.load('coal.png'),
           CLOUD: pygame.image.load('cloud.png'),
           WOOD : pygame.image.load('wood.png'),
           FIRE : pygame.image.load('fire.png'),
           SAND : pygame.image.load('sand.png'),
           GLASS: pygame.image.load('glass.png'),
           ROCK : pygame.image.load('rock.png'),
           STONE: pygame.image.load('stone.png'),
           BRICK: pygame.image.load('brick.png'),
           DIAMOND:pygame.image.load('diamond.png')
          }

inventory = {
               DIRT : 0,
               GRASS: 0,
               WATER: 0,
               COAL : 0,
               WOOD : 0,
               FIRE : 0,
               SAND : 0,
               GLASS: 0,
               ROCK : 0,
               STONE: 0,
               BRICK: 0,
               DIAMOND:0
            }

TILESIZE = 20
MAPWIDTH = 50
MAPHEIGHT = 20

PLAYER = pygame.image.load("player.png")
playerPos = [0,0]

resource = [DIRT,GRASS,WATER,COAL,WOOD,FIRE,SAND,GLASS,ROCK,STONE,BRICK,DIAMOND]

tilemap = [ [DIRT for w in range(MAPWIDTH)] for h in range(MAPHEIGHT) ]

for rw in range(MAPHEIGHT):
    for cl in range(MAPWIDTH):
        randomNumber = random.randint(0,20)
        if randomNumber == 0:
            tile = COAL
        elif randomNumber ==1 or randomNumber < 5:
            tile = WATER
        elif randomNumber >=5 and randomNumber <=6:
            tile = SAND
        elif randomNumber > 6 and randomNumber <9:
            tile = WOOD            
        elif randomNumber > 9 and randomNumber <11:
            tile = ROCK
        elif randomNumber > 11 and randomNumber <18:
            tile = GRASS          
        else:
            tile = DIRT
        tilemap[rw][cl] = tile
        
controls = {
             DIRT   : 49,           # 1键
             GRASS  : 50,
             WATER  : 51,
             COAL   : 52,
             WOOD   : 53,
             FIRE   : 54,
             SAND   : 55,           # 7键
             GLASS  : 56,           # 8键 
             ROCK   : 57,           # 9键
             STONE  : 48,           # 0键
             BRICK  : 45,           # -键 
             DIAMOND: 61            # =键
            }

"合成规则"
craft = {
           FIRE : { WOOD :2,ROCK : 2 }, # 两块木片和2块岩石合成一把火
           STONE: { ROCK :2},           
           BRICK : { STONE:2,FIRE : 1},
           SAND : { ROCK : 2 },
           GLASS: { FIRE :1,SAND : 2 },
           DIAMOND:{ WOOD:2,COAL : 3,GLASS:2 } 
         }
    
pygame.init()
DISPLAYSURF = pygame.display.set_mode((MAPWIDTH*TILESIZE,MAPHEIGHT*TILESIZE +50 ))

INVFONT = pygame.font.Font('FreeSansBold.ttf',18)
fpsClock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_RIGHT and playerPos[0] < MAPWIDTH  - 1:
                playerPos[0] +=1
            if event.key == K_LEFT and playerPos[0] > 0 :
                playerPos[0] -=1
            if event.key == K_UP and playerPos[1]>0:
                playerPos[1] -=1
            if event.key == K_DOWN and playerPos[1] < MAPHEIGHT -1 :
                playerPos[1] +=1
            if event.key == K_SPACE:                           # 按空格键捡东西
                currentTile = tilemap[playerPos[1]][playerPos[0]]
                inventory[currentTile] +=1
                tilemap[playerPos[1]][playerPos[0]] = DIRT     # 捡到东西后,变成地面了
                
            for key in controls:
                
                if (event.key == controls[key]):                    

                    if pygame.mouse.get_pressed()[0] :
                        print("单击左键")
                        if key not in craft :break             # 按的键代表某物质,不在合成表里则不合成
                        canBeMade = True                       # 假设能被合成
                        for i in craft[key]:
                            if craft[key][i] > inventory[i]:   # 合成所需的数量大于仓库中数量
                                canBeMade = False              # 无法合成
                                break
                        if canBeMade == True:                  # 如果能被合成
                            for i in craft[key]:               # 仓库中的每项都根据规则减去相应的数量
                                inventory[i] -= craft[key][i]  #
                            inventory[key] += 1                # 合成的这种物质数量加1 
                    else:
                        "按数字键放东西在史蒂夫所在的位置"
                        currentTile = tilemap[playerPos[1]][playerPos[0]]
                        if inventory[key] > 0:                 # 如果仓库中这种物质数量大于0
                            inventory[key] -= 1                # 那么数量减去1 
                            tilemap[playerPos[1]][playerPos[0]] = key  # 放到史蒂夫所在的位置
                            inventory[currentTile] += 1                # 仓库中相应数量加1

                    
    DISPLAYSURF.fill(BLACK)           
    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 resource:
        DISPLAYSURF.blit(textures[item],(placePosition,MAPHEIGHT*TILESIZE + 20))
        placePosition +=30
        textObj = INVFONT.render(str(inventory[item]),True,WHITE,BLACK)
        DISPLAYSURF.blit(textObj,(placePosition,MAPHEIGHT*TILESIZE+20))
        placePosition +=50
        
    pygame.display.update()
    fpsClock.tick(24)