Life is short, I use Python!
As a new programmer, I started from scratch. At the beginning, I could only write simple programs, and the most common thing I did was to use IDLE’s shell experiments. But in the second year, I learned almost everything, and I grew by leaps and bounds...
first successful experience
This was my first successful and long program called "Calculating the Area of Plane Figures". It's not complicated, but it provided me with plenty of experience.
Its main technical content is not much, but it provides my morale.
"""
名称:平面图形面积计算
作者:李沐辰
"""
a=b=h=S=0
w="输入有误,请重新输入"
def getSrect(a,b):
S=a*b
return S
def getSsquare(a):
S=a*a
return S
def getSparallelogram(a,h):
S=a*h
return S
def getStriangle(a,h):
S=a*h/2
if S==int(S):
S=int(S)
return S
def getStrapezium(a,b,h):
S=(a+b)*h/2
if S==int(S):
S=int(S)
return S
def tick(string):
try:
string=int(string)
can=True
return can
except ValueError:
can=False
return can
def showinfo():
print("平面图形面积计算")
print("1、长方形")
print("2、正方形")
print("3、平行四边形")
print("4、三角形")
print("5、梯形")
print("6、退出")
while True:
showinfo()
key=input("请选择(序号):")
if key=="1":#长方形
a=input("请输入长:")
b=input("请输入宽:")
a2=tick(a)
b2=tick(b)
if not(a2 and b2):
print(w)
print("-"*30)
continue
else:
a=int(a)
b=int(b)
S=getSrect(a,b)
print("S=ab")
print(" ="+str(a)+"×"+str(b))
print(" ="+str(S))
print("-"*30)
elif key=="2":#正方形
a=input("请输入边长:")
a2=tick(a)
if not a2:
print(w)
print("-"*30)
continue
else:
a=int(a)
S=getSsquare(a)
print("S=a2")
print(" ="+str(a)+"×"+str(a))
print(" ="+str(S))
print("-"*30)
elif key=="3":#平行四边形
a=input("请输入底:")
h=input("请输入高:")
a2=tick(a)
h2=tick(h)
if not(a2 and h2):
print(w)
print("-"*30)
continue
else:
a=int(a)
h=int(h)
S=getSparallelogram(a,h)
print("S=ah")
print(" ="+str(a)+"×"+str(h))
print(" ="+str(S))
print("-"*30)
elif key=="4":#三角形
a=input("请输入底:")
h=input("请输入高:")
a2=tick(a)
h2=tick(h)
if not(a2 and h2):
print(w)
print("-"*30)
continue
else:
a=int(a)
h=int(h)
S=getStriangle(a,h)
print("S=ah÷2")
print(" ="+str(a)+"×"+str(h)+"÷2")
print(" ="+str(S))
print("-"*30)
elif key=="5":#梯形
a=input("请输入上底:")
b=input("请输入下底:")
h=input("请输入高:")
a2=tick(a)
b2=tick(b)
h2=tick(h)
if not(a2 and b2 and h2):
print(w)
print("-"*30)
continue
else:
a=int(a)
b=int(b)
h=int(h)
S=getStrapezium(a,b,h)
print("S=(a+b)h÷2")
print(" =("+str(a)+"+"+str(b)+")×"+str(h)+"÷2")
print(" ="+str(S))
print("-"*30)
elif key=="6":
a=input("你确定吗(Y或N)?")
if a.upper()=="Y":
print("谢谢使用。")
print("-"*30)
break
else:
print("-"*30)
else:
print(w)
print("-"*30)
continue
Accelerate to become great
Then, I followed suit and studied the "Volume Calculator". Same as above, there is no technical content. Indeed, I am more proficient in programming.
"""
名称:体积计算器
作者:李沐辰
"""
a=b=h=V=0
w="输入错误,请重新输入"
def cuboid(a,b,h):
V=a*b*h
return V
def cube(a):
V=a*a*a
return V
def tick(string):
try:
string=int(string)
can=True
return can
except:
can=False
return can
def showinfo():
print("体积计算器\n1、长方体\n2、正方体\n3、退出")
key=input("请选择功能序号:")
return key
while True:
key=showinfo()
if key=="1":
a=input("请输入长:")
b=input("请输入宽:")
h=input("请输入高:")
a2=tick(a)
b2=tick(b)
h2=tick(h)
if not(a2 and b2 and h2):
print(w)
print("-"*30)
continue
else:
a=int(a)
b=int(b)
h=int(h)
V=cuboid(a,b,h)
print("V=abh\n ="+str(a)+"×"+str(b)+"×"+str(h)+"\n ="+str(V))
print("-"*30)
continue
elif key=="2":
a=input("请输入棱长:")
a2=tick(a)
if not a2:
print(w)
print("-"*30)
continue
else:
a=int(a)
V=cube(a)
print("V=a3\n ="+str(a)+"3\n ="+str(V))
print("-"*30)
continue
elif key=="3":
key=input("你确定吗(Y或N)?")
if key.upper()=="Y":
print("谢谢使用")
break
else:
print("-"*30)
continue
else:
print(w)
print("-"*30)
continue
How Many Failures Behind the Success of Pygame Library
Entering pygame is something I have been looking forward to for a long time. But when I really entered the field, even though I read the book n times, I still failed. For example, the following "Star Wars" is a typical example of a project that I worked hard on in the early stage and turned into a bad project in the later stage.
import pygame
pygame.init()
windowSurface=pygame.display.set_mode([1500,1000])
BLACK=(0,0,0)
pygame.display.set_caption("星球大战")
pic=pygame.image.load("开始.png")
windowSurface.blit(pic,(0,0))
pygame.display.update()
Running=True
while Running:
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
windowSurface.fill(BLACK)
pic=pygame.image.load("飞机.png")
windowSurface.blit(pic,(750,500))
pygame.display.update()
picX=750
picY=500
Run=True
while Run:
for event in pygame.event.get():
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
picY -=20
windowSurface.fill(BLACK)
windowSurface.blit(pic,(picX,picY))
pygame.display.update()
if event.key==pygame.K_DOWN:
picY +=20
windowSurface.fill(BLACK)
windowSurface.blit(pic,(picX,picY))
pygame.display.update()
if event.key==pygame.K_LEFT:
picX -=20
windowSurface.fill(BLACK)
windowSurface.blit(pic,(picX,picY))
pygame.display.update()
if event.key==pygame.K_RIGHT:
picX +=20
windowSurface.fill(BLACK)
windowSurface.blit(pic,(picX,picY))
pygame.display.update()
The final result of Pygame is satisfactory
After absorbing the failures of the two mini-games "Star Wars" and "Jump", I learned from experience and learned Tkinter. I successfully programmed the game "Survival from Desperate Places" and improved it. Now I have compiled 16 versions. . Let me show you my latest version 4.0.
import pygame
import random
import sys
import tkinter as tk
import time
"""
名称:绝处逢生 4.0
作者:李沐辰
日期:2023.8.18
"""
#关于账户
try:
file_name=open("Name.txt","r")
name=file_name.read()
file_name.close()
except:
name_window=tk.Tk()
name_window.geometry("250x150+730+440")
name_window.title("创建一个新的账户")
name_lable=tk.Label(name_window,text="请输入名字:",fg="black",font=("楷体",21))
name_entry=tk.Entry(name_window)
def BeOK():
global name,file_name,name_entry,name_window
name=name_entry.get()
file_name=open("Name.txt","w")
file_name.write(name)
file_name.close()
name_window.destroy()
bn=tk.Button(name_window,text="OK",command=BeOK)
name_lable.pack()
name_entry.pack()
bn.pack()
name_window.mainloop()
try:
file_dj=open("For Alive.txt","r")
dj=int(file_dj.read())
file_dj.close()
except:
dj=1
file_dj=open("For Alive.txt","w")
file_dj.write("1")
file_dj.close()
try:
file_jyz=open("experience.txt","r")
jyz=int(file_jyz.read())
file_jyz.close()
except:
jyz=0
file_jyz=open("experience.txt","w")
file_jyz.write("0")
file_jyz.close()
pygame.init()
screen=pygame.display.set_mode([1500,1000])
pygame.display.set_caption("绝处逢生")
#设置初始变量
DODGERBLUE=(30,144,255)
DODGERBLUE2=(0,114,225)
SPRINGGREEN2=(0,255,140)
BLACK=(10,10,10)
RED=(255,0,0)
WHITE=(255,255,255)
#加载图片、音乐,添加时钟
GameStart=pygame.image.load("GameStart.png")
GameOver=pygame.image.load("GameOver.png")
RunGame=pygame.image.load("Game Run.png")
Win=pygame.image.load("Win.png")
me=pygame.image.load("Run 2.png")
knife=pygame.image.load("Sword.png")
knife2=pygame.image.load("Sword.png")
music=pygame.mixer.Sound("Video Game 2.wav")
music_win=pygame.mixer.Sound("Win.wav")
music_Oops=pygame.mixer.Sound("Oops.wav")
timer=pygame.time.Clock()
music.play(-1)
#等待开始
def wait():
screen.blit(GameStart,(0,0))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
file_dj=open("For Alive.txt","w")
file_dj.write(str(dj))
file_dj.close()
file_jyz=open("experience.txt","w")
file_jyz.write(str(jyz))
file_jyz.close()
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN:
return
screen.blit(GameStart,(0,0))
font=pygame.font.SysFont("SimHei",72)
string1="沐辰游戏 出品"
text1=font.render(string1,True,WHITE)
screen.blit(text1,(700,0))
pygame.display.update()
#说明
HelpOfGame=tk.Tk()
HelpOfGame.title("说明")
HelpOfGame.geometry("800x450+730+440")
lable_help=tk.Label(HelpOfGame,text=name+",欢迎使用!\n说明\n一、开始\n按下键盘任意键开始游戏。\n二、游戏中\n1、任务:\n(1)去一次敌方基地(红色方块),将获取的敌方信息带走。\n(2)尽可能安全的回到我国(绿色区域),避开飞刀。\n2、操作\n上:“↑”键。\n下:“↓”键。\n关闭:点击关闭按钮或按下esc键。\n关闭背景音乐:按F1键。\n开启音乐:按F2键。\n提醒:\n出现弹窗后,阅读上面内容,然后关闭,否则无法继续游戏。",font=("楷体",20))
lable_help.pack()
HelpOfGame.mainloop()
font=pygame.font.SysFont("SimHei",48)
#开始
while True:
picX = 750
picY = 500
pic2X = pic3X = 1500
pic2Y = pic3Y = random.randint(600, 650)
moveup = movedown = will = moveleft = moveright =False
mark = shangxian=xiaxian=0
xl=dj*50
wait()
while True:
screen.fill(DODGERBLUE)
pygame.draw.line(screen, BLACK, (0, 700), (1500, 700), 20)
pygame.draw.rect(screen, SPRINGGREEN2, (0, 700, 1500, 300), 0)
pygame.draw.rect(screen, RED, (700, 0, 100, 50))
pygame.draw.rect(screen, DODGERBLUE2, (690, -10, 120, 70), 10)
screen.blit(me,(picX,picY))
string2="躲避刀数:"+str(mark)
text2=font.render(string2,True,BLACK)
screen.blit(text2,(1100,0))
string7="血量:"+str(xl)
text7=font.render(string7,True,RED)
screen.blit(text7,(1100,100))
string8=time.strftime("%H:%M:%S",time.localtime())
text8=font.render(string8,True,BLACK)
screen.blit(text8,(1100,200))
for event in pygame.event.get():
if event.type==pygame.QUIT:
file_dj = open("For Alive.txt", "w")
file_dj.write(str(dj))
file_dj.close()
file_jyz = open("experience.txt", "w")
file_jyz.write(str(jyz))
file_jyz.close()
pygame.quit()
sys.exit()
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP and picY>=20:
moveup=True
elif event.key==pygame.K_DOWN:
movedown=True
elif event.key==pygame.K_F1:
music.stop()
music_win.stop()
music_Oops.stop()
elif event.key==pygame.K_F2:
music.play(-1)
elif event.key==pygame.K_ESCAPE:
file_dj = open("For Alive.txt", "w")
file_dj.write(str(dj))
file_dj.close()
file_jyz = open("experience.txt", "w")
file_jyz.write(str(jyz))
file_jyz.close()
pygame.quit()
sys.exit()
elif event.type==pygame.KEYUP:
if event.key==pygame.K_UP:
moveup=False
elif event.key==pygame.K_DOWN:
movedown=False
if moveup==True:
picY -=int(dj*0.5*2.5)
if movedown==True:
picY +=int(dj*0.5*2.5)
if pic2X<=225 or pic2X==picX:
try:
pic2Y = random.randint(shangxian, xiaxian)
except ValueError:
errorWindow=tk.Tk()
errorWindow.title("ValueError")
errorWindow.geometry("500x200+730+440")
errorLable=tk.Label(errorWindow,text="错误\n不规范操作导致random模块出现故障,\n无法使用。",fg="red",font=("楷体",20))
def Error():
global errorWindow,qingkuang
qingkuang="GameOver"
errorWindow.destroy()
be=tk.Button(errorWindow,text="OK",command=Error)
errorLable.pack()
be.pack()
errorWindow.mainloop()
pic2X=1350
mark +=1
else:
pic2X -=random.randint(30,80)
screen.blit(knife,(pic2X,pic2Y))
if pic3X<=225 or pic3X==picX:
try:
pic3Y = random.randint(shangxian, xiaxian)
except ValueError:
errorWindow = tk.Tk()
errorWindow.title("ValueError")
errorWindow.geometry("500x200+730+440")
errorLable = tk.Label(errorWindow, text="错误\n不规范操作导致random模块出现故障,\n无法使用。",fg="red", font=("楷体", 20))
def Error():
global errorWindow, qingkuang
qingkuang = "GameOver"
errorWindow.destroy()
be = tk.Button(errorWindow, text="OK", command=Error)
errorLable.pack()
be.pack()
errorWindow.mainloop()
break
pic3X=1500
mark +=1
else:
pic3X -=random.randint(30,105)
screen.blit(knife2,(pic3X,pic3Y))
HowLong=picX-pic2X
HowLong2=picY-pic2Y
HowLong3=picX-pic3X
HowLong4=picY-pic3Y
if (HowLong>-30 and HowLong<30 and HowLong2>-30 and HowLong2<30) or (HowLong3>-30 and HowLong3<30 and HowLong4>-30 and HowLong4<30):
xl -=50+int(random.randint(35,55)*0.25)
music_Oops.play()
if picY>=725 and will==True:
qingkuang="Win"
music_win.play()
break
if picY<=0:
shangxian=0
moveup=False
else:
shangxian=abs(int(picY-40))
if picY>=800:
movedown=False
if picY<=20:
will=True
if xl<=0:
qingkuang="GameOver"
music_Oops.play()
break
if picY>=700:
xiaxian=690
else:
xiaxian=int(picY+40)
pygame.display.update()
timer.tick(105)
string3="共躲避刀数为"+str(mark)+"刀"
text3=font.render(string3,True,BLACK)
string4="完成了任务①,未完成任务②"
text4=font.render(string4,True,BLACK)
string5="未完成任务①,未完成任务②"
text5=font.render(string5,True,BLACK)
if qingkuang=="GameOver":
jyz +=5+mark*0.125
screen.blit(GameOver,(0,0))
screen.blit(text3,(600,0))
if will==True:
screen.blit(text4,(400,100))
else:
screen.blit(text5,(400,100))
pygame.display.update()
OverWindow=tk.Tk()
OverWindow.title("GameOver")
OverWindow.geometry("300x200+730+440")
OverLable=tk.Label(OverWindow,text="GameOver\n您输了!",fg="red",font=("楷体",30))
OverLable.pack()
OverWindow.mainloop()
elif qingkuang=="Win":
jyz +=10+mark*0.25
screen.blit(Win,(0,0))
screen.blit(text3,(600,0))
pygame.display.update()
WinWindow=tk.Tk()
WinWindow.title("Win")
WinWindow.geometry("300x200+730+440")
WinLable=tk.Label(WinWindow,text="Win\n您赢了!",fg="blue",font=("楷体",30))
WinLable.pack()
WinWindow.mainloop()
if jyz>=125:
dj +=1
jyz -=25
window_sj=tk.Tk()
window_sj.title("升级")
window_sj.geometry("300x100+740+430")
lable_sj=tk.Label(window_sj,text="已将等级升为"+str(dj)+",剩余经验值为"+str(jyz),fg="red",font=("楷体",20))
lable_sj.pack()
window_sj.mainloop()
continue
Not very satisfied with other aspects
I have spent a lot of experience on pygame, and there are bound to be things I can't do well, such as the Chinese word segmentation jieba module, the word cloud module, and the turtle module.
Summarize
Although I have thoroughly mastered pygame, I am not very good in other aspects.
Come on! juvenile!
I still say the same thing:
Life is short, I use Python!