python设计“跳一跳”游戏的计分函数

设计“跳一跳”游戏的计分函数。“跳一跳”游戏中黑色小人从一个方块跳到另一个方块上会获得1分,如果跳到方块的中心点上会获得2分,连续跳到中心点会依次获得2分、4分、6分、……。函数传入一个列表,列表中用布尔值TrueFalse表示黑色小人是否跳到方块的中心点,函数返回游戏获得的分数。

#假设list01里的元素是跳的是否在方块中心点
list01=[1,0,1,0,1,1,1,0,0,1,1]
def get_score(point):
score=0
total_score=0
for i in point:
if i:
score+=2
total_score+=score
else:
score=1
total_score+=1
return total_score
print(get_score(list01))

run的结果:32

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一个简单的贪吃蛇游戏示例,使用Python和turtle库编写。以下是代码: ```python import turtle import time import random delay = 0.1 # 设置窗口 wn = turtle.Screen() wn.title("贪吃蛇小游戏") wn.bgcolor("green") wn.setup(width=600, height=600) wn.tracer(0) # 蛇头 head = turtle.Turtle() head.speed(0) head.shape("square") head.color("black") head.penup() head.goto(0, 0) head.direction = "stop" # 食物 food = turtle.Turtle() food.speed(0) food.shape("circle") food.color("red") food.penup() food.goto(0, 100) # 蛇身列表 segments = [] # 记分板 score = 0 high_score = 0 pen = turtle.Turtle() pen.speed(0) pen.shape("square") pen.color("white") pen.penup() pen.hideturtle() pen.goto(0, 260) pen.write("得分: 0 最高分: 0", align="center", font=("Courier", 24, "normal")) # 移动函数 def move(): if head.direction == "up": y = head.ycor() head.sety(y + 20) if head.direction == "down": y = head.ycor() head.sety(y - 20) if head.direction == "right": x = head.xcor() head.setx(x + 20) if head.direction == "left": x = head.xcor() head.setx(x - 20) # 改变方向函数 def go_up(): if head.direction != "down": head.direction = "up" def go_down(): if head.direction != "up": head.direction = "down" def go_right(): if head.direction != "left": head.direction = "right" def go_left(): if head.direction != "right": head.direction = "left" # 监听键盘事件 wn.listen() wn.onkeypress(go_up, "Up") wn.onkeypress(go_down, "Down") wn.onkeypress(go_right, "Right") wn.onkeypress(go_left, "Left") # 循环游戏 while True: wn.update() # 碰到边界判断 if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: time.sleep(1) head.goto(0, 0) head.direction = "stop" # 隐藏蛇身 for segment in segments: segment.goto(1000, 1000) # 清空蛇身列表 segments.clear() # 重置分数 score = 0 pen.clear() pen.write("得分: {} 最高分: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) # 碰到食物判断 if head.distance(food) < 20: x = random.randint(-290, 290) y = random.randint(-290, 290) food.goto(x, y) # 新增蛇身 new_segment = turtle.Turtle() new_segment.speed(0) new_segment.shape("square") new_segment.color("grey") new_segment.penup() segments.append(new_segment) # 增加分数 score += 10 if score > high_score: high_score = score pen.clear() pen.write("得分: {} 最高分: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) # 新增蛇身 for index in range(len(segments) - 1, 0, -1): x = segments[index - 1].xcor() y = segments[index - 1].ycor() segments[index].goto(x, y) if len(segments) > 0: x = head.xcor() y = head.ycor() segments[0].goto(x, y) move() # 碰到蛇身判断 for segment in segments: if segment.distance(head) < 20: time.sleep(1) head.goto(0, 0) head.direction = "stop" # 隐藏蛇身 for segment in segments: segment.goto(1000, 1000) # 清空蛇身列表 segments.clear() # 重置分数 score = 0 pen.clear() pen.write("得分: {} 最高分: {}".format(score, high_score), align="center", font=("Courier", 24, "normal")) time.sleep(delay) wn.mainloop() ``` 这个小游戏使用turtle库创建游戏窗口以及蛇和食物的图形。玩家需要通过键盘控制蛇的移动,去吃食物并且尽可能地长大,直到游戏结束。你可以根据自己的需求,修改代码来创建自己的贪吃蛇游戏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值