Re: จะตรวจจับและจบเกมได้อย่างไรเมื่อมี pygame sprite วิ่งเข้าที่หางของมัน?

ฉันสร้างเกมที่มีลักษณะคล้ายงูโดยให้ผู้ใช้เคลื่อนสไปรต์ และสไปรต์ก็ทิ้งร่องรอยไว้ หากผู้ใช้วิ่งเข้าไปในเส้นทางที่เขาสร้างขึ้น ฉันอยากให้เกมจบลงและผู้เล่นคนนั้นจะต้องแพ้

แนวคิดหนึ่งที่ฉันมีคือติดตามตำแหน่งที่ผ่านมาของสไปรท์ (อาจอยู่ในรายการ) จากนั้นสร้างคำสั่ง if ที่จะนำไปสู่การแพ้ในเกม (อย่างไรก็ตาม ฉันยังไม่แน่ใจเล็กน้อยว่าต้องทำอย่างไร)

ฉันได้รับคำตอบสำหรับคำถามนี้ซึ่งเขียนโค้ดสำหรับรายการนี้:

"ฉันคิดว่าคุณสามารถประกาศรายการสองมิติได้ดังนี้: pastPositions = [[400, 300]] จากนั้นทุกครั้งที่ตำแหน่งของผู้เล่นย้าย ให้ตรวจสอบรายการ:
สำหรับแถวใน pastPositions: If (player.rect.x == pastPositions[row][0] and player.rect.y == pastPositions[row][1]): done = true # game over หากผู้เล่นยังไม่ได้ไปที่นั่น ให้เพิ่มตำแหน่งนั้นไปที่ รายการ pastPositions.append([player.rect.x, player.rect.y])

ดูเหมือนว่าจะใช้งานได้ แต่เมื่อฉันพยายามเรียกใช้โค้ด (ใน Python Interactive) ฉันได้รับข้อความแสดงข้อผิดพลาดที่อ่านว่า: "line 86, in <module> if player.rect.x == astPositions[row][0] and player.rect.y == pastPositions[row][1]: IndexError: list index out of range"

คุณจะแนะนำให้ฉันเปลี่ยนช่วงเพื่อไม่ให้สิ่งนี้เกิดขึ้นได้อย่างไร ฉันพยายามตั้งค่าเป็นความกว้างและความสูงของหน้าต่าง pygame

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class Player(pygame.sprite.Sprite):

   def __init__(self, x, y):
       super().__init__()

       self.image = pygame.Surface([15, 15])
       self.image.fill(WHITE)

       self.rect = self.image.get_rect()
       self.rect.x = x
       self.rect.y = y

       self.change_x = 0
       self.change_y = 0


   def changespeed(self, x, y):
       self.change_x += x
       self.change_y += y


   def update(self):
       self.rect.x += self.change_x
       self.rect.y += self.change_y

pygame.init()

screen = pygame.display.set_mode([800, 600])

pygame.display.set_caption('The Etch-a-Sketch Game')

myfont = pygame.font.SysFont('Times', 20)
textsurface = myfont.render('This is the Etch-a-Sketch Game', False, (255, 255, 255))
screen.blit(textsurface,(0,0))

myfont = pygame.font.SysFont('Times', 15)
textsurface = myfont.render('Feel free to draw, but if you cross your own    path, you will die.', False, (255, 255, 255))
screen.blit(textsurface,(0,20))


player = Player(400, 300)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(player)

clock = pygame.time.Clock()
done = False

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, -3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 3)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(3, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(-3, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, 3)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, -3)

    player.update()

    all_sprites_list.draw(screen)

    pygame.display.flip()
    clock.tick(75)

pygame.quit ()

person Gal    schedule 14.01.2019    source แหล่งที่มา


คำตอบ (1)


การบันทึกจุดต่างๆ ของเส้นทางที่ยาวมากดูเหมือนจะเป็นวิธีที่ช้าในการตรวจสอบการชนหางของตัวเอง

หากพื้นหลังเป็นสีที่รู้จัก อาจตรวจสอบสีพิกเซลที่ไม่ใช่พื้นหลังในพื้นที่ที่โปรแกรมเล่นกำลังจะย้ายเข้าไป

เป็นไปได้ที่จะคว้าสีพิกเซลเดียวด้วย Surface.get_at( ) ฟังก์ชัน ซึ่งจะส่งคืนสี RGBA โดยสามารถเปรียบเทียบสีนี้กับสีพื้นหลังเดียวได้ (หรือสีพื้นหลังที่รู้จักจากบิตแมป ณ จุดนั้น) ทำให้สามารถตัดสินใจการชนกันได้

ใน player.update():

if (screen.get_at((player_next_x, player_next_y)) != BACKGROUND_COLOUR):
    # player hit tail

แน่นอนว่าอาจจำเป็นต้องตรวจสอบสไปรต์ของผู้เล่นทั้งสองมุม (ในทิศทางการเคลื่อนที่)

person Kingsley    schedule 15.01.2019
comment
ขอบคุณมากสำหรับคำตอบของคุณ! ดูเหมือนว่าจะใช้งานได้ แต่ฉันมีคำถามสองสามข้อ: - person Gal; 15.01.2019
comment
#1 - คุณจะกำหนด (player_next_x) และ (player_next_y) ได้อย่างไร เมื่อคำนึงถึงอินพุตของผู้ใช้บนปุ่มทิศทาง - person Gal; 15.01.2019
comment
#2 - ฉันถามเพราะฉันลองใช้ (player.rect.x) และ (player.rect.y) ซึ่งฉันได้กำหนดไว้แล้ว และแน่นอนว่าระบบปิดทันทีเพราะตรวจพบการชนกับตำแหน่งปัจจุบัน - person Gal; 15.01.2019
comment
เมื่อโค้ดกำลังจะดึงตำแหน่งผู้เล่นคนถัดไป มันจะใช้พิกัดเหล่านี้ ดังนั้นเพียงตรวจสอบก่อนที่จะวาดภาพบนบิตแมป - person Kingsley; 16.01.2019