大家好,我是涵子码农。今天我会为大家带来两个pygame小程序。希望大家可以多多点赞,关注。
我们废话不多说,进入正题!
目录
一、“人间”大炮
1.1.提前准备
1.2.开始编写
1.3.知识分解
1.3.1.认识math.sin和math.cos
1.3.2.认识pygame.mixer和pygame.transform
1.3.3.开关思维
1.3.4.part
1.4.完整代码
二、海浪
2.1.提前准备
2.2.开始编写
2.3.完整代码
三、总结
小提示
没有pygame模块的朋友们要到cmd里面pip一下,程序如下:
pip install pygame
一、“人间”大炮
1.1.提前准备
导入pygame模块和math模块
# import pygame and math
import pygame as pg
import math
# init program
pg.init()
pg.mixer.init()
screen = pg.display.set_mode((900, 800))
pg.display.set_caption("Code5-2-1_pygame_CannonBomb", "2.0")
1.2.开始编写
这里我们先看一下代码:
def playSound():
# play sound
pg.mixer.music.load(r"素材\boom.wav")
pg.mixer.music.set_volume(0.5)
pg.mixer.music.play()
# class bomb
class Bomb():
def __init__(self):
self.color = (0, 0, 0)
self.dot = [200, 500]
self.r = 10
self.speedx = 0 # two speed
self.speedy = 0
# creat variables
# switch thinking
fps = pg.time.Clock()
power = 0
statueflag = 1
cannonflag = 1
bakage = 1
direction = 0
dirflag = 1
partflag = 1 # what part is now
# check quit and fps
while True:
fps.tick(60)
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
screen.fill((255, 255, 255))
pg.draw.line(screen, (0, 255, 0), (0, 580), (900, 580), 10)
# partflag change
if partflag == 1:
# make power to up and down
# don't worry += -1
# it just means -= 1
power += statueflag
# load image
# check statueflag and bakage
if power == 0:
statueflag = 1 # statue change
if power == 100:
statueflag = -1 # statue change
if pg.mouse.get_pressed()[0] and partflag == 1:
bakage = statueflag
statueflag = 0 # statue change
partflag = 2
# loop to wait
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
if pg.mouse.get_pressed()[2] and statueflag == 1 and partflag == 1:
statueflag = bakage # statue change
if pg.mouse.get_pressed()[1] and statueflag == 0:
power = 0 # power change
# draw
y = (1- power / 100) * 400 + 100
pg.draw.line(screen, (0, 0, 0), (60, 100), (60, 500), 50)
pg.draw.line(screen, (255, 0, 0), (60, y), (60, 500), 50)
# direction
if partflag == 2:
direction += dirflag
if direction < 0:
dirflag = 1
if direction > 90:
dirflag = -1
if pg.mouse.get_pressed()[0] == 1:
dirflag = 0
partflag = 3
# loop to wait
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
# load image
cannon = pg.image.load(r"素材\cannon.png")
cannon = pg.transform.scale(cannon,(100, 100))
cannon = pg.transform.rotate(cannon, direction)
screen.blit(cannon, (50, 500))
# part 3
if partflag == 3:
if pg.mouse.get_pressed()[0]:
# cos and sin!
# triangle!
# ready fire
bomb = Bomb()
bomb.speedx = power*math.cos(2 * math.pi/(360/direction))
bomb.speedy = power*math.sin(2 * math.pi/(360/direction))*(-1)
pg.draw.circle(screen, bomb.color, bomb.dot, bomb.r, 0)
partflag = 4
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
if partflag == 4:
# fire!
bomb.dot[0] += int(bomb.speedx) / 4
bomb.dot[1] += int(bomb.speedy) / 4
bomb.speedy += 0.09
pg.draw.circle(screen, bomb.color, bomb.dot, bomb.r, 0)
# if hits the ground...
if bomb.dot[1] >= 580:
partflag = 5
continue
if partflag == 5:
# bomb!
playSound()
boom = pg.image.load(r"素材\boom.png")
boom = pg.transform.scale(boom,(200, 200))
screen.blit(boom, (int(bomb.dot[0]), int(bomb.dot[1] - 75)))
partflag = 6
if partflag == 6:
# stop
pass
# display program
pg.display.update()
这里用到了很多个知识,我来讲一下几个知识。
1.3.知识分解
1.3.1.认识math.sin和math.cos
这就是sin函数。这个涉及到初中知识。由于我还是个四年级的小学生(这才知道啊~),所以我只会放一张图片和百度的讲解。大家看不懂或者已经知道了的可以略过此段。(蓝字为链接,可以打开百度百科讲解)
三角函数是数学中属于初等函数中的超越函数的一类函数。它们的本质是任意角的集合与一个比值的集合的变量之间的映射。通常的三角函数是在平面直角坐标系中定义的,其定义域为整个实数域。另一种定义是在直角三角形中,但并不完全。现代数学把它们描述成无穷数列的极限和微分方程的解,将其定义扩展到复数系。
由于三角函数的周期性,它并不具有单值函数意义上的反函数。
三角函数在复数中有较为重要的应用。在物理学中,三角函数也是常用的工具。
在RT△ABC中,如果锐角A确定,那么角A的对边与邻边的比便随之确定,这个比叫做角A 的正切,记作tanA
即tanA=角A 的对边/角A的邻边
同样,在RT△ABC中,如果锐角A确定,那么角A的对边与斜边的比便随之确定,这个比叫做角A的正弦,记作sinA
即sinA=角A的对边/角A的斜边
同样,在RT△ABC中,如果锐角A确定,那么角A的邻边与斜边的比便随之确定,这个比叫做角A的余弦,记作cosA
即cosA=角A的邻边/角A的斜边
1.3.2.认识pygame.mixer和pygame.transform
pygame中的mixer库可以放出音乐
pygame中的transform库可以把图片进行很多处理
更多知识到后面会进行更加细致的学习~
1.3.3.开关思维
开关思维,就是一种思维上的开关。
开关控制着程序的运行,进度条的上下运动,大炮的发射,都由开关控制。
1.3.4.part
part把程序分为几个部分
就好比如:part1:进度条运动
if partflag == 1:
# make power to up and down
# don't worry += -1
# it just means -= 1
power += statueflag
# load image
# check statueflag and bakage
if power == 0:
statueflag = 1 # statue change
if power == 100:
statueflag = -1 # statue change
if pg.mouse.get_pressed()[0] and partflag == 1:
bakage = statueflag
statueflag = 0 # statue change
partflag = 2
# loop to wait
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
if pg.mouse.get_pressed()[2] and statueflag == 1 and partflag == 1:
statueflag = bakage # statue change
if pg.mouse.get_pressed()[1] and statueflag == 0:
power = 0 # power change
part2:大炮转向
if partflag == 2:
direction += dirflag
if direction < 0:
dirflag = 1
if direction > 90:
dirflag = -1
if pg.mouse.get_pressed()[0] == 1:
dirflag = 0
partflag = 3
# loop to wait
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
......
1.4.完整代码
# import pygame and math
import pygame as pg
import math
# init program
pg.init()
pg.mixer.init()
screen = pg.display.set_mode((900, 800))
pg.display.set_caption("Code5-2-1_pygame_CannonBomb", "2.0")
def playSound():
# play sound
pg.mixer.music.load(r"素材\boom.wav")
pg.mixer.music.set_volume(0.5)
pg.mixer.music.play()
# class bomb
class Bomb():
def __init__(self):
self.color = (0, 0, 0)
self.dot = [200, 500]
self.r = 10
self.speedx = 0 # two speed
self.speedy = 0
# creat variables
# switch thinking
fps = pg.time.Clock()
power = 0
statueflag = 1
cannonflag = 1
bakage = 1
direction = 0
dirflag = 1
partflag = 1 # what part is now
# check quit and fps
while True:
fps.tick(60)
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
screen.fill((255, 255, 255))
pg.draw.line(screen, (0, 255, 0), (0, 580), (900, 580), 10)
# partflag change
if partflag == 1:
# make power to up and down
# don't worry += -1
# it just means -= 1
power += statueflag
# load image
# check statueflag and bakage
if power == 0:
statueflag = 1 # statue change
if power == 100:
statueflag = -1 # statue change
if pg.mouse.get_pressed()[0] and partflag == 1:
bakage = statueflag
statueflag = 0 # statue change
partflag = 2
# loop to wait
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
if pg.mouse.get_pressed()[2] and statueflag == 1 and partflag == 1:
statueflag = bakage # statue change
if pg.mouse.get_pressed()[1] and statueflag == 0:
power = 0 # power change
# draw
y = (1- power / 100) * 400 + 100
pg.draw.line(screen, (0, 0, 0), (60, 100), (60, 500), 50)
pg.draw.line(screen, (255, 0, 0), (60, y), (60, 500), 50)
# direction
if partflag == 2:
direction += dirflag
if direction < 0:
dirflag = 1
if direction > 90:
dirflag = -1
if pg.mouse.get_pressed()[0] == 1:
dirflag = 0
partflag = 3
# loop to wait
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
# load image
cannon = pg.image.load(r"素材\cannon.png")
cannon = pg.transform.scale(cannon,(100, 100))
cannon = pg.transform.rotate(cannon, direction)
screen.blit(cannon, (50, 500))
# part 3
if partflag == 3:
if pg.mouse.get_pressed()[0]:
# cos and sin!
# triangle!
# ready fire
bomb = Bomb()
bomb.speedx = power*math.cos(2 * math.pi/(360/direction))
bomb.speedy = power*math.sin(2 * math.pi/(360/direction))*(-1)
pg.draw.circle(screen, bomb.color, bomb.dot, bomb.r, 0)
partflag = 4
while pg.mouse.get_pressed()[0]:
# must check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
if partflag == 4:
# fire!
bomb.dot[0] += int(bomb.speedx) / 4
bomb.dot[1] += int(bomb.speedy) / 4
bomb.speedy += 0.09
pg.draw.circle(screen, bomb.color, bomb.dot, bomb.r, 0)
# if hits the ground...
if bomb.dot[1] >= 580:
partflag = 5
continue
if partflag == 5:
# bomb!
playSound()
boom = pg.image.load(r"素材\boom.png")
boom = pg.transform.scale(boom,(200, 200))
screen.blit(boom, (int(bomb.dot[0]), int(bomb.dot[1] - 75)))
partflag = 6
if partflag == 6:
# stop
pass
# display program
pg.display.update()
二、海浪
2.1.提前准备
和之前差不多:
import pygame as pg
import math, random
# init program
pg.init()
screen = pg.display.set_mode((1000, 800))
pg.display.set_caption("Code5-2-4_pygame_WaterWave", "2.0")
2.2.开始编写
定义变量,这里用到了random.uniform,这是用来随机小数的。
这里还有for循环嵌套。
开关思维又来了!
sin函数又来了!
# pos
pos = [0, 600]
# waveX
waveX = 0
# waveY
waveY = 0
# y
y = 1
# check loop and fps
fps = pg.time.Clock()
while True:
# fps tick
fps.tick(60)
# check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
# fill screen
screen.fill((255, 255, 255))
waveX += random.uniform(0.05, 0.1) # random.uniform is used for decimal representation
# switch thinking again!
waveY += y
if waveY == -5:
y = 1 # speed change
if waveY == 200:
y = -1 # speed change
# draw
for i in range(250):
# posX change
pos[0] = 4 * i
# posY change
for j in range(50):
# use math.sin
pos[1] = 10 * j + 600 + waveY * math.sin(i / 40 + waveX)
# draw circles
pg.draw.circle(screen, (0, 55, 120), pos, 10, 0)
# display program
pg.display.update()
2.3.完整代码
# import pygame, math and random
import pygame as pg
import math, random
# init program
pg.init()
screen = pg.display.set_mode((1000, 800))
pg.display.set_caption("Code5-2-4_pygame_WaterWave", "2.0")
# pos
pos = [0, 600]
# waveX
waveX = 0
# waveY
waveY = 0
# y
y = 1
# check loop and fps
fps = pg.time.Clock()
while True:
# fps tick
fps.tick(60)
# check quit
event = pg.event.poll()
if event.type == pg.QUIT:
pg.quit()
exit()
# fill screen
screen.fill((255, 255, 255))
waveX += random.uniform(0.05, 0.1) # random.uniform is used for decimal representation
# switch thinking again!
waveY += y
if waveY == -5:
y = 1 # speed change
if waveY == 200:
y = -1 # speed change
# draw
for i in range(250):
# posX change
pos[0] = 4 * i
# posY change
for j in range(50):
# use math.sin
pos[1] = 10 * j + 600 + waveY * math.sin(i / 40 + waveX)
# draw circles
pg.draw.circle(screen, (0, 55, 120), pos, 10, 0)
# display program
pg.display.update()
三、总结
你们觉得python好用吗?我觉得挺好用的。
作为一个初级编程学习者,python适用于任何人。
我认为,大家可以多多编程。同时增长知识。
大家高考加油!一起度过疫情难关!
好文推荐
发表评论