猫和老鼠电脑版游戏

发布时间: 2023-04-16 20:34 阅读: 文章来源:转载

点击上方关注订阅黑码教主获取更多精彩内容

写出一个猫和老鼠的游戏。游戏者使用方向键来控制老鼠,使其保持在猫的前方(由计算机控制猫)。保持时间越长,得分越高。

import turtleimport timeboxsize = 200caught = Falsescore = 0# functions that are called on keypressesdef up(): mouse.forward(10) checkbound()def left(): mouse.left(45)def right(): mouse.right(45)def back(): mouse.backward(10) checkbound()def quitTurtles(): window.bye()# stop the mouse from leaving the square set by box sizedef checkbound(): global boxsize if mouse.xcor() > boxsize: mouse.goto(boxsize, mouse.ycor()) if mouse.xcor() < -boxsize: mouse.goto(-boxsize, mouse.ycor()) if mouse.ycor() > boxsize: mouse.goto(mouse.xcor(), boxsize) if mouse.ycor() < boxsize: mouse.goto(mouse.xcor(), -boxsize)# set up screenwindow = turtle.Screen()mouse = turtle.Turtle()cat = turtle.Turtle()mouse.penup()mouse.penup()mouse.goto(100, 100)# add key listenerswindow.onkeypress(up, ‘Up‘)window.onkeypress(left, ‘Left‘)window.onkeypress(right, ‘Right‘)window.onkeypress(back, ‘Down‘)window.onkeypress(quitTurtles, ‘Escape‘)difficulty = window.numinput(‘Difficulty‘, ‘Enter a difficulty from easy (1), for hard(5)‘, minval=1, maxval=5)window.listen()# main loop# note how it changes with difficultywhile not caught: cat.setheading(cat.towards(mouse)) cat.forward(8 + difficulty) score = score + 1 if cat.distance(mouse) < 5: caught = True time.sleep(0.2 - (0.01 * difficulty))window.textinput(‘Game Over‘, ‘Well done. You scored: ‘ + str(score * difficulty))window.bye()

如果你觉得本篇还不错,请点赞关注!

文章由黑码教主创作,配图源于网络版权归原作者所有,如有侵权联系删除!

•••展开全文
相关文章