简体   繁体   中英

Collision in python Turtle Snake game

I'm writing a code where there is a maze that a turtle has to get through. The only issue is that I can't figure out how to create collision with all of the walls. I have been incapable of finding a way of creating barriers without making limits for every coordinate of the wall. I only have access to the turtle and random modules. Is there a way to do this? Here is my code:

import turtle

screen = turtle.Screen()
tr=turtle.Turtle()
screen.tracer(0)

mazeWidth=150
tr = turtle.Turtle()
tr.width(5)
tr.speed(0)
tr.penup()
tr.goto(-mazeWidth,190)
def drawMazeSection(color):
  tr.pencolor("lightblue")
  tr.penup()
  tr.goto(-20,200)
  tr.pendown()
  tr.goto(20,200)
  tr.penup()
  tr.goto(-20,-200)
  tr.pendown()
  tr.goto(20,-200)
  tr.penup()
  tr.pencolor("black")
  tr.goto(-200,200)
  tr.pendown()
  tr.goto(-200,-160)
  tr.begin_fill()
  tr.goto(-180,-160)
  tr.goto(-180,-200)
  tr.goto(-200,-200)
  tr.end_fill()
  tr.goto(-20,-200)
  tr.penup()
  tr.goto(20,-200)
  tr.pendown()
  tr.begin_fill()
  tr.goto(200,-200)
  tr.goto(200,200)
  tr.goto(20,200)
  tr.goto(20,140)
  tr.goto(80,140)
  tr.goto(80,180)
  tr.goto(180,180)
  tr.goto(180,-80)
  tr.goto(100,-80)
  tr.goto(100,-140)
  tr.goto(20,-140)
  tr.goto(20,-200)
  tr.end_fill()
  tr.penup()
  tr.goto(20,160)
  tr.pendown()
  tr.begin_fill()
  tr.goto(-80,160)
  tr.goto(-80,140)
  tr.goto(-60,140)
  tr.goto(-60,160)
  tr.end_fill()
  tr.goto(-60,120)
  tr.goto(-20,120)
  tr.goto(-20,100)
  tr.goto(40,100)
  tr.penup()
  tr.goto(120,140)
  tr.pendown()
  tr.begin_fill()
  tr.goto(140,140)
  tr.goto(140,100)
  tr.goto(120,100)
  tr.goto(120,120)
  tr.goto(120,140)
  tr.end_fill()
  tr.goto(140,100)
  tr.goto(80,100)
  tr.goto(80,80)
  tr.goto(80,60)
  tr.begin_fill()
  tr.goto(140,60)
  tr.goto(140,20)
  tr.goto(100,20)
  tr.goto(100,60)
  tr.end_fill()
  tr.goto(20,60)
  tr.goto(20,-100)
  tr.goto(20,-40)
  tr.goto(-20,-40)
  tr.goto(-20,-100)
  tr.goto(-60,-100)
  tr.penup()
  tr.goto(-60,-140)
  tr.pendown()
  tr.goto(-60,-160)
  tr.goto(-140,-160)
  tr.goto(-140,-120)
  tr.goto(-160,-120)
  tr.goto(-160,-40)
  tr.penup()
  tr.goto(-60,-60)
  tr.pendown()
  tr.goto(-60,0)
  tr.goto(-20,0)
  tr.goto(-20,40)
  tr.goto(-60,40)
  tr.goto(-60,80)
  tr.goto(-120,80)
  tr.goto(-120,200)
  tr.penup()
  tr.goto(-160,160)
  tr.pendown()
  tr.goto(-160,40)
  tr.goto(-40,40)
  tr.penup()
  tr.goto(60,20)
  tr.pendown()
  tr.goto(60,-80)
  tr.begin_fill()
  tr.goto(60,-40)
  tr.goto(140,-40)
  tr.goto(140,-20)
  tr.goto(60,-20)
  tr.end_fill()
  tr.penup()
  tr.goto(-200,0)
  tr.pendown()
  tr.goto(-100,0)
  tr.begin_fill()
  tr.goto(-100,-120)
  tr.goto(-100,-80)
  tr.goto(-120,-80)
  tr.goto(-120,0)
  tr.end_fill()
  tr.penup()
  tr.goto(-20,-140)
  tr.pendown()
  tr.goto(-20,-200)
  
for color in ["#FF0000","#0000FF","#00FF00"]:
  drawMazeSection(color)
  

screen.tracer(1)    
from turtle import Turtle, Screen

wn = Screen()
wn.bgcolor('darkgreen')

tr.color('red')
tr.penup()

tr.speed(40)
speed = 1.5

wn.mainloop()
tr.penup()
tr.goto(20,-180)
tr.pendown()
tr.shape('turtle')
tr.color("#DB148E")
tr.width(5)
tr.left(90)

tr.penup()
tr.goto(0,-200)
def travel():
    tr.forward(speed)
    wn.ontimer(travel, 10)

wn.onkey(lambda: tr.setheading(90), 'w')
wn.onkey(lambda: tr.setheading(180), 'a')
wn.onkey(lambda: tr.setheading(0), 'd')
wn.onkey(lambda: tr.setheading(270), 's')

wn.listen()
travel()

I think you need the turtle.window_width() and turtle.window_height functions

Gets the width and height of a program

width, height = turtle.window_width(), turtle.window_height()

## Gets the boundaries for the x coordinates 
minX, maxX = -width/2, width/2

## Sets the boundaries for the y coordinates
minY, maxY = -height/2, height / 2

## Checks if the turtle hits the boundaries
def check():
    if tr.xcor() <= maxX:
       tr.setheading(0)
    if tr.xcor() >= minX:
       tr.setheading(180)
    if tr.ycor() >= minY:
       tr.setheading(270)
    if tr.ycor() <= maxY:
       tr.setheading(90)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM