[英]Score/points update in beginner python turtle game
I'm a newbie to python and I'm having a problem with the score in my turtle game.我是 python 的新手,我的海龟游戏得分有问题。 The score is updated the first time I collect the ball, but it does not update when I collect the ball on any subsequent occasion.比分在我第一次接球时更新,但在我以后任何时候接球时都不会更新。 I would like the score to increase by a number eg 2 every time a ball is collected.我希望每次收集球时分数增加一个数字,例如 2。
Might someone be able to offer a solution?也许有人能够提供解决方案? I suspect the issue could lie in:我怀疑问题可能在于:
import turtle
import random
import math
screen = turtle.Screen()
screen.title("My game by python code")
screen.bgcolor("black")
screen.setup(width=600, height=600)
# Making the user 'bubble'
bubble = turtle.Turtle()
bubble.color("red")
bubble.shape("circle")
bubble.penup()
speed = 3
# Making the collection balls
collection_ball = turtle.Turtle()
collection_ball.color("red")
collection_ball.penup()
collection_ball.shape("circle")
collection_ball.shapesize(0.5, 0.5, 0.5)
ball_cor1 = random.randint(30, 280)
ball_cor2 = random.randint(30, 280)
collection_ball.setposition(ball_cor1, ball_cor2)
collection_ball.color("yellow")
# Scoring
points = turtle.Turtle()
points.color("yellow")
style = ('Courier', 30, 'italic')
points.penup()
points.goto(-200, 250)
points.write("Points: 0", font=style)
points.hideturtle()
# Turning
def turn_left():
bubble.left(90)
def turn_right():
bubble.right(90)
# Collection of the balls
def collection(a, b):
return abs(a.xcor() - b.xcor()) < 10 and abs(a.ycor() - b.ycor()) < 20
def collection_ball_restart():
collection_ball.color("black")
ball_cor1 = random.randint(30, 280)
ball_cor2 = random.randint(30, 280)
collection_ball.goto(ball_cor1, ball_cor2)
collection_ball.color("yellow")
bubble.forward(speed)
screen.ontimer(play_game, 10)
def play_game():
if collection(bubble, collection_ball):
score = 0
score += 2
points.clear()
points.write("Points: " + str(score), font=style)
collection_ball_restart()
bubble.forward(speed)
else:
bubble.forward(speed)
screen.ontimer(play_game, 10)
turtle.onkeypress(turn_left, "Left")
turtle.onkeypress(turn_right, "Right")
screen.listen()
play_game()
screen.mainloop()
In addition to the score
initialization issue that @TimRoberts points out (+1), below is a rework of your code to simplify the logic:除了@TimRoberts 指出的score
初始化问题 (+1) 之外,下面是对代码的返工以简化逻辑:
from turtle import Screen, Turtle
from random import randint
FONT_STYLE = ('Courier', 30, 'italic')
screen = Screen()
screen.title("My game by python code")
screen.bgcolor('black')
screen.setup(width=600, height=600)
# Making the user 'bubble'
bubble = Turtle()
bubble.color('red')
bubble.shape('circle')
bubble.penup()
# Making the collection balls
collection_ball = Turtle()
collection_ball.color('yellow')
collection_ball.shape('circle')
collection_ball.shapesize(0.5)
collection_ball.penup()
ball_cor1 = randint(30, 280)
ball_cor2 = randint(30, 280)
collection_ball.setposition(ball_cor1, ball_cor2)
# Scoring
score = 0
speed = 3
points = Turtle()
points.hideturtle()
points.color('yellow')
points.penup()
points.goto(-200, 250)
points.write("Points: 0", font=FONT_STYLE)
# Turning
def turn_left():
bubble.left(90)
def turn_right():
bubble.right(90)
# Collection of the balls
def was_collected(bubble):
return bubble.distance(collection_ball) < 15
def collection_ball_reset():
collection_ball.hideturtle()
collection_ball.goto(randint(30, 280), randint(30, 280))
collection_ball.showturtle()
def play_game():
global score
if was_collected(bubble):
score += 2
points.clear()
points.write("Points: " + str(score), font=FONT_STYLE)
collection_ball_reset()
bubble.forward(speed)
screen.ontimer(play_game, 10)
screen.onkeypress(turn_left, 'Left')
screen.onkeypress(turn_right, 'Right')
screen.listen()
play_game()
screen.mainloop()
Eg use hideturtle()
and showturtle()
instead of color tricks;例如,使用hideturtle()
和showturtle()
代替颜色技巧; minimize necessary calls to ontimer()
;尽量减少对ontimer()
的必要调用; use built-in distance function.使用内置距离 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.