我对编程很陌生,我想创建一个简单的游戏,玩家不能在该游戏上穿梭。我检查了碰撞,但如果玩家碰撞了固体,他将被冻结。我知道为什么(因为我让玩家只有在不接触固体的情况下才能够行走),但我不知道该怎么用。
这是我的代码:
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:对不起,我的英语不好
我现在找到了解决方案,这要感谢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")