简体   繁体   English

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

[英]Python Turtle Collision Help and Updating Score/Lives

I am working on a scrolling game that uses multiple turtles.我正在开发一个使用多只海龟的滚动游戏。 The player turtle moves on the Y-Axis on key commands.玩家海龟通过键盘命令在 Y 轴上移动。 While the Harm and Benefits move across the X-Axis then looping around and changing Y-Axis position.当伤害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。 I have tried defining a function called colliding() that can test if the player and benefit turtles collide.我尝试定义一个名为colliding()的函数,它可以测试玩家和受益海龟是否发生碰撞。 Can someone help with the collision function?有人可以帮忙解决碰撞功能吗? And I also have a question on how, after detecting collision, to change the value of the player score?而且我还有一个问题,即在检测到碰撞后如何更改玩家得分的值? I think I have figured this out: I used player['lives']+=1 .我想我已经弄清楚了:我使用了player['lives']+=1 But this doesn't change because -- is something wrong with my collision function or loop?但这不会改变,因为——我的碰撞函数或循环有问题吗?

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!

Your code is a disaster, there are any number of bits of logic in it that will keep it from running.您的代码是一场灾难,其中有许多逻辑会阻止它运行。 To address your question, turtle has it's own distance() method to measure the distance between turtles or turtles and positions.为了解决您的问题,海龟有自己的distance()方法来测量海龟或海龟与位置之间的距离。 Using that, your colliding() method could be as simple as:使用它,您的colliding()方法可以很简单:

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

if you were to actually call your colliding() method, which you don't, as @Hoog points out.如果你真的要调用你的colliding()方法,你没有,正如@Hoog 指出的那样。 Other showstoppers in this code:此代码中的其他showstoppers:

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

The radius properties are never defined. radius属性从未定义。

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

Although player's x position is updated in the dictionary, benefit's isn't, so this will never work.虽然玩家的 x 位置在字典中更新,但好处的不是,所以这永远不会起作用。

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

Missing comment character.缺少注释字符。

clearscreen

What is this?这是什么? Probably should be screen.clearscreen() .可能应该是screen.clearscreen()

collision_detected = False;
...
collision_detected = True;

semicolons in Python are usually a sign that things aren't going well. 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 instances don't respond to the tracer() method.海龟实例不响应tracer()方法。 Screen instances do.屏幕实例可以。

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

Ditto for update() . update() And so forth.等等。

Below is my rework of your code to make it basically run:以下是我对您的代码进行的修改,以使其基本运行:

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()

It still needs a lot of work, however.然而,它仍然需要大量的工作。

Pygame has a built in collision detection algorithm rect.colliderect() which might help you out. Pygame 有一个内置的碰撞检测算法rect.colliderect()可以帮助你。

For your code the issue seems to be this line if colliding == True: you never set the variable colliding, perhaps you meant to call the function with something like: if colliding(Player1,Ben1) == True:对于您的代码,问题似乎是这一行if colliding == True:您从未设置变量 colliding,也许您打算使用以下内容调用该函数: if colliding(Player1,Ben1) == True:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM