Python Turtle碰撞帮助和更新分数/寿命

时间:2019-03-05 19:15:14

标签: python python-3.x turtle-graphics

我正在研究使用多个乌龟的滚动游戏。玩家乌龟通过按键命令在Y轴上移动。当危害和利益在X轴上移动时,便会四处循环并更改Y轴的位置。我尝试定义一个名为colliding()的函数,该函数可以测试玩家和受益乌龟是否发生碰撞。有人可以提供碰撞功能吗?我还有一个问题,在检测到碰撞后如何更改玩家得分的值?我想我已经弄清楚了:我使用了player['lives']+=1。但这不会改变,因为-我的碰撞函数或循环出了点问题吗?

import turtle
import random
import math

#The list of the turtles being used
t = turtle.Turtle()
Harm1 = turtle.Turtle()       
Harm2 = turtle.Turtle()
Ben = turtle.Turtle()
player1 = turtle.Turtle()

#Screen Specifications
screen = turtle.Screen()
screen.setup(500,500)
screen.bgcolor('darkgray')
Harm1.tracer(0)
Harm2.tracer(0)
Ben.tracer(0)
player1.tracer(0)


#Character dictionary
player ={"type":'Player',"x" : -200,"y" : 0,"size" : 20,"speed" : 10,
"color" : "green","Lives":3,"Score":0}

harm = {"type":'Harm',"x" : -200,"y" : 0,"size" : 30,"speed" : 6,
"color" : "red",}

benefit = {"type":'Benefit',"x" : -200,"y" : 0,"size" : 15,"speed":6,
"color" : "yellow",}

#These can change when collision happens
lives = player['Lives']
score = player['Score']

#These are the keys that let the player move up/down
def move_up():
  player['y']+=player['speed']
  return
def move_down():
  player['y']-= player['speed']

#Player gets info from dictionary
def draw_player():
  player1.clear()
  player1.penup()
  player1.goto(player["x"], player["y"])  
  player1.pendown()
  player1.color(player["color"])
  player1.begin_fill()
  player1.circle(player["size"])
  player1.end_fill()
  #if player1(player['y']) > 250 or player1(player['y']) < -250:
     #player1.sety(player['y'])
  player1.update()
  player1.hideturtle()
screen.listen()
screen.onkey(move_up, 'up')
screen.onkey(move_down, 'down')

def draw_harms():
  Harm1.clear()
  Harm1.color(harm['color'])
  Harm1.begin_fill()
  Harm1.circle(harm['size'])
  Harm1.end_fill()
  Harm1.update()
  Harm1.setx(Harm1.xcor()-(harm['speed']))
  if (Harm1.xcor() < -260 ): #This part of the code makes the object comeback.
     Harm1.setx(260)
     Harm1.sety(random.randint(-160,160)) #This makes the object change Y cor
  Harm1.hideturtle()
  Harm2.clear()
  Harm2.color(harm['color'])
  Harm2.begin_fill()
  Harm2.circle(harm['size'])
  Harm2.end_fill()
  Harm2.update()
  Harm2.setx(Harm2.xcor()-(harm['speed']))
  if (Harm2.xcor() < -260 ): #This part of the code makes the object comeback.
     Harm2.setx(220)
     Harm2.sety(random.randint(-160,160)) #This makes the object change Y cor
  Harm2.hideturtle()

def draw_benefit():
  Ben.clear()
  Ben.color(benefit['color'])
  Ben.begin_fill()
  Ben.circle(benefit['size'])
  Ben.end_fill()
  Ben.update()
  Ben.setx(Ben.xcor()-(benefit['speed']))
  if (Ben.xcor() < -260 ): #This part of the code makes the object comeback.
     Ben.setx(220)
     Ben.sety(random.randint(-160,160)) #This makes the object change Y cor
  Ben.hideturtle()   


