gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:15:28 30 4
gpt4 key购买 nike

我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding() 的函数,它可以测试玩家和受益 turtle 是否发生碰撞。有人可以帮忙解决碰撞功能吗?我还有一个问题,在检测到碰撞后,如何改变玩家得分的值?我想我已经弄清楚了:我使用了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!

最佳答案

你的代码是一场灾难,其中有许多逻辑位会阻止它运行。为了解决您的问题, turtle 有自己的 distance() 方法来测量 turtle 之间或 turtle 与位置之间的距离。使用它,您的 colliding() 方法可以简单如下:

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

如果您实际上要调用您的 colliding() 方法,但正如 @Hoog 指出的那样,您没有这样做。此代码中的其他精彩内容:

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

radius 属性从未定义。

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

尽管玩家的 x 位置在字典中更新,但 yield 却没有更新,所以这永远不会起作用。

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

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

关于Python Turtle 碰撞帮助和更新分数/生命,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55009976/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com