我对编程很陌生,我想创建一个简单的游戏,玩家不应该在其中穿过实体。我检查了碰撞,但如果玩家碰撞固体,他就会被冻结。我知道为什么(因为我让玩家只有在不接触固体的情况下才能走路)但我不知道应该用什么代替。
这是我的代码:
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")
感谢您的任何想法
PS:对不起我的英语不好