#This Keeps the score and Lives
def draw_title_name(): #This writes the title on the screen
  t.penup()
  t.goto(-210,-200)
  t.pendown()
  t.write(("Score:", score),font=("Arial", 18, "normal"))
  t.penup()
  t.goto(-210,-223)
  t.pendown()
  t.write(('Lives:',lives),font=('Arial',18,'normal'))
  t.hideturtle()
  return


def colliding(player,benefit):
  collision_detected = False;
  var_dx = player['x'] - benefit['x']
  var_dy = player['y'] - benefit['y']
  distance = math.sqrt(var_dx * var_dx + var_dy * var_dy)
  if (distance < player['radius']+ benefit['radius']):
    collision_detected = True;
  return collision_detected



while lives > 0: #ANIMATION LOOP
  draw_player()
  draw_harms()
  draw_benefit()
  draw_title_name()
  if colliding == True:
    player['lives'] += 1 This changes the lives in the player Dict






  if lives == 0:
    clearscreen
    #Finish with a gameover screen!

2 个答案:

答案 0 :(得分:1)

您的代码是一场灾难,其中有许多逻辑位使其无法运行。为了解决您的问题,乌龟有自己的distance()方法来测量乌龟之间的距离或位置。使用该方法,您的colliding()方法可能很简单:

def colliding(player, benefit):
    return player.distance(benefit) < player_dict['size'] + benefit_dict['size']

@Hoog指出,如果您实际上要调用您的colliding()方法,则不需要。此代码中的其他排行榜:

if (distance < player['radius']+ benefit['radius']):

从未定义radius属性。

var_dx = player['x'] - benefit['x']

尽管玩家的x位置在字典中已更新,但利益的位置却未更新,因此这将永远无效。

player['lives'] += 1 This changes the lives in the player Dict

缺少注释字符。

clearscreen

这是什么?可能应该是screen.clearscreen()

collision_detected = False;
...
collision_detected = True;

Python中的分号通常表明情况不佳。

Harm1 = turtle.Turtle()       
Harm2 = turtle.Turtle()
Ben = turtle.Turtle()
player1 = turtle.Turtle()
...
Harm1.tracer(0)
Harm2.tracer(0)
Ben.tracer(0)
player1.tracer(0)

Turtle实例不响应tracer()方法。屏幕实例可以。

player1.update()
Harm1.update()
Harm2.update()
Ben.update()

update()的同上。依此类推。

下面是我对您的代码进行的重做,以使其基本运行:

from turtle import Screen, Turtle
from random import randint

FONT = ('Arial', 18, 'normal')
CURSOR_SIZE = 20

# Character dictionaries
player_dict = {'type': 'Player', 'x': -200, 'y': 0, 'radius': 20, 'speed':10, 'color': 'green', 'lives': 3, 'score': 0}

harm_dict = {'type': 'Harm', 'x': 0, 'y': 0, 'radius' : 30, 'speed': 6, 'color': 'red'}

benefit_dict = {'type': 'Benefit', 'x': 0, 'y': 0, 'radius': 15, 'speed': 6, 'color': 'yellow'}

# These are the keys that let the player move up/down
def move_up():
    player_dict['y'] += player_dict['speed']

def move_down():
    player_dict['y'] -= player_dict['speed']

# Player gets info from dictionary
def draw_player():
    player1.sety(player_dict['y'])

def draw_harms():
    harm1.forward(harm_dict['speed'])

    if harm1.xcor() < -250 - harm_dict['radius']:  # This part of the code makes the object come back.
        harm1.hideturtle()
        harm1.setx(250 + harm_dict['radius'])
        harm1.sety(randint(-160, 160))  # This makes the object change Y coordinate
        harm1.showturtle()

    harm2.forward(harm_dict['speed'])

    if harm2.xcor() < -250 - harm_dict['radius']:  # This part of the code makes the object comeback.
        harm2.hideturtle()
        harm2.setx(250 + harm_dict['radius'])
        harm2.sety(randint(-160, 160))  # This makes the object change Y coordinate
        harm2.showturtle()

