这是我第一次参加这种比赛,所以作品质量会有点差,游戏规则就在游戏里
代码
m=0
n=0
import turtle
import random
import time
screen=turtle.Screen()
a = screen.textinput(title="猫抓老鼠 双人模式 重磅来袭", prompt="游戏规则:吃老鼠最多的获胜。你们想抓几只老鼠:")
turtle.ht()
turtle.up()
turtle.goto(-350,250)
turtle.down()
turtle.pencolor("white")
turtle.pensize(10)
turtle.forward(700)
start_time=time.time()
used_time=0
game=turtle.Screen()
game.setup(700,700)
game.bgcolor('black')
game.title("猫抓老鼠 双人模式")
turtle.tracer(3)
cat=turtle.Turtle()
cat.color('red')
cat.shapesize(2,2)
cat.speed(0)
cat.up()
cat.goto(-100,0)
cat2=turtle.Turtle()
cat2.color('yellow')
cat2.shapesize(2,2)
cat2.speed(0)
cat2.up()
cat2.goto(100,0)
pen=turtle.Turtle()
pen.ht()
pen.up()
pen.setpos(0,270)
pen.color('red')
pen.write("计时" ,align="left",font=('Arial',20,'bold'))
pen2=turtle.Turtle()
pen2.ht()
pen2.up()
pen2.setpos(0,270)
pen2.color("red")
def update_time():
global used_time
now_used_time=int(time.time()-start_time)
if now_used_time>used_time:
used_time=now_used_time
time_str="计时"+str(used_time)
pen.clear()
pen.write(time_str,align="left",font=('Arial',20,'bold'))
rats_number=int(a)
rats=[]
for r in range(rats_number):
rat=turtle.Turtle()
rat.ht()
rat.up()
rat.speed(0)
rat.left(random.randint(0,360))
rat.color('sky blue')
x=random.randint(-340,340)
y=random.randint(-340,240)
rat.setpos(x,y)
rat.st()
rats.append(rat)
turtle.listen()
def move_left():
cat.left(30)
def move_right():
cat.right(30)
def speedup():
global cat_speed
cat_speed+=1
def slowdown():
global cat_speed
cat_speed-=1
def catch(rat):
if cat.distance(rat)<10:
global m
m+=1
rat.ht()
rats.remove(rat)
def move_left2():
cat2.left(30)
def move_right2():
cat2.right(30)
def speedup2():
global cat2_speed
cat2_speed+=1
def slowdown2():
global cat2_speed
cat2_speed-=1
def catch2(rat):
if cat2.distance(rat)<10:
global n
n+=1
rat.ht()
rats.remove(rat)
turtle.onkey(move_left2,'Left')
turtle.onkey(move_right2,'Right')
turtle.onkey(speedup2,'Up')
turtle.onkey(slowdown2,'Down')
turtle.onkey(move_left,'a')
turtle.onkey(move_right,'d')
turtle.onkey(speedup,'w')
turtle.onkey(slowdown,'s')
cat_speed=1
cat2_speed=1
while True:
update_time()
cat.fd(cat_speed)
cat2.fd(cat2_speed)
x=cat.xcor()
y=cat.ycor()
x2=cat2.xcor()
y2=cat2.ycor()
if x>350 or x<-350 or y>250 or y<-350:
cat.left(180)
if x2>350 or x2<-350 or y2>250 or y2<-350:
cat2.left(180)
for rat in rats:
rat.fd(1)
catch(rat)
catch2(rat)
x=rat.xcor()
y=rat.ycor()
if x>350 or x<-350 or y>250 or y<-350:
rat.left(180)
if len(rats)==0:
pen3=turtle.Turtle()
pen3.ht()
pen3.up()
pen3.setpos(-100,0)
pen3.color("red")
over_str="游戏结束,用时"+str(used_time)+"秒"
pen3.write(over_str,align="left",font=('Arial',20,'bold'))
pen4=turtle.Turtle()
pen4.ht()
pen4.up()
pen4.goto(-100,-100)
pen4.color('red')
if m>n:
mouse_number="1P获胜 吃了"+str(m)+"只"
pen4.write(mouse_number,align="left",font=('Arial',20,'bold'))
if m==n:pen4.write("平局",align="left",font=('Arial',20,'bold'))
if m<n:
mouse_number="2P获胜 吃了"+str(n)+"只"
pen4.write(mouse_number,align="left",font=('Arial',20,'bold'))
break
game.mainloop()
评论