การแสดงภาพแบบสุ่มในเกม Python ด้วย Pygame
ฉันกำลังเล่นเกมเขาวงกตนักแสดงหลักต้องหาเส้นทางโดยการย้ายไปมาระหว่างกำแพงเพื่อไปยังทางออก
ฉันทำส่วนหนึ่งของโครงการ แต่ฉันไม่สามารถแสดงวัตถุ (เข็มฉีดยาเข็มและหลอดพลาสติก) แบบสุ่ม (ต้องเปลี่ยนตำแหน่งเมื่อเริ่มต้นแต่ละครั้ง) ในเขาวงกตจากนั้นจึงจะหยิบและแสดงตัวนับซึ่ง จะแสดงรายการที่รวบรวม
ฉันต้องแก้ไขฟังก์ชัน GENERATE ของฉันในลูปที่ผ่านไฟล์ข้อความของฉันฉันต้องดึงช่องว่าง (สไปรต์ == 0) ใส่ไว้ในรายการจากนั้นใช้การสุ่มที่ฉันคิดว่าจะดึง 3 ตำแหน่งแบบสุ่มสำหรับแต่ละตำแหน่ง วัตถุ ตัวอย่างเช่นสำหรับตำแหน่งสุ่มสามตำแหน่งวัตถุเข็มฉีดยาที่เก็บไว้ในรายการฉันต้องแทนที่ (สไปรต์ == 0) โดย (สไปรต์ == s), 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
หลังจากนั้นฉันต้องหาวิธีเปรียบเทียบตำแหน่ง (x และ y) ของวัตถุ (เช่นเข็มฉีดยา) กับตำแหน่งปัจจุบันของตัวละครหลักและถ้าทั้งสองเท่ากันฉันก็บอกได้ว่าอักขระนั้นอยู่ตรง จุด. 'วัตถุ.
นี่คือปัญหา mn ฉันหวังว่าฉันจะอธิบายได้ดี
ขอขอบคุณ
คำตอบ
สำหรับการวางเข็มฉีดยา (3) แบบสุ่มให้ลองใช้ตรรกะนี้:
- นับช่องว่างทั้งหมดในเขาวงกต
- หารจำนวนพื้นที่ด้วย 3
- ในแต่ละสามวางเข็มฉีดยาในตำแหน่งสุ่ม
สิ่งนี้ควรทำให้ตำแหน่งเข็มฉีดยาเป็นแบบสุ่ม แต่กระจายอย่างสม่ำเสมอ
นี่คือรหัสที่อัปเดต ยังไม่ผ่านการทดสอบดังนั้นคุณอาจต้องปรับเปลี่ยนบ้าง
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
สำหรับส่วนอื่น ๆ ของโพสต์การตรวจสอบว่าผู้เล่นพบเข็มฉีดยาหรือไม่ฉันไม่รู้ว่าคุณกำลังเคลื่อนย้ายเครื่องเล่นอย่างไรฉันจึงได้ แต่เดา สมมติว่าผู้เล่นมีพิกัด (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)