def draw_benefit():
    ben.forward(benefit_dict['speed'])

    if ben.xcor() < -250 - benefit_dict['radius']:  # This part of the code makes the object comeback.
        ben.hideturtle()
        ben.setx(250 + benefit_dict['radius'])
        ben.sety(randint(-160, 160))  # This makes the object change Y coordinate
        ben.showturtle()

# This Keeps the score and Lives
def draw_lives():
    lives.undo()
    lives.write("Lives: {}".format(player_dict['lives']), font=FONT)

def draw_score():
    score.undo()
    score.write("Score: {}".format(player_dict['score']), font=FONT)

def colliding(player, benefit):
    return player.distance(benefit) < player_dict['radius'] + benefit_dict['radius']

# Screen Specifications
screen = Screen()
screen.setup(500, 500)
screen.bgcolor('darkgray')

# The list of the turtles being used
t = Turtle(visible=False)
t.penup()

harm1 = Turtle('circle', visible=False)
harm1.color(harm_dict['color'])
harm1.shapesize(harm_dict['radius'] * 2 / CURSOR_SIZE)
harm1.penup()
harm1.setx(250 + harm_dict['radius'])
harm1.sety(randint(-160, 160))
harm1.setheading(180)
harm1.showturtle()

harm2 = Turtle('circle', visible=False)
harm2.color(harm_dict['color'])
harm2.shapesize(harm_dict['radius'] * 2 / CURSOR_SIZE)
harm2.penup()
harm2.setx(250 + harm_dict['radius'])
harm2.sety(randint(-160, 160))
harm2.setheading(180)
harm2.showturtle()

ben = Turtle('circle', visible=False)
ben.color(benefit_dict['color'])
ben.shapesize(benefit_dict['radius'] * 2 / CURSOR_SIZE)
ben.penup()
ben.setx(250 + benefit_dict['radius'])
ben.sety(randint(-160, 160))
ben.setheading(180)
ben.showturtle()

player1 = Turtle('circle', visible=False)
player1.color(player_dict['color'])
player1.shapesize(player_dict['radius'] * 2 / CURSOR_SIZE)
player1.penup()
player1.goto(player_dict['x'], player_dict['y'])
player1.showturtle()

score = Turtle(visible=False)
score.penup()
score.goto(-210, -200)
score.write("Score: {}".format(player_dict['score']), font=FONT)

lives = Turtle(visible=False)
lives.penup()
lives.goto(-210, -223)
lives.write("Lives: {}".format(player_dict['lives']), font=FONT)

screen.onkey(move_up, 'Up')
screen.onkey(move_down, 'Down')
screen.listen()

def move():
    draw_player()
    draw_harms()
    draw_benefit()
    # draw_score()

    if colliding(player1, ben):
        player_dict['lives'] += 1  # This increases the lives in the player dictionary
        ben.hideturtle()
        ben.setx(250 + benefit_dict['radius'])
        ben.sety(randint(-160, 160))
        ben.showturtle()
        draw_lives()

    if colliding(player1, harm1):
        player_dict['lives'] -= 1  # This decreases the lives in the player dictionary
        harm1.hideturtle()
        harm1.setx(250 + harm_dict['radius'])
        harm1.sety(randint(-160, 160))
        harm1.showturtle()
        draw_lives()

    if colliding(player1, harm2):
        player_dict['lives'] -= 1
        harm2.hideturtle()
        harm2.setx(250 + harm_dict['radius'])
        harm2.sety(randint(-160, 160))
        harm2.showturtle()
        draw_lives()

    if player_dict['lives'] == 0:
        screen.clearscreen()
        # Finish with a gameover screen!
        return

    if player_dict['lives'] > 0:
        screen.ontimer(move, 75)

move()
screen.mainloop()

但是,它仍然需要大量工作。

答案 1 :(得分:0)

Pygame具有内置的碰撞检测算法rect.colliderect(),可以帮助您解决问题。

对于您的代码,问题似乎是此行if colliding == True:,您永远不会设置变量冲突,也许是想用类似以下内容的函数来调用函数:if colliding(Player1,Ben1) == True: