如何删除某些平铺纹理的黑色

时间:2019-01-29 20:02:16

标签: python pygame

因此,我的程序是一款寻宝游戏,该游戏使用一组图块作为游戏地图,在这些图块上,我使用PNG照片作为纹理,但是当使用不完全占据图块的PNG照片时,黑色会覆盖剩下的瓷砖。我只是想知道如何删除黑色并将一个纹理文件放在另一个纹理文件上。

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

pygame.display.set_caption('My first game')

#colours
black = (0, 0, 0)
white = (255, 255, 255)
brown = (153, 76, 0)
blue  = (0, 0, 255)
grey  = (192,192,192)

#game dimensions
tilesize = 20
mapwidth = 30
mapheight = 20


coins = 0
ship = 1
water = 2
rock = 3
movesMade = 4

#dictionary for texture of the map
textures = { #the transform function scales the photo to the tile size
    ship : pygame.transform.smoothscale(pygame.image.load('ship.png'), (tilesize, tilesize)),
    water: pygame.transform.smoothscale(pygame.image.load('water.png'), (tilesize, tilesize)),
    rock: pygame.transform.smoothscale(pygame.image.load('rock.png'), (tilesize, tilesize)),
    coins: pygame.transform.smoothscale(pygame.image.load('chest.png'), (tilesize, tilesize)),
    movesMade: pygame.transform.smoothscale(pygame.image.load('player.png'), (tilesize, tilesize))
    }

inventory = {
    coins: 0,
    ship: 0,
    rock: 0,
    movesMade: 0
    }

#image that will represent player
PLAYER = pygame.transform.smoothscale(pygame.image.load('player.png'), (tilesize, tilesize))

#position of the player
playerPos = [0,0]

resources = [coins, movesMade]

#utilise list comprehension to create grid
tilemap = [[water for w in range(mapwidth)] for h in range(mapheight)]

pygame.init()

#set up display
displaysurf = pygame.display.set_mode((mapwidth*tilesize,mapheight*tilesize + 60))

invfont = pygame.font.Font('FreeSansBold.ttf', 18)

#loops through each row
for rw in range(mapheight):
    for cl in range(mapwidth):
        randomnumber = random.randint(0,15)
        if randomnumber == 0 or randomnumber == 1:
            tile = rock
        elif randomnumber == 2 or randomnumber == 3 :
            tile = ship
        else:
            tile = water
        #sets position in the grid
        tilemap[rw][cl] = tile

while True:
    #user events
    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]]
                if currentTile == rock:
                    p = 10
                    while p > 0:
                        ran1 = random.randint(0,p)
                        if ran1 % 2 == 0:
                            inventory[coins] == inventory[coins] - inventory[coins]
                            p = p - 1
                        else:
                            p = p - 1

                elif currentTile == ship:
                    inventory[coins] += 50
                    inventory[movesMade] += 1

    #loops through each row 
    for row in range(mapheight):
        #loops through each column in row
        for column in range(mapwidth):
            displaysurf.blit(textures[tilemap[row][column]], (column*tilesize,row*tilesize))

    displaysurf.blit(PLAYER,(playerPos[0]*tilesize,playerPos[1]*tilesize))

    placePosition = 10
    for item in resources:
        displaysurf.blit(textures[item],(placePosition, mapheight*tilesize + 20))
        placePosition += 30
        #text displays amount of coin
        textObj = invfont.render(str(inventory[item]), True, white, black)
        displaysurf.blit(textObj,(placePosition, mapheight*tilesize + 20))
        placePosition += 50

    #if inventory[coins] > 100:
    #    break
    pygame.display.update()

1 个答案:

答案 0 :(得分:2)

如果希望纹理的黑色部分显示为黑色,则必须convert()表面,然后通过set_colorkey()设置透明色键。

为此,必须先通过pygame.init()初始化pygame,并通过pygame.display.set_mode设置视频模式:

textures = { #the transform function scales the photo to the tile size
    ship : pygame.transform.smoothscale(pygame.image.load('ship.png'), (tilesize, tilesize)),
    water: pygame.transform.smoothscale(pygame.image.load('water.png'), (tilesize, tilesize)),
    rock: pygame.transform.smoothscale(pygame.image.load('rock.png'), (tilesize, tilesize)),
    coins: pygame.transform.smoothscale(pygame.image.load('chest.png'), (tilesize, tilesize)),
    movesMade: pygame.transform.smoothscale(pygame.image.load('player.png'), (tilesize, tilesize))
    }

PLAYER = pygame.transform.smoothscale(pygame.image.load('player.png'), (tilesize, tilesize))

# [...]

pygame.init()
displaysurf = pygame.display.set_mode((mapwidth*tilesize,mapheight*tilesize + 60))

PLAYER = PLAYER.convert()
PLAYER.set_colorkey(black)

for k in (coins, ship, water, rock, movesMade):
    textures[k] = textures[k].convert()
    textures[k].set_colorkey(black)