python小学生编程小游戏打地鼠turtle

老鼠和锤子都是自己画的

应用了turtle的监听模块

可以通过按下键盘一些按键改变显示的图形,进行打地鼠游戏的编程

主要应用知识点:监听:本次写的是上下左右键,执行函数操作

turtle.listen()
turtle.onkey(lambda :shang(),'Up')
turtle.onkey(lambda :xia(),'Down')
turtle.onkey(lambda :zuo(),'Left')
turtle.onkey(lambda :you(),'Right')

锤子的图像和老鼠的图像都是自己画的,可能不是很好看嘿嘿

python小学生编程小游戏打地鼠turtle_第1张图片

按键后出现锤子,然后老鼠和锤子同时消失,使用的就是画了一个黑色的圆遮住

知识点

import turtle
turtle.bgcolor("red")#设置背景颜色为红色
turtle.penup()#抬笔
turtle.pendown()#落笔
turtle.goto(100,100)#前往坐标(100,100)
turtle.pensize(5)#画笔粗细为5
turtle.pencolor("blue")#画笔颜色为白色
turtle.forward(100)#顺着海龟方向画单位为100的直线
turtle.begin_fill()#开始填充
turtle.fillcolor("black")#填充颜色为黑色
turtle.end_fill()#结束填充 将需要填充的形状写在两者中间
turtle.circle(100)#画半径为100的圆
turtle.left(90)#海龟方向向左旋转90度
turtle.right(90)#海龟方向向右旋转90度

完整代码

import turtle
import random
import time
turtle.bgcolor('black')
turtle.hideturtle()
turtle.speed(0)
turtle.delay(0)

turtle.penup()
turtle.goto(320,180)
turtle.pendown()
turtle.pencolor("white")
turtle.write("游戏说明:\n控制键:\n↑ 打上面地鼠\n↓ 打下面地鼠\n← 打左面地鼠\n→ 打右面地鼠\n",align="right",font=("楷体", 14,"normal"))
turtle.pencolor("black")

def chuizi():
    turtle.hideturtle()
    turtle.begin_fill()
    turtle.fillcolor("#804000")
    turtle.left(135)
    turtle.forward(50)
    turtle.left(90)
    turtle.forward(30)
    turtle.right(90)
    turtle.forward(30)
    turtle.right(90)
    turtle.forward(65)
    turtle.right(90)
    turtle.forward(30)
    turtle.right(90)
    turtle.forward(30)
    turtle.left(90)
    turtle.forward(50)
    turtle.right(90)
    turtle.forward(5)
    turtle.end_fill()
    turtle.penup()
    turtle.home()

def laoshu():
    turtle.begin_fill()
    turtle.fillcolor("#D3D3D3")
    turtle.forward(50)
    turtle.left(90)
    turtle.forward(60)
    turtle.circle(25, 180)
    turtle.forward(60)
    turtle.right(180)
    turtle.forward(60)
    turtle.right(90)
    turtle.end_fill()
    turtle.penup()
    turtle.forward(15)
    turtle.pendown()
    turtle.begin_fill()
    turtle.fillcolor("black")
    turtle.circle(3)
    turtle.end_fill()
    turtle.penup()
    turtle.forward(18)
    turtle.pendown()
    turtle.begin_fill()
    turtle.fillcolor("black")
    turtle.circle(3)
    turtle.end_fill()
    turtle.penup()
    turtle.right(180)
    turtle.forward(6)
    turtle.left(90)
    turtle.forward(5)
    turtle.pendown()
    turtle.right(90)
    turtle.forward(6)
    turtle.right(180)
    turtle.forward(3)
    turtle.right(90)
    turtle.forward(5)
    turtle.circle(3, 180)
    turtle.penup()
    turtle.left(90)
    turtle.forward(12)
    turtle.left(90)
    turtle.pendown()
    turtle.circle(3, 180)
    turtle.penup()
    turtle.home()

def shang():
    turtle.penup()
    turtle.goto(100,200)
    chuizi()
    time.sleep(0.5)
    turtle.pendown()
    turtle.pencolor('black')
    turtle.color('black')
    turtle.begin_fill()
    turtle.circle(150)
    turtle.end_fill()
    turtle.penup()
    turtle.home()

def xia():
    turtle.penup()
    turtle.goto(100,-200)
    turtle.pendown()
    chuizi()
    time.sleep(0.5)
    turtle.penup()
    turtle.goto(50, -300)
    turtle.pendown()
    turtle.pencolor('black')
    turtle.color('black')
    turtle.begin_fill()
    turtle.circle(150)
    turtle.end_fill()
    turtle.penup()
    turtle.home()

def zuo():
    turtle.penup()
    turtle.goto(-100,0)
    turtle.pendown()
    chuizi()
    time.sleep(0.5)
    turtle.penup()
    turtle.goto(-100,-50)
    turtle.pendown()
    turtle.pencolor('black')
    turtle.color('black')
    turtle.begin_fill()
    turtle.circle(130)
    turtle.end_fill()
    turtle.penup()
    turtle.home()

def you():
    turtle.penup()
    turtle.goto(300,0)
    turtle.pendown()
    chuizi()
    time.sleep(0.5)
    turtle.penup()
    turtle.goto(250,-100)
    turtle.pendown()
    turtle.pencolor('black')
    turtle.color('black')
    turtle.begin_fill()
    turtle.circle(150)
    turtle.end_fill()
    turtle.penup()
    turtle.home()


turtle.listen()
turtle.onkey(lambda :shang(),'Up')
turtle.onkey(lambda :xia(),'Down')
turtle.onkey(lambda :zuo(),'Left')
turtle.onkey(lambda :you(),'Right')

def huayuan():
    colors = random.choice(["pink", "yellow", "white", "red", "green"])
    x=random.choice([0,-200,200])
    if x==0:
        y=random.choice([-200,200])
    else:
        y=0
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    laoshu()

for i in range(10):
    huayuan()
    time.sleep(2)
    turtle.penup()
    turtle.home()
turtle.done()

 

你可能感兴趣的:(python,python)