【问题标题】:How to make a player not be able to walk trough other objects in python?如何让玩家无法在 python 中穿过其他物体?
【发布时间】:2020-07-07 12:27:58
【问题描述】:

我对编程很陌生,我想创建一个简单的游戏,玩家不应该在该游戏上穿过实体。我检查了碰撞,但如果玩家碰撞固体,他就会被冻结。我知道为什么(因为我让玩家只有在不接触固体的情况下才能走路)但我不知道应该用什么代替。

这是我的代码:

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:对不起我的英语不好

【问题讨论】:

  • 假设我正确阅读了您的代码,您在允许它们移动之前检查是否存在碰撞。这意味着玩家已经处于“碰撞”状态,然后他们不能从那时起移动。你想要做的是在你移动之前,检查如果它们按计划移动,它们是否会处于碰撞状态,如果是这样......阻止它们移动

标签: python collision-detection turtle-graphics python-turtle


【解决方案1】:

感谢 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多