Python Turtle Collision Помощь и обновление счета / Lives - PullRequest
1 голос
/ 05 марта 2019

Я работаю над игрой с прокруткой, которая использует несколько черепах. Черепаха игрока перемещается по оси 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 ]

1 голос
/ 06 марта 2019

Ваш код является катастрофой, в нем есть любое количество битов логики, которые будут препятствовать его работе. Чтобы ответить на ваш вопрос, у черепахи есть собственный метод distance() для измерения расстояния между черепахами или черепахами и их позициями. Используя это, ваш colliding() метод может быть таким простым:

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

если бы вы на самом деле вызывали ваш colliding() метод, который вы не делаете, как указывает @Hoog. Другие showtoppers в этом коде:

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

Свойства radius никогда не определяются.

var_dx = player['x'] - benefit['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()

Однако это все еще требует большой работы.

0 голосов
/ 05 марта 2019

Pygame имеет встроенный алгоритм обнаружения столкновений rect.colliderect(), который может вам помочь.

Для вашего кода проблема, по-видимому, заключается в следующей строке if colliding == True: вы никогда не устанавливали конфликт переменных, возможно, вы хотели вызвать функцию с чем-то вроде: if colliding(Player1,Ben1) == True:

...