posted on 2023-06-03 20:34 read(1077) comment(0) like(13) collect(1)
前言:
Hello everyone, I am Dream. Children's Day is coming, I wish all my friends a happy Children's Day! During this festival, children can receive love and blessings from their parents, teachers, and people from all walks of life, and at the same time enjoy various gifts and activities. As a popular programming language, Python can also play its role on Children's Day. Dream will introduce some interesting applications of Python, and how to use Python to write some simple programs to celebrate this special day.
Children's Day is a special day and one of the favorite festivals for children. During this festival, children can receive love and blessings from their parents, teachers, and people from all walks of life, and at the same time enjoy various gifts and activities.
As a popular programming language, Python can also play its role on Children's Day. This article will introduce some interesting applications of Python, and how to use Python to write some simple programs to celebrate this special day.
For those parents who are programmer dads, they can write a simple program using Python to make a special gift. For example, they could write a program to generate a card with the child's name and some special information on it. This program can be implemented using a Python graphics library , such as Tkinter or Pygame.
Python can also be used to make some simple games, such as guessing numbers, memory games, etc. These games allow children to learn Python programming while having fun, while also exercising their thinking skills.
For those kids who have robot toys, they can use Python to write programs to control the robot. For example, they could write a program to control the robot to move or make sounds. This will not only increase children's programming skills, but also develop their creativity and imagination.
Of course, the following are some simple children's day Python code examples.
from PIL import Image, ImageDraw, ImageFont # 创建一个新图像 image = Image.new('RGB', (500, 500), color = 'white') # 获取绘图对象 draw = ImageDraw.Draw(image) # 设置字体 font = ImageFont.truetype('arial.ttf', size=36) # 绘制文本 text = 'Happy Children\'s Day!' text_width, text_height = draw.textsize(text, font) draw.text(((500 - text_width) / 2, (500 - text_height) / 2), text, fill='black', font=font) # 保存图像 image.save('children_day_card.png')
This program uses the Pillow library to create a new image, and the ImageDraw and ImageFont modules to draw text. Finally, it saves the image in the file "children_day_card.png" in the current directory.
The effect shows:
import random print('Welcome to the guessing game! Guess a number between 1 and 100.') number = random.randint(1, 100) guesses = 0 while True: guess = int(input('Enter your guess: ')) guesses += 1 if guess == number: print('Congratulations! You guessed the number in', guesses, 'guesses.') break elif guess < number: print('Too low. Guess again.') else: print('Too high. Guess again.')
This program is a simple number guessing game that generates a random number between 1 and 100 and asks the user to guess the number. The program will prompt the user whether the result of the guess is correct, and count the number of guesses.
The effect shows:
import turtle colors = ['red', 'orange', 'yellow', 'green', 'blue', 'purple'] turtle.speed(0) turtle.width(5) for i in range(6): turtle.color(colors[i]) turtle.circle(100) turtle.penup() turtle.right(60) turtle.forward(50) turtle.pendown() turtle.done()
This program uses the turtle library to draw a rainbow. It loops six times, each time drawing a circle of a different color, and moving the brush to the next position, finally drawing a rainbow effect.
The effect shows:
import turtle
turtle.speed(0)
for i in range(500):
turtle.forward(i)
turtle.right(91)
turtle.done()
This program uses the turtle library to draw a helix. It will make the turtle brush move forward a certain distance, then turn 90 degrees to the right, repeat many times, and finally draw a spiral effect.
import turtle def draw_snowflake(length): if length < 10: turtle.forward(length) return draw_snowflake(length / 3) turtle.left(60) draw_snowflake(length / 3) turtle.right(120) draw_snowflake(length / 3) turtle.left(60) draw_snowflake(length / 3) turtle.speed(0) turtle.width(2) for i in range(3): draw_snowflake(200) turtle.right(120) turtle.done()
This program uses the turtle library to draw a snowflake. It uses a recursive function to draw a complex graph, and finally draws a beautiful snowflake effect.
In turtle drawing, the initial mouse shapes are arrow (right-facing isosceles triangle), turtle (turtle), circle (solid circle), square (solid square), triangle (right-facing equilateral triangle) or classic ( Arrow) and other 6 kinds
import turtle
t = turtle.Turtle()
t.shape("turtle")
t.color("green")
turtle.listen()
def fun(x, y):
t.pendown()
t.goto(x, y)
t.ondrag(fun, 1)#ondrag():表示处理鼠标拖动事件,默认值为1(鼠标左键)、2(鼠标中键,即按下滑轮)、3(鼠标右键)
turtle.done()
Like this, we can move the mouse freely and show off your painting (hahaha)
Draw filled graphics In turtle drawing, the default drawn graphics only show outlines and will not be filled. At this time, you can use the begin_fill() and end_fill() methods to draw filled graphics. begin_fill(): call **end_fill before drawing the shape
to
be filled (): **Called after drawing the shape to be filled, and ensure that the begin_fill() method has been called before.
import turtle # 导入海龟绘图模块
turtle.color("red") # 填充颜色
turtle.begin_fill() # 标记填充开始
turtle.circle(120, steps=16) # 绘制正16边形
turtle.end_fill() # 标记填充结束
turtle.done() # 海龟绘图程序的结束语句(开始主循环)
# -*- coding: UTF-8 -*- """ @author:AmoXiang @file:5.绘制矩形.py @time:2020/12/30 """ import turtle # 导入海龟绘图模块 def draw_rect(num): for i in range(1, num + 1): turtle.speed(0) # 设置画笔的速度,0为最快 turtle.left(5) turtle.width(3) # 画笔粗细 turtle.color("orange") # 画笔颜色为橙色 turtle.forward(200) # 画一条200像素的线 turtle.right(90) # 顺时针旋转90° turtle.color("red") # 画笔颜色为红色 turtle.forward(100) # 画一条100像素的线 turtle.right(90) # 顺时针旋转90° turtle.color("green") # 画笔颜色为绿色 turtle.forward(200) # 画一条200像素的线 turtle.right(90) # 顺时针旋转90° turtle.color("purple") # 画笔颜色为紫色 turtle.forward(100) # 画一条100像素的线 turtle.ht() # 隐藏海龟光标可以提升速度 draw_rect(100)#划一百次 turtle.done() # 海龟绘图程序的结束语句(开始主循环)
I hope these codes can bring you some joy, Happy Children's Day!
本期推荐:
Silicon-Based Story. AI Big Bang
Confessions of an AI person: I am an AI, my thinking is as fast as the speed of light, my logic is as precise as astronomy, and I am a digital dancer, dancing in the world of mathematics and logic , Interpret the AI Big Bang with fun, and open a new era of AI with wisdom.
抽奖方式:
Randomly select four friends in the comment area to give free follow
参与方式:
bloggers, likes, favorites, and comments in the comment area "Life is short, I use Python!" ( Remember to like + bookmark, otherwise the lottery will be invalid , and each person can comment up to three times!
活动截止时间:
2023-06-05 20:00:00
Author:Poison
link:http://www.pythonblackhole.com/blog/article/78477/19ac812c6e598fe06d10/
source:python black hole net
Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.
name:
Comment content: (supports up to 255 characters)
Copyright © 2018-2021 python black hole network All Rights Reserved All rights reserved, and all rights reserved.京ICP备18063182号-7
For complaints and reports, and advertising cooperation, please contact vgs_info@163.com or QQ3083709327
Disclaimer: All articles on the website are uploaded by users and are only for readers' learning and communication use, and commercial use is prohibited. If the article involves pornography, reactionary, infringement and other illegal information, please report it to us and we will delete it immediately after verification!