gpt4 book ai didi

python - 使用 Pygame 在 Python 游戏中随机显示图像

转载 作者:行者123 更新时间:2023-12-05 07:02:06 25 4
gpt4 key购买 nike

我正在制作一个迷宫游戏,主要 Actor 必须通过在墙壁之间移动来找到一条路径以进入导出。

我做了一部分项目,只是我不能在迷宫中随机显示物体(注入(inject)器、针头和塑料管)(它们必须在每次开始时改变位置),然后拿起并显示一个计数器,它将列出收集的项目。

我必须修改我的 GENERATE 函数,在遍历我的文本文件的循环中,我必须检索空白空间 (sprite == 0),将它们放入列表中,然后使用我想象的随机数来检索 3 个随机数每个对象的位置。例如,对于存储在列表中的注入(inject)器对象的三个随机位置,我必须将 (sprite == 0) 替换为 (sprite == s), s = syringe.所以最后我会在 show 函数中使用三个 s 位置来进行显示。

    def generer(self):
"""Method for generating the start based on the file.
we create a general list, containing one list per line to display"""
# We open the file
with open(self.file, "r") as file:
structure_level = []
# We browse the lines of the file
for line in file:
line_level = []
# We browse the sprites (letters) contained in the file
for sprite in line:
# We ignore the end of line "\ n"
if sprite != '\n':
# We add the sprite to the list of the line
line_level.append(sprite)
# Add the line to the level list
structure_level.append(line_level)
# We save this structure
self.structure = structure_level

def show(self, window):
"""Méthode permettant d'afficher le niveau en fonction
de la liste de structure renvoyée par generer()"""
# Chargement des images (seule celle d'arrivée contient de la transparence)
wall = pygame.image.load(wall_image).convert()
departure = pygame.image.load(departure_image).convert_alpha()
arrived = pygame.image.load(Gardien_image).convert_alpha()
syringe = pygame.image.load(syringe_image).convert_alpha()

# We go through the list of the level
number_line = 0
for line in self.structure:
# On parcourt les listes de lignes
num_case = 0
for sprite in line:
# We calculate the real position in pixels
x = num_case * sprite_size
y = number_line * sprite_size
if sprite == 'w': # w = Wall
window.blit(wall, (x, y))
elif sprite == 'd': # d = Départure
window.blit(departure, (x, y))
elif sprite == 'a': # a = Arrived
window.blit(arrived, (x, y))
elif sprite == 's': # s = syringe
window.blit(syringe, (x, y))

num_case += 1
number_line += 1

之后我必须找到一种方法来比较对象(例如注入(inject)器)的位置(x 和 y)与主角的当前位置,如果两者相等,那么我可以说角色正是当场。 '对象。

这里是mn问题,希望我解释的很好。

谢谢

最佳答案

对于随机放置 (3) 个注入(inject)器,请尝试以下逻辑:

  • 数一数迷宫中的所有空间
  • 将空格数除以 3
  • 每隔三分之一,将注入(inject)器放在随机位置

这应该使注入(inject)器位置保持随机,但分布均匀。

这是更新后的代码。它未经测试,因此您可能需要对其进行一些调整。

def generer(self):
"""Method for generating the start based on the file.
we create a general list, containing one list per line to display"""
import random
zerocnt = 0 # counter for empty spaces
# We open the file
with open(self.file, "r") as file:
structure_level = []
# We browse the lines of the file
for line in file:
line_level = []
# We browse the sprites (letters) contained in the file
for sprite in line:
# We ignore the end of line "\ n"
if sprite != '\n':
# We add the sprite to the list of the line
line_level.append(sprite)
if sprite == 0: zerocnt += 1 # another space
# Add the line to the level list
structure_level.append(line_level)

# 3 syringes, distribute evenly
div3 = zerocnt//3 # divide empty spaces by 3
# generate 3 positions, 1 syringe per third of maze
poslst = [random.randint(i*div3, (i+1)*div3-1) for i in [0,1,2]]
ctr=0
# scan empty spaces, update 3 spots
for lvl in structure_level:
for i in range(len(lvl)):
if ctr in poslst:
lvl[i]='s' # put syringe here
ctr += 1

# We save this structure
self.structure = structure_level

帖子的另一部分,检查玩家是否找到了注入(inject)器,我不知道你是如何移动玩家的,所以我只能猜测。假设玩家有一个 (x,y) 坐标,您可以检查该位置是否在数组中有一个 's'。

尝试这样的事情:

Player.X = 5
Player.Y = 5
if self.structure[Player.X][Player.Y] == 's':
print("Found syringe")
Player.Syringes += 1
self.structure[Player.X][Player.Y] = 0 # remove syringe from maze (or set to player)

关于python - 使用 Pygame 在 Python 游戏中随机显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63664947/

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