如何在我的Python乌龟迷宫游戏中添加碰撞检测

问题描述 投票:0回答:1

在我的 Python 迷宫游戏中,我需要为墙壁添加碰撞检测,这样玩家就不会直接越过墙壁。我尝试了很多方法,但似乎都不起作用。

我认为使用数组来检测碰撞是可能的,但我不知道如何做到这一点,因为我对 Python 和海龟特别陌生。

import turtle
import random
import time

print("TURTLE MAZE")


player = turtle.Turtle()
player.penup()
player.speed(0)
player.shape("square")
player.pensize(5)
player.color('red')
player.pencolor('white')
player.goto(-210,-210)

screen = turtle.Screen()
screen.title("Maze ")
screen.bgcolor("White")
screen.setup(width=1000, height=600)


grid = [
    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ,2, 2 ,2, 2 ,2, 2, 2, 2, 2, 2, 2, 2, ],
    [2, 3, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 ,0, 0 ,0, 0 ,0, 0, 0, 1, 0, 1, 3, 2, ],
    [2, 0, 1, 1, 0, 0, 0, 1, 0, 8, 0, 0 ,1, 1 ,0, 1 ,0, 1, 0, 1, 0, 0, 0, 2, ],
    [2, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1 ,1, 0 ,0, 1 ,0, 1, 1, 1, 0, 1, 0, 2, ],
    [2, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0 ,1, 0 ,1, 1 ,0, 0, 0, 1, 0, 8, 0, 2, ],
    [2, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1 ,1, 0 ,0, 1 ,0, 1, 0, 0, 0, 1, 1, 2, ],
    [2, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0 ,0, 0 ,1, 1 ,0, 1, 1, 1, 0, 0, 0, 2, ],
    [2, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1 ,1, 1 ,1, 0 ,0, 0, 0, 1, 0, 1, 0, 2, ],
    [2, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 ,1, 0 ,1, 1 ,1, 1, 0, 1, 1, 1, 8, 2,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, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 ,0, 1 ,0, 1, 0, 1, 0, 0, 3, 2, ],
    [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ,2, 2 ,2, 2 ,2, 2, 2, 2, 2, 2,  ]
    ]

tiles = ['empty', 'wall']

pen = turtle.Turtle()
pen.penup()
pen.speed(0)
pen.shape("square")
pen.setundobuffer(None)

def draw_grid(pen, grid):
    pen.clear()
    top = 230
    left = -230
    
    colors = ["black", "lightblue", "blue", "orange", "yellow", "green", "purple", "red","sky blue"]
    
    for y in range(len(grid)):
        for x in range(len(grid[0])):
            screen_x = left + (x * 20)
            screen_y = top - (y * 20)
            color_number = grid[y][x]
            color = colors[color_number]
            pen.color(color)
            pen.goto(screen_x, screen_y)
            pen.stamp()

draw_grid(pen, grid)

class Movement:
    def movePlayerRight():
        x = player.xcor()
        x += 20
        player.setx(x)
        print(x)

    def movePlayerLeft():
        x = player.xcor()
        x -= 20
        player.setx(x)
        print(x)
    def movePlayerUp():
        y = player.ycor()
        y += 20
        player.sety(y)
        print(y)

    def movePlayerDown():
        y = player.ycor()
        y -= 20
        player.sety(y)
        print(y)


    screen.listen()
    screen.onkeypress(movePlayerRight, "Right")
    screen.onkeypress(movePlayerLeft, "Left")
    screen.onkeypress(movePlayerUp, "Up")
    screen.onkeypress(movePlayerDown, "Down")

Movement()

while True:
    screen.update()
    if player.xcor() < -210:
        player.setx(player.xcor() + 20)
        print("Nuh uh, Dont try go outside...")
    if player.xcor() > 210:
        player.setx(player.xcor() - 20)
        print("Nuh uh, Dont try go outside...")
    if player.ycor() < -210:
        player.sety(player.ycor() + 20)
        print("Nuh uh, Dont try go outside...")
    if player.ycor() > 210:
        player.sety(player.ycor() - 20)
        print("Nuh uh, don't try go outside...")
going = True
while going == True:
  screen.update()
python collision-detection python-turtle
1个回答
0
投票

您可以使用此功能:

def isCollided(col, self, radius):
    return abs(col.xcor() - self.xcor()) < radius and abs(col.ycor() - self.ycor()) < radius

如果“col”在“self”的“半径”内,则返回 True

使用示例:

#Python

from turtle import Turtle, Screen
import keyboard

#setup

screen = Screen()

screen.screensize(250, 250)
screen.title("collision test")

t1 = Turtle("turtle")
t2 = Turtle("turtle")

t1.goto(-100, -100)
t2.goto(100, 100)


def isCollided(col, self, radius): #isCollided function
    return abs(col.xcor() - self.xcor()) < radius and abs(col.ycor() - self.ycor()) < radius

#Main Loop

while True:
    if keyboard.is_pressed("right"): #move right it right arrow is pressed
        t1.goto(t1.xcor()+2, t1.ycor())
    if keyboard.is_pressed("up"): #etc
        t1.goto(t1.xcor(), t1.ycor()+2)
    if keyboard.is_pressed("left"): 
        t1.goto(t1.xcor()-2, t1.ycor())
    if keyboard.is_pressed("down"):
        t1.goto(t1.xcor(), t1.ycor()-2)
    

    if isCollided(t2, t1, 10): #check if t1 is within 10 pixels of t2
        print("It works!")     #If so, print "it works"

#INSTRUCTIONS:
#Press the arrow keys to move one turtle
#If you are within 10 pixels of the other turtle, the console will print "It works!"
#You can use this as a simple collision test
© www.soinside.com 2019 - 2024. All rights reserved.