gpt4 book ai didi

python - 我怎样才能去除一些瓷砖纹理的黑色

转载 作者:太空宇宙 更新时间:2023-11-04 02:07:07 24 4
gpt4 key购买 nike

所以我的程序是一个寻宝游戏,它使用一组图 block 作为游戏 map ,在这些图 block 上我使用 PNG 照片作为纹理,但是当使用没有完全占据图 block 的 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()

最佳答案

如果你想让纹理的黑色部分呈现黑色,那么你必须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)

关于python - 我怎样才能去除一些瓷砖纹理的黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54428774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com