因此,我正在使用Pygame进行图块/网格寻宝游戏,而我正在努力思考一种实现此功能的方法。因此,每当用户按下空格时,它都会检查一个特定的图块(例如带有船的图块),并使用一个随机数(出于概率),它应该删除所有硬币或不执行任何操作。而且,每当您在图块上使用了空间时,就不应再次使用该图块。所以这些是我目前的问题。
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()
最佳答案
因此,每当用户按下空格时,它都会检查一个特定的图块(例如带有飞船的图块),并使用随机数(出于概率),它应该删除所有硬币或不执行任何操作。[...]==
是比较运算符。因此,inventory[coins] == inventory[coins] - inventory[coins]
将inventory[coins]
与0
(inventory[coins] - inventory[coins]
)进行比较,结果无处可去。
如果您已解决问题,则while
循环将执行10次检查。硬币丢失的可能性非常高:
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
定义一个百分比(
percent
)值,该值定义硬币没有丢失,并检查随机值是否超出限制:percent = 30 # coins are kept with a probability of 30 percent and will be lost by 70 percent
ran1 = random.randint(0,100) # random value in [0, 99]
if ran1 >= percent:
inventory[coins] = 0
[...]每当您在图块上使用了空间时,就不应再次使用该图块。
创建一个字典,存储有关“用途”磁贴的信息:
visit = {}
如果按下空格,则将磁贴标记为“二手”:
pos = (playerPos[1], playerPos[0])
visit[pos] = True
检查以下位置之前是否已经“使用”了图块:
pos = (playerPos[1], playerPos[0])
if not pos in visit:
# [...]
顺便说一下,使用
pygame.Surface.fill()
清除每帧开始处的显示。displaysurf.fill(black) # choose background color
主循环代码清单:
visit = {}
while True:
displaysurf.fill(black) # choose background color
#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:
pos = (playerPos[1], playerPos[0])
if not pos in visit:
visit[pos] = True
currentTile = tilemap[playerPos[1]][playerPos[0]]
if currentTile == rock:
percent = 30 # coins are kept with a probability of 30 percent and will be lost by 70 percent
ran1 = random.randint(0,100) # random value in [0, 99]
if ran1 >= percent:
inventory[coins] = 0
elif currentTile == ship:
inventory[coins] += 50
inventory[movesMade] += 1
# [...]
关于python - 如何在pygame程序中实现此功能?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54427344/