Python 当两只海龟碰撞时更新分数
我正在做一个作业,我必须写一个小游戏。当一只乌龟与屏幕上的一个点(虫子)碰撞时,它会在左上角的分数值上加一分,并将虫子传送到另一个随机点。当它们碰撞时,我很难更新分数Python 当两只海龟碰撞时更新分数,python,turtle-graphics,python-3.7,Python,Turtle Graphics,Python 3.7,我正在做一个作业,我必须写一个小游戏。当一只乌龟与屏幕上的一个点(虫子)碰撞时,它会在左上角的分数值上加一分,并将虫子传送到另一个随机点。当它们碰撞时,我很难更新分数 我试图将分数更新放在游戏循环中,但没有成功,因为它一直告诉我值没有定义。我尝试用全局值来解决这个问题,但没有任何效果: 导入海龟 输入数学 随机输入 #为游戏设置常量。 窗户高度=300 窗宽=300 前进步=10#海龟向前移动了多少 转动步数=30#海龟转动多少度(以度为单位) 收缩系数=0.95#海龟移动时收缩了多少 DEAT
导入海龟
输入数学
随机输入
#为游戏设置常量。
窗户高度=300
窗宽=300
前进步=10#海龟向前移动了多少
转动步数=30#海龟转动多少度(以度为单位)
收缩系数=0.95#海龟移动时收缩了多少
DEATH_WIDTH=0.05#由于用户失败而停止游戏的大小
碰撞阈值=10#我们说如果两只海龟相距这么远,它们就会相撞
#相互之间
#定义函数
def game_设置():
“设置游戏窗口,一只虫子和玩家乌龟”
#创建屏幕
wn=tutle.Screen()
wn.屏幕尺寸(窗口高度、窗口宽度)
wn.bgcolor(“浅绿色”)
#创建玩家乌龟
玩家=乌龟。乌龟()
播放器颜色(“蓝色”)
玩家形状(“海龟”)
player.penup()
player.setpos(random.randrange(1301),random.randrange(1301))
#创建一个bug
bug1=海龟。海龟()
bug1.颜色(“黑色”)
bug1.形状(“圆”)
bug1.shapesize(拉伸宽度=0.2,拉伸长度=0.2)
bug1.penup()
错误1.速度(0)#错误没有移动
bug1.设置位置(-200200)
#创造得分乌龟
记分员=海龟。海龟()
记分员
记分员
记分员设置位置(-400360)
分数=0
scorestring=“分数:%s”%Score
score_keeper.write(scorestring,False,align=“left”,font=(“Arial”,14,“normal”))
返回(wn、球员、bug1、记分员)
def是_碰撞(玩家,bug1):
距离=(math.sqrt((player.xcor()-bug1.xcor())**2+(player.ycor()-bug1.ycor())**2))
如果距离小于碰撞阈值:
返回真值
其他:
返回错误
def main():
#设置窗口,播放海龟和虫子
(wn,玩家,bug1,记分员)=游戏设置()
#使箭头键移动玩家乌龟
键盘(播放器)
#如果希望游戏结束,请在下面的循环中将此veriableto设置为True。
游戏结束=错误
玩家宽度=获取宽度(玩家)
#这是主要的游戏循环-当游戏还没有结束,海龟足够大时,打印海龟的宽度
#在屏幕上。
而不是游戏结束,玩家宽度>死亡宽度:
#你的碰撞检测应该在这里
如果是碰撞(玩家,bug1):
bug1.setpos(random.randrange(1301),random.randrange(1301))
player.shapesize(拉伸宽度=1,拉伸长度=1)
玩家宽度=获取宽度(玩家)
player.showturtle()
打印(播放器宽度)
打印(“完成”)
wn.exitonclick()
main()
is\u collision()
函数发生时,它将score
的值加1,然后score\u keeper
海龟将该值打印在窗口中from turtle import Screen, Turtle
from random import randrange
from functools import partial
# Set up the constants for the game.
WINDOW_WIDTH, WINDOW_HEIGHT = 500, 500
FORWARD_STEP = 10 # how much does the turtle move forward
TURN_STEP = 30 # how much does the turtle turn (in degrees)
COLLISION_THRESHOLD = 10 # we say that two turtles collided if they are this much away from each other
CURSOR_SIZE = 20
FONT = ('Arial', 14, 'normal')
# Define functions
def is_collision(player, bug):
return player.distance(bug) < COLLISION_THRESHOLD
def random_position(turtle):
scale, _, _ = turtle.shapesize()
radius = CURSOR_SIZE * scale
return randrange(radius - WINDOW_WIDTH/2, WINDOW_WIDTH/2 - radius), randrange(radius - WINDOW_HEIGHT/2, WINDOW_HEIGHT/2 - radius)
def forward():
global score
player.forward(FORWARD_STEP)
if is_collision(player, bug):
bug.setposition(random_position(bug))
score += 1
score_keeper.clear()
score_keeper.write(f"Score: {score}", font=FONT)
# Set up the window for the game, a bug and the player turtle.
# Create the screen
screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT)
screen.bgcolor('light green')
# Create score turtle
score_keeper = Turtle(visible=False)
score_keeper.penup()
score_keeper.setposition(-230, 230)
score = 0
score_keeper.write(f"Score: {score}", font=FONT)
# Create a bug
bug = Turtle('circle', visible=False)
bug.shapesize(4 / CURSOR_SIZE)
bug.penup()
bug.setposition(random_position(bug))
bug.showturtle()
# Create player turtle
player = Turtle('turtle', visible=False)
player.color('blue')
player.speed('fastest')
player.penup()
player.setposition(random_position(player))
player.showturtle()
# make the arrow keys move the player turtle
screen.onkey(partial(player.left, TURN_STEP), 'Left')
screen.onkey(partial(player.right, TURN_STEP), 'Right')
screen.onkey(forward, 'Up')
screen.listen()
screen.mainloop()
从海龟导入屏幕,海龟
从随机输入范围
从functools导入部分
#为游戏设置常量。
窗宽,窗高=500500
前进步=10#海龟向前移动了多少
转动步数=30#海龟转动多少度(以度为单位)
碰撞阈值=10#我们说如果两只海龟相距如此之远,它们就会发生碰撞
光标大小=20
字体=('Arial',14,'normal')
#定义函数
def是_碰撞(玩家、错误):
返回玩家。距离(错误)<碰撞阈值
def随机_位置(海龟):
比例,u,u=turtle.shapesize()
半径=光标大小*比例
返回randrange(半径-窗宽/2,窗宽/2-半径),randrange(半径-窗高/2,窗高/2-半径)
def forward():
全球得分
球员向前(向前步)
如果发生碰撞(玩家、bug):
设置位置(随机位置(错误))
分数+=1
记分员
记分员。写(f“分数:{score}”,font=font)
#设置游戏窗口,一只虫子和玩家乌龟。
#创建屏幕
screen=screen()
屏幕设置(窗口宽度、窗口高度)
screen.bgcolor('浅绿色')
#创造得分乌龟
记分员=乌龟(可见=假)
记分员
记分员设置位置(-230230)
分数=0
记分员。写(f“分数:{score}”,font=font)
#创建一个bug
bug=海龟('圆圈',可见=假)
bug.shapesize(4/光标大小)
bug.penup()
设置位置(随机位置(错误))
bug.showturtle()
#创建玩家乌龟
玩家=乌龟(“乌龟”,可见=假)
player.color('blue'))
玩家速度(“最快”)
player.penup()
玩家设置位置(随机)_
from turtle import Screen, Turtle
from random import randrange
from functools import partial
# Set up the constants for the game.
WINDOW_WIDTH, WINDOW_HEIGHT = 500, 500
FORWARD_STEP = 10 # how much does the turtle move forward
TURN_STEP = 30 # how much does the turtle turn (in degrees)
COLLISION_THRESHOLD = 10 # we say that two turtles collided if they are this much away from each other
CURSOR_SIZE = 20
FONT = ('Arial', 14, 'normal')
# Define functions
def is_collision(player, bug):
return player.distance(bug) < COLLISION_THRESHOLD
def random_position(turtle):
scale, _, _ = turtle.shapesize()
radius = CURSOR_SIZE * scale
return randrange(radius - WINDOW_WIDTH/2, WINDOW_WIDTH/2 - radius), randrange(radius - WINDOW_HEIGHT/2, WINDOW_HEIGHT/2 - radius)
def forward():
global score
player.forward(FORWARD_STEP)
if is_collision(player, bug):
bug.setposition(random_position(bug))
score += 1
score_keeper.clear()
score_keeper.write(f"Score: {score}", font=FONT)
# Set up the window for the game, a bug and the player turtle.
# Create the screen
screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT)
screen.bgcolor('light green')
# Create score turtle
score_keeper = Turtle(visible=False)
score_keeper.penup()
score_keeper.setposition(-230, 230)
score = 0
score_keeper.write(f"Score: {score}", font=FONT)
# Create a bug
bug = Turtle('circle', visible=False)
bug.shapesize(4 / CURSOR_SIZE)
bug.penup()
bug.setposition(random_position(bug))
bug.showturtle()
# Create player turtle
player = Turtle('turtle', visible=False)
player.color('blue')
player.speed('fastest')
player.penup()
player.setposition(random_position(player))
player.showturtle()
# make the arrow keys move the player turtle
screen.onkey(partial(player.left, TURN_STEP), 'Left')
screen.onkey(partial(player.right, TURN_STEP), 'Right')
screen.onkey(forward, 'Up')
screen.listen()
screen.mainloop()