import turtle
import os
import math
ms = turtle.Screen()
ms.bgcolor("grey")
ms.title("ok")
ground = turtle.Turtle()
ground.speed(0)
ground.color("black")
ground.penup()
ground.setposition(-500, -500)
ground.shape("square")
ground.shapesize(20, 200)
player = turtle.Turtle()
player.shape("square")
player.color("blue")
player.penup()
player.speed(0)
player.setposition(-450, -280)
playerspeed = 15
prop = turtle.Turtle()
prop.speed(0)
prop.shape("square")
prop.penup()
prop.color("red")
prop.setposition(-200, -50)
def move_left():
x = player.xcor()
x-= playerspeed
if x <-460:
x = - 460
player.setx(x)
def move_right():
x = player.xcor()
x+= playerspeed
if x >460:
x = 460
player.setx(x)
def move_down():
y = player.ycor()
y-= playerspeed
if y <-290:
y = - 290
player.sety(y)
def move_up():
y = player.ycor()
y+= playerspeed
if y >290:
y = 290
player.sety(y)
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor() - t2.xcor(), 2) + math.pow(t1.ycor() - t2.ycor(), 2))
if distance < 10:
return True
else:
return False
while True:
if isCollision(player, prop):
player.setposition(100, 100)
If I change the distance to > 10
it works but not as I want it to.
I want it to change the position of the player when prop and player are 10 pixels or less away from each other.
I've tried most things I know about, but I am still new to Python, or any programming language.
But I don't know what would cause it to freeze and crash -- any help is appreciated.
The primary issue I see with your code is while True: which shouldn't be used in an event-based environment. By creating a tight loop, you can keep events (key pressed, cursor movements, window closing) from being processed.
Below is a rework of your code using a timer event instead of while True:. I've also parameterized it as every turtle user sees a different size default window based on the dimensions of their screen. So you either need to use setup() to force a fixed size window or have your code adust to the window size. Now it adjusts to the window size. I've also made prop move to a random location upon collision, just to make the game more fun for me to play. And I've tossed your distance code in favor of turtle's own implementation:
from turtle import Screen, Turtle, mainloop
from random import randint
PLAYER_SPEED = 15
GROUND_HEIGHT = 100
PROXIMITY = 10
CURSOR_SIZE = 20
def move_left():
x = player.xcor() - PLAYER_SPEED
if x < CURSOR_SIZE - width/2:
x = CURSOR_SIZE - width/2
player.setx(x)
def move_right():
x = player.xcor() + PLAYER_SPEED
if x > width/2 - CURSOR_SIZE:
x = width/2 - CURSOR_SIZE
player.setx(x)
def move_down():
y = player.ycor() - PLAYER_SPEED
if y < CURSOR_SIZE/2 + GROUND_HEIGHT - height/2:
y = CURSOR_SIZE/2 + GROUND_HEIGHT - height/2
player.sety(y)
def move_up():
y = player.ycor() + PLAYER_SPEED
if y > height/2 - CURSOR_SIZE:
y = height/2 - CURSOR_SIZE
player.sety(y)
def isCollision(t1, t2):
return t1.distance(t2) < PROXIMITY
def random_position():
x = randint(CURSOR_SIZE - width//2, width//2 - CURSOR_SIZE)
y = randint(CURSOR_SIZE + GROUND_HEIGHT - height//2, height//2 - CURSOR_SIZE)
return x, y
def check():
if isCollision(player, prop):
prop.setposition(random_position())
ms.ontimer(check, 100)
ms = Screen()
ms.bgcolor('grey')
ms.title("ok")
width, height = ms.window_width(), ms.window_height()
ground = Turtle('square')
ground.shapesize(GROUND_HEIGHT / CURSOR_SIZE, width / CURSOR_SIZE)
ground.speed('fastest')
ground.penup()
ground.sety(GROUND_HEIGHT/2 - height/2)
player = Turtle('square')
player.speed('fastest')
player.color('blue')
player.penup()
player.setposition(CURSOR_SIZE/2 - width/2, GROUND_HEIGHT + CURSOR_SIZE/2 - height/2)
prop = Turtle('square')
prop.speed('fastest')
prop.color('red')
prop.penup()
prop.setposition(random_position())
ms.onkey(move_left, 'Left')
ms.onkey(move_right, 'Right')
ms.onkey(move_up, 'Up')
ms.onkey(move_down, 'Down')
ms.listen()
check()
mainloop()
Related
I was trying to make this snake game (no food yet) by using turtle. At length of 1 it still fast but as it goes longer it runs slower and slower.
from turtle import Turtle, Screen
SIZE = 500
GRID_NUM = 20
GRID_SIZE = SIZE // GRID_NUM
screen = Screen()
screen.setup(SIZE, SIZE)
screen.bgcolor('black')
screen.setworldcoordinates(10, 8, SIZE + 2, SIZE)
class Snake:
def __init__(self):
self.head = init_turtle()
self.head.setpos((GRID_SIZE // 2 + GRID_SIZE * 9, GRID_SIZE // 2 + GRID_SIZE * 10))
self.body = []
self.direction = 0 # 0 = left, 1 = up, 2 = right, 3 = down
self.speed = 100 * len(self.body) if len(self.body) > 0 else 100
def levelup(self):
if len(self.body) == 0:
self.body.append(self.head.clone())
else:
self.body.append(self.body[len(self.body) - 1].clone())
def update(self):
for i in range(len(self.body) - 1, -1 , -1):
if i == 0:
self.body[i].setpos(self.head.pos())
else:
self.body[i].setpos(self.body[i - 1].pos())
def init_turtle():
turtle = Turtle()
turtle.shape('square')
turtle.shapesize(1.25)
turtle.color('red')
turtle.pencolor('#404040')
turtle.speed('fastest')
turtle.up()
return turtle
def init_border():
def border():
turtle = init_turtle()
for _ in range(4):
turtle.down()
turtle.forward(SIZE)
turtle.left(90)
yield(0)
turtle.penup()
turtle.hideturtle()
def horizontal():
turtle = init_turtle()
for x in range(GRID_SIZE, SIZE, GRID_SIZE):
turtle.goto(x, 0)
turtle.pendown()
turtle.goto(x, SIZE)
turtle.penup()
yield(0)
turtle.hideturtle()
def vertical():
turtle = init_turtle()
for y in range(GRID_SIZE, SIZE, GRID_SIZE):
turtle.goto(0, y)
turtle.pendown()
turtle.goto(SIZE, y)
turtle.penup()
yield(0)
turtle.hideturtle()
generator1 = border()
generator2 = horizontal()
generator3 = vertical()
while(next(generator1, 1) + next(generator2, 1) + next(generator3, 1) < 3):
pass
def start_game():
snake = Snake()
def pop():
snake.body.pop()
def right():
snake.direction = 0
def up():
snake.direction = 1
def left():
snake.direction = 2
def down():
snake.direction = 3
def exit():
screen.bye()
screen.onkey(pop, 'q')
screen.onkey(left, 'Left')
screen.onkey(up, 'Up')
screen.onkey(right, 'Right')
screen.onkey(down, 'Down')
screen.onkey(exit, 'Escape')
screen.onkey(snake.levelup, 'Return')
screen.listen()
while True:
snake.update()
snake.head.setheading(snake.direction * 90)
snake.head.forward(GRID_SIZE)
print('snake', snake.head.pos())
init_border()
start_game()
screen.mainloop()
At first i think the cause of the problem was defines a lot of function but i don't know. Also i am new to python so i don't know any kind of optimizing methods.
Any ideas to optimize this program?
We can speed up long snakes by doing the drawing off screen and sending explicit updates. Below is a rework of your code that does just that and includes a temporary patch to make the snake move in a square so you can just hit to see how much the snake is affected by additional body segments:
from turtle import Turtle, Screen
SIZE = 525
GRID_NUM = 20
GRID_SIZE = SIZE // GRID_NUM
RIGHT, UP, LEFT, DOWN = range(4)
class Snake:
def __init__(self):
self.head = init_turtle()
self.head.setpos(GRID_NUM//2, GRID_NUM//2)
self.direction = LEFT
self.body = []
def levelup(self):
if self.body:
self.body.append(self.body[-1].clone())
else:
self.body.append(self.head.clone())
def update(self):
for i in range(len(self.body) - 1, -1, -1):
if i == 0:
self.body[i].setposition(self.head.position())
else:
self.body[i].setposition(self.body[i - 1].position())
def init_turtle():
turtle = Turtle()
turtle.shape('square')
print(GRID_SIZE)
turtle.shapesize((GRID_SIZE - 1) / 20)
turtle.color('#404040', 'red')
turtle.speed('fastest')
turtle.penup()
return turtle
def init_border():
def border():
turtle = init_turtle()
turtle.goto(-0.5, -0.5)
for _ in range(4):
turtle.pendown()
turtle.forward(GRID_NUM)
turtle.left(90)
turtle.penup()
turtle.hideturtle()
def vertical():
turtle = init_turtle()
for x in range(GRID_NUM):
turtle.goto(x - 0.5, -0.5)
turtle.pendown()
turtle.goto(x - 0.5, GRID_NUM - 0.5)
turtle.penup()
yield 0
turtle.hideturtle()
def horizontal():
turtle = init_turtle()
for y in range(GRID_NUM):
turtle.goto(-0.5, y + 0.5)
turtle.pendown()
turtle.goto(GRID_NUM - 0.5, y + 0.5)
turtle.penup()
yield 0
turtle.hideturtle()
border()
generator1 = horizontal()
generator2 = vertical()
while next(generator1, 1) + next(generator2, 1) < 2:
pass
def start_game():
snake = Snake()
def right():
snake.direction = RIGHT
def up():
snake.direction = UP
def left():
snake.direction = LEFT
def down():
snake.direction = DOWN
screen.onkey(snake.body.pop, 'q')
screen.onkey(left, 'Left')
screen.onkey(up, 'Up')
screen.onkey(right, 'Right')
screen.onkey(down, 'Down')
screen.onkey(screen.bye, 'Escape')
screen.onkey(snake.levelup, 'Return')
screen.listen()
while True:
snake.update()
snake.head.setheading(snake.direction * 90)
snake.head.forward(1)
# Run snake around in a square for testing purposes
x, y = snake.head.position()
if round(x) == 1 and snake.direction == LEFT:
up()
elif round(y) == GRID_NUM - 2 and snake.direction == UP:
right()
elif round(x) == GRID_NUM - 2 and snake.direction == RIGHT:
down()
elif round(y) == 1 and snake.direction == DOWN:
left()
screen.update()
screen = Screen()
screen.setup(SIZE, SIZE)
screen.bgcolor('black')
screen.setworldcoordinates(-0.75, -0.75, GRID_NUM + 0.25, GRID_NUM + 0.25)
init_border()
screen.tracer(False)
start_game()
screen.mainloop() # never reached!
I'd recommend both approaches as the main loop will only get more complicated (slower) as you add food and border detection.
I changed your coordinate system to make it snake centric. That is, forward(1) moves one square on the grid rather than having to do the math. If you're going to mess with the coordinates anyway, why not tweak them to your best advantage.
I'm making a game using Turtle in Python 3.
My main problem is that the turtle can move through the "drawn" lines in the game. It's basically a randomized "flappy-bird" game, different each time you run the program.
Is it possible to prevent the Turtle from going through lines it has drawn?
The program creates both walls and holes the player should be able to get through, like flappy bird where it's a pipe-hole-pipe with different lengths on the pipes.
And if I run print(hole) at the end of the program it returns None. Why is this?
Here is my program:
import turtle
import random
GameOver=False
import math
import time
board = turtle.Screen()
board.screensize(50.50)
board.bgcolor("black")
board.title("Labyrinten")
Hast_på_ritande_av_bana= 0
TURTLE_SIZE = 20
playerspeed = 50
n=1 #variabeler som gör att den hoppar ett steg åt höger när den gör raderna t.ex. ((930/8)*n)
b=2 #
global player
#SÄTTER UPP SKÄRM
border = turtle.Turtle()
border.speed(Hast_på_ritande_av_bana)
border.hideturtle()
border.color("white")
border.shape("triangle")
border.penup()
border.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
border.pendown()
border.showturtle()
border.rt(90)
border.fd(780)
border.penup()
#ritar banan
for rader in range(4): #creating the maze
border.lt(90)
border.fd(930/8)
border.pendown()
border.lt(90)
wall_ett = border.fd(random.randint(50,630)) #lower wall on row 1
border.penup()
hole=border.fd(random.randint(30,70)) #hole on row 1
border.pendown()
wall_ett_ett = border.goto (TURTLE_SIZE/2 - board.window_width()/2 + (930/8*n), board.window_height()/2 - TURTLE_SIZE/2) #higher wall on row 1
border.rt(90)
border.fd(930/8)
border.rt(90)
wall_två_ett = border.fd(random.randint(50, 630)) #lower wall on row 2
border.penup()
hole2 = border.fd(random.randint(30, 70)) #hole on row 2
border.pendown()
wall_två_två = border.goto(TURTLE_SIZE/2 - board.window_width()/2 + (930/8*b), + TURTLE_SIZE/2 - board.window_height()/2 ) # higher wall on row 2
n = n + 2
b = b + 2
border.goto(TURTLE_SIZE/2 - board.window_width()/2 , TURTLE_SIZE/2 - board.window_height()/2 )
border.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
border.penup()
border.lt(90)
border.fd(930/8)
border.pendown()
border.goto(-TURTLE_SIZE + board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
border.hideturtle()
player=turtle.Turtle()
player.color("green")
player.shape("turtle")
player.hideturtle()
player.penup()
player.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
player.rt(90)
player.fd(30)
player.lt(90)
player.fd((930/8)/2)
player.rt(90)
player.showturtle()
coordinates = player.pos()
#Skapa spelaren (som går igenom banan)
def spawna_spelare():
spawn = turtle.Turtle()
spawn.color("green")
spawn.shape("turtle")
spawn.hideturtle()
spawn.penup()
spawn.goto(TURTLE_SIZE/2 - board.window_width()/2, board.window_height()/2 - TURTLE_SIZE/2)
spawn.rt(90)
spawn.fd(30)
spawn.lt(90)
spawn.fd((930/8)*(random.randint(1,8)))
spawn.rt(90)
spawn.showturtle()
#Gå med spelaren
def move_left():
x = player.xcor()
x -= playerspeed
if x < -460:
x = -460
player.setx(x)
def move_right():
x = player.xcor()
x += playerspeed
if x > 450:
x = 450
player.setx(x)
if x.distance(wall_ett) >20: #or wall_ett_ett or wall_två_ett or wall_två_två) == 1: #
player.shape("triangle")
def move_down():
y = player.ycor()
y -= playerspeed
if y < -375:
y = -375
player.sety(y)
def move_up():
y = player.ycor()
y += playerspeed
if y > 380:
y = 380
player.sety(y)
turtle.listen()
turtle.onkey(spawna_spelare, "space")
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_down, "Down")
turtle.onkey(move_up, "Up")
wall_ett = border.xcor()
wall_två_ett=border.xcor()
print(wall_ett)
print(wall_ett)
print(hole)
board.mainloop()
So you need to draw the walls in a way you can check afterwards if the move the player requested is crossing any wall (or border).
here is a suggestion:
SCREEN_WIDTH = 800
HOLE_HEIGHT = 50
SCREEN_HEIGHT = 600
NUM_WALLS = 8
board = turtle.Screen()
board.setup(SCREEN_WIDTH+10, SCREEN_HEIGHT+10)
board.screensize(SCREEN_WIDTH, SCREEN_HEIGHT)
board.setworldcoordinates(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
border = turtle.Turtle()
border.speed(10) # draw walls as fast as possible
border.color('white')
border.hideturtle()
# list of the drawn walls, each wall is represented by a tuple:
# (wall_x, wall_hole_lower_y, wall_hole_upper_y)
walls = []
for wall_ix in range(NUM_WALLS):
# compute coordinates of wall and store them
wall_x = int((1+wall_ix) * SCREEN_WIDTH / (1+NUM_WALLS))
wall_ybottom = random.randint(0, SCREEN_HEIGHT - HOLE_HEIGHT)
wall_ytop = wall_ybottom + HOLE_HEIGHT
walls.append((wall_x, wall_ytop, wall_ybottom))
# draw wall
border.penup()
border.goto(wall_x, 0)
border.pendown()
border.goto(wall_x, wall_ybottom)
border.penup()
border.goto(wall_x, wall_ytop)
border.pendown()
border.goto(wall_x, SCREEN_HEIGHT)
def move_right():
old_x = player.xcor()
new_x = old_x + playerspeed
y = player.ycor()
for wall_x, wall_ytop, wall_ybottom in walls:
if old_x <= wall_x <= new_x:
# player is possibly moving across the wall, check that y is valid
if wall_ybottom < y < wall_ytop:
# OK ! player moves through the hole
player.setx(new_x)
else:
# invalid, player is crossing a wall line
# resetting to old position, but you might want to stop the game
player.setx(old_x)
I'm trying to make a tic tac toe game where you use arrow keys to move a turtle around the board, and when you press the enter key it draws a circle (nought in the code) or a cross.
I've tried using similar code to what I used for the movement keys but replace it for the enter key and C key (for cross) but they don't work and automatically draw the cross when it starts up. I've also tried using monkey but that doesn't work.
import turtle
sc = turtle.Screen()
bo = turtle.Turtle()
#screen
sc.bgcolor("black")
sc.title("tic tac toe")
#board
bo.color("white")
bo.penup()
bo.speed(0)
bo.setposition(-100, -300)
bo.pendown()
bo.setposition(-100, 300)
bo.penup()
bo.setposition(100, 300)
bo.pendown()
bo.setposition(100, -300)
bo.penup()
bo.setposition(-300, 100)
bo.pendown()
bo.setposition(300, 100)
bo.penup()
bo.setposition(300, -100)
bo.pendown()
bo.setposition(-300,-100)
bo.penup()
bo.setposition(0,0)
#naught and cross function
def c() :
bo.pendown()
x = bo.xcor()-80
y = bo.ycor()+80
bo.penup()
bo.setposition(x,y)
bo.pendown()
x = bo.xcor()+160
y = bo.ycor()-160
bo.setposition(x,y)
bo.penup()
x = bo.xcor()-160
bo.setposition(x,y)
bo.pendown()
x = bo.xcor()+160
y = bo.ycor()+160
bo.setposition(x,y)
bo.penup()
bo.setposition(0,0)
def n() :
y = bo.ycor()-80
x = bo.xcor()
bo.setposition(x, y)
bo.pendown()
bo.circle(80)
bo.penup()
bo.setposition(0,0)
#movment
def move_left(event):
x = bo.xcor()
x -= 200
if x < -300:
x = -200
bo.setx(x)
def move_right(event):
x = bo.xcor()
x += 200
if x > 300:
x = 200
bo.setx(x)
def move_down(event):
y = bo.ycor()
y -= 200
if y < -300:
y = -200
bo.sety(y)
def move_up(event):
y = bo.ycor()
y += 200
if y > 300:
y = 200
bo.sety(y)
#Keybinding
turtle.listen()
turtle.getcanvas().bind("<Left>", move_left)
turtle.getcanvas().bind("<Right>", move_right)
turtle.getcanvas().bind("<Up>", move_up)
turtle.getcanvas().bind("<Down>", move_down)
I'd eventually like to have it where once you draw a cross, and press enter, it draws a circle where there isn't a cross.
There's no need to drop down to the Tk Canvas to configure your key events:
turtle.getcanvas().bind("<Left>", move_left)
You can do the same from turtle itself:
sc.onkey(move_left, "Left")
I've reworked your code below, adding a key event for "Return" that draws the naughts and crosses. Since you don't have an underlying data structure for your game, I set it up to simply alternate between the two:
from turtle import Screen, Turtle
# naught and cross functions
def cross():
x, y = board.position()
board.penup()
board.setposition(x - 80, y - 80)
board.pendown()
board.setposition(x + 80, y + 80)
board.penup()
board.setx(x - 80)
board.pendown()
board.setposition(x + 80, y - 80)
board.penup()
board.home()
def naught():
board.sety(board.ycor() - 80)
board.pendown()
board.circle(80)
board.penup()
board.home()
first_player = True
def choose():
global first_player
if first_player:
naught()
else:
cross()
first_player = not first_player
# movement
def move_left():
x = board.xcor() - 200
if x < -300:
x = -200
board.setx(x)
def move_right():
x = board.xcor() + 200
if x > 300:
x = 200
board.setx(x)
def move_down():
y = board.ycor() - 200
if y < -300:
y = -200
board.sety(y)
def move_up():
y = board.ycor() + 200
if y > 300:
y = 200
board.sety(y)
# screen
screen = Screen()
screen.bgcolor("black")
screen.title("tic tac toe")
# board
board = Turtle()
board.color("white")
board.speed('fastest')
# vertical lines
board.penup()
board.setposition(-100, -300)
board.pendown()
board.sety(300)
board.penup()
board.setx(100)
board.pendown()
board.sety(-300)
board.penup()
# horizontal lines
board.setposition(-300, 100)
board.pendown()
board.setx(300)
board.penup()
board.sety(-100)
board.pendown()
board.setx(-300)
board.penup()
board.home()
# Keybinding
screen.onkey(move_left, "Left")
screen.onkey(move_right, "Right")
screen.onkey(move_up, "Up")
screen.onkey(move_down, "Down")
screen.onkey(choose, "Return")
screen.listen()
screen.mainloop()
This allows two people to play the game and should be something you can build upon.
I have written some code but can't get the ball to bounce on the floor or the ceiling naturally. Please help!
I've already tried getting the ball heading with ball_heading = ball.heading, but that didn't work
#Python 3.6.3
from turtle import *
import math
import random
#Screen Setup
bgcolor("black")
wn = Screen()
wn.title("Pong")
#Drawing Border
bd = Turtle()
bd.pencolor("white")
bd.pensize(3)
bd.hideturtle()
bd.penup()
bd.setposition(-400, -300)
bd.pendown()
bd.speed(0)
bd.pendown()
for line in range(2):
bd.forward(800)
bd.left(90)
bd.forward(600)
bd.left(90)
bd.penup()
bd.setposition(0, 300)
bd.setheading(270)
bd.pendown()
for dash in range(30):
bd.forward(10)
bd.penup()
bd.forward(10)
bd.pendown()
#Creating Paddles
#Paddle 1
player1 = Turtle()
player1.color("white")
player1.shape("square")
player1.shapesize(stretch_wid=5, stretch_len=1)
player1.penup()
player1.setposition(-370, 0)
#Paddle 2
player2 = Turtle()
player2.color("white")
player2.shape("square")
player2.shapesize(stretch_wid=5, stretch_len=1)
player2.penup()
player2.setposition(370, 0)
#Creating the ball
ball = Turtle()
ball.color("white")
ball.shape("square")
ball.speed(0)
ball.penup()
ball.setposition(0, 0)
ball.setheading(random.randint(0, 360))
#Moving the player
playerspeed = 15
#p1
def move_up():
y = player1.ycor()
y += playerspeed
#Setting the boundries
if y > 245:
y = 245
player1.sety(y)
def move_down():
y = player1.ycor()
y -= playerspeed
#Setting the boundries
if y < -245:
y = -245
player1.sety(y)
#p2
def move_up2():
y = player2.ycor()
y += playerspeed
#Setting the boundries
if y > 245:
y = 245
player2.sety(y)
def move_down2():
y = player2.ycor()
y -= playerspeed
#Setting the boundries
if y < -245:
y = -245
player2.sety(y)
#Ball movement
def ball_fd():
ball.forward(3)
#Ball ceiling / floor bounce
def ball_bounce():
by = ball.ycor()
if by > 279:
by = 279
ball.sety(by)
bx = ball.ycor()
if bx < -279:
bx = -279
ball.setx(bx)
#binding
listen()
onkey(move_up, "Up")
onkey(move_down, "Down")
onkey(move_up2, "w")
onkey(move_down2, "s")
#Making the ball move / main game loop
while True:
ball_fd()
ball_bounce()
Sorry the code is kind of long, but feel free to copy + paste it into IDLE or whatever.
Thank you
You didn't turn the ball when it hit the floor or ceiling.
while True:
ball.fd(3)
by = ball.ycor()
if abs(by) > 279:
ball.setheading(-ball.heading())
I wanted to make a snake game in turtle but I can't seem to get the tail segments to follow the leader. As if to say I can't get the first body part to follow the head, and then the second body part to follow the first body, etc.
I tried using what maths I have but I can't seem to get it to calculate where the head just was or where the body in front of it just was.
here is my code:
#libraries
import turtle
import random
import math
#screen
the_screen = turtle.Screen()
the_screen.bgcolor("lightgreen")
the_screen.title("Catapillar")
#border
border_writer = turtle.Turtle()
border_writer.speed(0)
border_writer.color("green")
border_writer.penup()
border_writer.setposition(-275, -275)
border_writer.pendown()
border_writer.pensize(7)
for side in range(4):
border_writer.fd(550)
border_writer.lt(90)
border_writer.hideturtle()
#player
player = turtle.Turtle()
player.color("yellow")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(0,0)
player.setheading(0)
playerspeed = 2
#fruit
fruit = 1
fruit = turtle.Turtle()
fruit.color("red")
fruit.shape("circle")
fruit.penup()
fruit.speed(0)
fruit.shapesize(0.6, 0.6)
x = random.randint(-200, 200)
y = random.randint(100, 250)
fruit.setposition(x, y)
#moving and collision checker
def turnleft():
player.left(24.5)
def turnright():
player.right(24.5)
def increasespeed():
global playerspeed
playerspeed += 1
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(), 2) + math.pow(t1.ycor()-t2.ycor(), 2))
if distance < 24:
return True
else:
return False
#making the tail(s) and hiding them
tail1 = turtle.Turtle()
tail1.hideturtle()
tail1.color("yellow")
tail1.shape("circle")
tail1.penup()
tail1.speed(0)
tail1.setposition(+700,700)
tail1.shapesize(0.6, 0.6)
tail1state = 'off'
#key presses
turtle.listen()
turtle.onkeypress(turnleft, "Left")
turtle.onkeypress(turnright, "Right")
turtle.onkeypress(increasespeed, "Up")
#main loop player always moves forward, out of bound set, reset fruit
#randomly when collided, create tail1...
while True:
player.forward(playerspeed)
#gameovers'
if (player.xcor() > 275) or (player.xcor() < -275):
playerspeed += 7
print("GAME OVER")
if (player.ycor() > 275) or (player.ycor() < -275):
playerspeed += 7
print("GAME OVER")
#collision check between fruit and head
if isCollision(player, fruit):
#resets the fruit, moves it randomly
fruit.hideturtle()
if tail1state == 'off':
uuu = player.xcor()
vvv = player.ycor()
tail1.setposition(uuu,vvv)
tail1.showturtle()
tail1state = 'on'
#reset the fruit
x = random.randint(-200, 200)
y = random.randint(-100, 250)
fruit.setposition(x, y)
fruit.showturtle()
#playerspeed +=1
If you have any idea on how you think I should approach it, please let me know what you think.
Thank you.
Since this comes up over and over (but effort to effect it is rarely shown) here's my minimal, generic, slithering around a screen effect that you should be able to adapt for your own purposes:
from turtle import Turtle, Screen
SNAKE_LENGTH = 10
SNAKE_SIZE = 15
WIDTH, HEIGHT = 375, 375
CURSOR_SIZE = 20
def slither():
segment = snake[-1].clone() if len(snake) < SNAKE_LENGTH else snake.pop(0)
screen.tracer(False) # segment.hideturtle()
segment.setposition(snake[-1].position())
segment.setheading(snake[-1].heading())
segment.forward(SNAKE_SIZE)
while not -WIDTH/2 < segment.xcor() < WIDTH/2 or not -HEIGHT/2 < segment.ycor() < HEIGHT/2:
segment.undo()
segment.right(95)
segment.forward(SNAKE_SIZE)
screen.tracer(True) # segment.showturtle()
snake.append(segment)
screen.ontimer(slither, 100)
boundary = Turtle(shape='square', visible=False)
boundary.shapesize(HEIGHT / CURSOR_SIZE, WIDTH / CURSOR_SIZE)
boundary.fillcolor("white")
boundary.stamp()
head = Turtle("circle")
head.shapesize(SNAKE_SIZE / CURSOR_SIZE)
head.penup()
snake = [head]
screen = Screen()
slither()
screen.exitonclick()