News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

2024-11(6)

原力计划

posted on 2023-06-03 20:34     read(1077)     comment(0)     like(13)     collect(1)


insert image description here

前言: 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.

1. Introduction

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.

1. A gift from the programmer's father

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.

2. Small games

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.

3. Robot programming

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.

2. Fun children's day codes

Of course, the following are some simple children's day Python code examples.

1. Children's Day card

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:
insert image description here

2. Guess the number game

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:
insert image description here

3. Draw a rainbow

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:
insert image description here

4. Draw a spiral

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.
insert image description here

5. Draw snowflakes

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.
insert image description here

3. Introduction to Turtle Drawing

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

1. Realize the free movement of the mouse

insert image description here

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)

2. Fill color

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()  # 海龟绘图程序的结束语句(开始主循环)

insert image description here

3. Simple example - draw a big circular fan

insert image description here

# -*- 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.insert image description here

抽奖方式: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



Category of website: technical article > Blog

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.

13 0
collect article
collected

Comment content: (supports up to 255 characters)