How to make a player not be able to walk trough other objects in python?

Viewed 99

I'm quite new to programming and I wanna create a simple game on which a player shouldn't be able to walk through a solid. I checked for a collision but if the player collides the solid, he is frozen. I know why (cause I made the player only able to walk if he isn't touching a solid) but I don't know what I should use instead.

Here's my Code:

import turtle
import os
import math

#Set up the screen
wn = turtle.Screen()
wn.bgcolor("green")
wn.title("RPG")

#Create the player
player = turtle.Turtle()
player.color("blue")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(0,0)
player.setheading(90)

playerspeed = 10

#Define collision

def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+ math.pow(t1.ycor()-t2.ycor(),2))
    if distance < 20:
        return True
    else:
        return False

#Player movement

def move_left():
    if isCollision(player,solid):
        turtle.Screen().bgcolor("green")
    else:
        x = player.xcor()
        x -= playerspeed
        if x < -280:
            x = -280
        player.setx(x)
def move_right():
    if isCollision(player,solid):
        turtle.Screen().bgcolor("green")
    else:
        x = player.xcor()
        x += playerspeed
        if x > 280:
            x = 280
        player.setx(x)
def move_up():
    if isCollision(player,solid):
        turtle.Screen().bgcolor("green")
    else:
        y = player.ycor()
        y += playerspeed
        if y > 280:
            y = 280
        player.sety(y)
def move_down():
    if isCollision(player,solid):
        turtle.Screen().bgcolor("green")
    else:
        y = player.ycor()
        y -= playerspeed
        if y < -280:
            y = -280
        player.sety(y)

#Add solid

solid = turtle.Turtle()
solid.color("black")
solid.shape("square")
solid.penup()
solid.speed(0)
solid.setposition(220,220)
solid.setheading(90)

#Keyboard
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")

delay = raw_input("Press enter to finish")

Thanks for any idea

PS: Sorry for my bad english

1 Answers

I found the solution now, thanks to sedavidw:

import turtle
import os
import math

#Set up the screen
wn = turtle.Screen()
wn.bgcolor("green")
wn.title("RPG")

#Create the player
player = turtle.Turtle()
player.color("blue")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(0,0)
player.setheading(90)

playerspeed = 10

#Define collision

def isCollision(t1, t2):
    distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+ math.pow(t1.ycor()-t2.ycor(),2))
    if distance < 20:
        return True
    else:
        return False

#Player movement

def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -280:
        x = -280
    player.setx(x)
    if isCollision(player,solid):
        x += playerspeed
        player.setx(x)
def move_right():
    x = player.xcor()
    x += playerspeed
    if x > 280:
        x = 280
    player.setx(x)
    if isCollision(player,solid):
        x -= playerspeed
        player.setx(x)
def move_up():
        y = player.ycor()
        y += playerspeed
        player.sety(y)
        if y > 280:
            y = 280
            player.sety(y)
        if isCollision(player,solid):
            y -= playerspeed
            player.sety(y)
def move_down():
    y = player.ycor()
    y -= playerspeed
    player.sety(y)
    if y < -280:
        y = -280
        player.sety(y)
    if isCollision(player,solid):
        y += playerspeed
        player.sety(y)

#Add solid

solid = turtle.Turtle()
solid.color("black")
solid.shape("square")
solid.penup()
solid.speed(0)
solid.setposition(220,220)
solid.setheading(90)

#Keyboard
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")

delay = raw_input("Press enter to finish")
Related