玩家与敌人碰撞的问题

时间:2019-06-17 17:29:33

标签: python turtle-graphics

给我一​​个错误,说name 'isCollision' is not defined,我试图找到定义它的方法,但找不到与我的代码兼容的任何东西:

import turtle
import os
import random


#create sprite
class Sprite(turtle.Turtle):
    def __init__(self, spriteshape, color, startx, starty):
        turtle.Turtle.__init__(self, shape = spriteshape)
        self.speed(0)
        self.penup()
        self.color(color)
        self.fd(0)
        self.goto(startx, starty)
        self.speed = 1
    def move(self):
        self.fd(self.speed)

 #Boundary detection
        if self.xcor() > 300:
            self.setx(300)
            self.rt(90)

        if self.xcor() < -300:
            self.setx(-300)
            self.rt(120)

        if self.ycor() > 300:
            self.sety(300)
            self.rt(90)

        if self.ycor() < -300:
            self.sety(-300)
            self.rt(120)
def is_collision(self, other):
    if (self.xcor() >= (other.xcor() - 20)) and \
       (self.xcor() <= (other.xcor() + 20)) and \
       (self.ycor() >= (other.ycor() - 20)) and \
       (self.ycor() <= (other.ycor() + 20)):
        return True
    else:
        return False

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("player v enemy")
# Draw border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()
border_pen.pensize(5)
for side in range(4):
    border_pen.fd(600)
    border_pen.lt(90)
border_pen.hideturtle()
# Create the player turtle
player = turtle.Turtle()
player.color("yellow")
player.shape("square")
player.penup()
player.speed(0)
player.setposition(0, -250)
player.shapesize(2.3, 2.3)

playerspeed = 35
# create enemy
class Enemy(Sprite):
    def __init__(self ,  spriteshape, color, startx, starty):
        Sprite.__init__(self  , spriteshape, color, startx, starty)
        self.speed = 6
        self.setheading(random.randint(0,360))  


# player movement
def move_left():
    x = player.xcor()
    x -= playerspeed
    if x < -280:
        x = - 280
    player.setx(x)


def move_right():
    x = player.xcor()
    x += playerspeed
    if x > 280:
        x = 280
    player.setx(x)




# keybinds
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")

#create sprite
enemy= Enemy("circle" ,"red", -200,250)
#main game loop
while True:
    enemy.move()

    #check for colision
    if isCollision(enemy, palyer):
        x = random.randint(-250, 250)
        y = random.randint(-250, 250)
        enemy.setposition(x, y)






我希望敌人与我的玩家发生碰撞并重置。

Traceback (most recent call last):
  File "C:\Users\prana\Desktop\game.py\players v enemys.py", line 109, in <module>
    if isCollision(enemy, palyer):
NameError: name 'isCollision' is not defined

2 个答案:

答案 0 :(得分:0)

读取回溯,您正在调用错误的函数:

您的功能是:

def is_collision(self, other)

您正在打电话

if isCollision(enemy, palyer)

您缺少下划线

还请注意,python区分大小写,因此请删除大写字母

您拼错了玩家

答案 1 :(得分:0)

下面是对代码的(Python 3)修改,使其可以播放。 。除了拼写错误的符号外,您还使用while True:处理事件模型;您将乌龟speed方法重新定义为数据;和其他故障:

from turtle import Screen, Turtle
from random import randint

# create sprite
class Sprite(Turtle):
    def __init__(self, sprite_shape, color, startx, starty):
        super().__init__(shape=sprite_shape)

        self.speed('fastest')
        self.penup()
        self.color(color)
        self.goto(startx, starty)

        self.step = 1

    def move(self):
        self.forward(self.step)

        # Boundary detection
        if self.xcor() > 300:
            self.setx(300)
            self.rt(90)
        elif self.xcor() < -300:
            self.setx(-300)
            self.rt(120)

        if self.ycor() > 300:
            self.sety(300)
            self.rt(90)
        elif self.ycor() < -300:
            self.sety(-300)
            self.rt(120)

    def is_collision(self, other):
        return self.distance(other) < 20

# create enemy
class Enemy(Sprite):
    def __init__(self, sprite_shape, color, startx, starty):
        super().__init__(sprite_shape, color, startx, starty)
        self.step = 6
        self.setheading(randint(0, 360))

# Set up the screen
screen = Screen()
screen.bgcolor('black')
screen.title("Player vs. Enemy")

# Draw border
border_pen = Turtle()
border_pen.hideturtle()
border_pen.speed('fastest')
border_pen.color('white')
border_pen.pensize(5)

border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()

for _ in range(4):
    border_pen.fd(600)
    border_pen.lt(90)

# Create the player turtle
player = Turtle()
player.shape('square')
player.shapesize(2.3)
player.color('yellow')
player.speed('fastest')

player.penup()
player.setposition(0, -250)

player_speed = 30

# player movement
def move_left():
    x = player.xcor() - player_speed

    if x < -280:
        x = - 280

    player.setx(x)

def move_right():
    x = player.xcor() + player_speed

    if x > 280:
        x = 280

    player.setx(x)

# keybindings
screen.onkey(move_left, 'Left')
screen.onkey(move_right, 'Right')
screen.listen()

# create sprite
enemy = Enemy('circle', 'red', -200, 250)
# main game loop

def play():
    enemy.move()

    # check for collision
    if enemy.is_collision(player):
        x = randint(-250, 250)
        y = randint(-250, 250)
        enemy.setposition(x, y)
        enemy.setheading(randint(0, 360))

    screen.ontimer(play, 75)

play()
screen.mainloop()

如果您需要与Python 2兼容,请发表评论,我将调整代码。