1. 弹跳的小球
pygame
#!/usr/bin/env Python
# coding=utf-8
#解决gbk编码问题
import pygame
import os
WINDOW_W, WINDOW_H = 640, 800 # 窗户大小
FPS = 50 # 帧数,每秒多少次
g = 9.8*100 # 现实中单位:平方米/二次方秒,游戏中像素/二次方秒
pygame.init() # 初始化
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (200, 100)
screen = pygame.display.set_mode((WINDOW_W, WINDOW_H), pygame.DOUBLEBUF, 32) # 创建一个窗口
pygame.display.set_caption('米粒儿')
clock = pygame.time.Clock()
x, y = WINDOW_W/2, 10 # 小球的坐标
vx, vy = 0, 0 # 小球在x,y上的速度
while True: # 游戏主循环
for event in pygame.event.get(): # 遍历事件
if event.type == pygame.QUIT:
exit()
# 下一时刻的速度、位置计算
vy += + g * 1/FPS # v = v0 + at 只有y坐标的速度发生改变
x += vx * 1/FPS
y += vy * 1/FPS
if y >= WINDOW_H - 10: # 小球刚好触碰到地面的瞬间
vy = -vy # 到达地面时速度反方向
screen.fill((0, 0, 0)) # 该句非常重要,每一帧都需要屏幕填充,黑色屏幕更新用于遮盖上一次小球的画面
# 画球
pygame.draw.circle(screen, (255, 125, 0), (int(x), int(y)), 10) # 绘制圆心和半径,半径是10
# 刷新界面,每一帧变化
pygame.display.update()
# 设置FPS
time_passed = clock.tick(FPS)
pgzrun
import pgzrun, random
WIDTH = 800
HEIGHT = 600
x = random.uniform(100, WIDTH) # 小球最开始出现的x,y
y = random.uniform(100, HEIGHT) # 注意,x和y随机数的起始值不建议设置为0,会出现小球震荡的效果,至少大于等于小球半径
speed_y = 3
speed_x = 3
r = 30
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 屏幕填充
screen.draw.filled_circle((x, y), r, 'red') # 画小球,参数:坐标,半径,颜色
def update(): # 更新模块,每帧重复操作
global x, y, speed_x, speed_y
y += speed_y
x += speed_x
if y > HEIGHT - r or y < r: # 当小球碰壁时,分别讨论x和y坐标
speed_y = -speed_y # 速度反方向即可,如果只设置其中一个坐标,那么小球只会沿着直线反弹
if x > WIDTH - r or x < r:
speed_x = -speed_x
pgzrun.go()
2.测色弱
pgzrun模块
import pgzrun, random
WIDTH = 1200
HEIGHT = 800
# 修改半径可以修改圆的个数
R = random.randint(20, 60)
def draw():
# 至于是哪个圆颜色不同,通过随机的方式产生
x_i = random.randint(1, WIDTH // 2 // R)
y_i = random.randint(1, HEIGHT // 2 // R)
print(x_i, y_i)
for j in range(R, HEIGHT, 2*R):
for i in range(R, WIDTH, 2*R):
if j == (2*y_i - 1)*R and i == (2*x_i - 1)*R:
screen.draw.filled_circle((i, j), R, (0, 255, 255))
else:
screen.draw.filled_circle((i, j), R, (0, 255, 220))
# 按下鼠标,调用函数
def on_mouse_down():
draw()
pgzrun.go()
pygame模块
import pygame
from pygame.locals import *
import random
WIDTH = 800
HEIGHT = 800
pygame.init() # 初始化
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # 主界面
R_list = [20,40,50,80]
# R = random.randint(20, 60)
R = random.choice(R_list)
while True: # 游戏开始啦
for event in pygame.event.get(): # 获取事件
if event.type == QUIT: # 如果发现有退出的操作,游戏就结束啦
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
screen.fill((0, 0, 0))
x_i = random.randint(1, WIDTH // 2 // R)
y_i = random.randint(1, HEIGHT // 2 // R)
print(x_i,y_i)
for j in range(R, HEIGHT, 2*R):
for i in range(R, WIDTH, 2*R):
if j == (2*y_i - 1)*R and i == (2*x_i - 1)*R:
pygame.draw.circle(screen, (0,255,255), (i,j), R)
else:
pygame.draw.circle(screen, (0,255,220), (i,j), R)
pygame.display.update()
3. 多彩的圈圈
import pgzrun, random
R = 50
WIDTH = 1200 # 设置窗口宽度
HEIGHT = 800 # 设置窗口的高度
# 绘制模块,每帧重复更新
def draw():
for j in range(R, HEIGHT, 2*R):
for i in range(R, WIDTH, 2*R):
for k in range(R, 1, -10):
screen.draw.filled_circle((i, j), k, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
# 按下鼠标时,调用函数,即每次按下鼠标图像都会变化
def on_mouse_down():
draw()
pgzrun.go()
4. 满屏小球反弹
import pgzrun, random
WIDTH, HEIGHT = 1200, 800
x, y = 20, 20 # 小球最开始出现的x,y,初始值
speed_x, speed_y = 3, 3
R, G, B = 0, 255, 255
r = 30
balls = []
# 画100个小球,每个小球的属性都是随机的
for i in range(100):
ball = [x, y, speed_x, speed_y, r, R, G, B]
ball[4] = random.randint(2, 80)
ball[0] = random.randint(ball[4], WIDTH-ball[4]) # 此处设置的起始点最小值至少小球的半径,最大是界面边界-半径,否则会出现振荡
ball[1] = random.randint(ball[4], HEIGHT-ball[4])
ball[2] = random.randint(1, 5)
ball[3] = random.randint(1, 5)
ball[5] = random.randint(0, 255)
ball[6] = random.randint(0, 255)
ball[7] = random.randint(0, 255)
balls.append(ball)
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 放到for循环外边,否则会出现界面只有一个的小球
for ball in balls:
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7])) # 画小球,参数:坐标,半径,颜色
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] += ball[2]
ball[1] += ball[3]
if ball[1] > HEIGHT - ball[4] or ball[1] < ball[4]: # 当小球碰壁时,分别讨论x和y坐标
ball[3] = -ball[3] # 速度反方向即可,如果只设置其中一个坐标,那么小球只会沿着直线反弹
if ball[0] > WIDTH - ball[4] or ball[0] < ball[4]:
ball[2] = -ball[2]
pgzrun.go()
5. 鼠标拖动产生多个小球
点击并拖动鼠标,产生一连串的小球
import pgzrun, random
WIDTH, HEIGHT = 1200, 800
x, y = 20, 20 # 小球最开始出现的x,y,初始值
speed_x, speed_y = 3, 3
R, G, B = 0, 255, 255
r = 30
balls = []
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 放到for循环外边,否则会出现界面只有一个的小球
for ball in balls:
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7])) # 画小球,参数:坐标,半径,颜色
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] += ball[2]
ball[1] += ball[3]
if ball[1] > HEIGHT - ball[4] or ball[1] < ball[4]: # 当小球碰壁时,分别讨论x和y坐标
ball[3] = -ball[3] # 速度反方向即可,如果只设置其中一个坐标,那么小球只会沿着直线反弹
if ball[0] > WIDTH - ball[4] or ball[0] < ball[4]:
ball[2] = -ball[2]
def on_mouse_move(pos, rel, buttons): # 当鼠标拖动
if mouse.LEFT in buttons: # 当按下鼠标左键时,获取鼠标的实时位置
x = pos[0]
y = pos[1]
speed_x = random.randint(2, 5)
speed_y = random.randint(2, 5)
r = random.randint(2, 50)
R = random.randint(0, 255)
G = random.randint(0, 255)
B = random.randint(0, 255)
ball = [x, y, speed_x, speed_y, r, R, G, B]
balls.append(ball)
pgzrun.go() # 使用pgzrun运行此脚本
6. 鼠标点击产生单个小球
每点击一次鼠标就产生一个小球
import pgzrun, random
WIDTH, HEIGHT = 1200, 800
x, y = 20, 20 # 小球最开始出现的x,y,初始值
speed_x, speed_y = 3, 3
R, G, B = 0, 255, 255
r = 30
balls = []
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 放到for循环外边,否则会出现界面只有一个的小球
for ball in balls:
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7])) # 画小球,参数:坐标,半径,颜色
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] += ball[2]
ball[1] += ball[3]
if ball[1] > HEIGHT - ball[4] or ball[1] < ball[4]: # 当小球碰壁时,分别讨论x和y坐标
ball[3] = -ball[3] # 速度反方向即可,如果只设置其中一个坐标,那么小球只会沿着直线反弹
if ball[0] > WIDTH - ball[4] or ball[0] < ball[4]:
ball[2] = -ball[2]
def on_mouse_down(): # 当鼠标点击
ball = [x, y, speed_x, speed_y, r, R, G, B]
ball[4] = random.randint(2, 80)
ball[0] = random.randint(ball[4], WIDTH - ball[4]) # 此处设置的起始点最小值至少小球的半径,最大是界面边界-半径,否则会出现振荡
ball[1] = random.randint(ball[4], HEIGHT - ball[4])
ball[2] = random.randint(1, 5)
ball[3] = random.randint(1, 5)
ball[5] = random.randint(0, 255)
ball[6] = random.randint(0, 255)
ball[7] = random.randint(0, 255)
balls.append(ball)
pgzrun.go() # 使用pgzrun运行此脚本
7. 点击拖动鼠标产生连环的同心圆
import pgzrun, random
WIDTH, HEIGHT = 1200, 800
x, y = 20, 20 # 小球最开始出现的x,y,初始值
speed_x, speed_y = 3, 3
R, G, B = 0, 255, 255
r = 30
balls = []
def draw(): # 绘制模块,每帧重复执行
screen.fill('white') # 放到for循环外边,否则会出现界面只有一个的小球
for ball in balls:
screen.draw.filled_circle((ball[0], ball[1]), ball[4], (ball[5], ball[6], ball[7])) # 画小球,参数:坐标,半径,颜色
for radius in range(1, ball[4], 4):
screen.draw.filled_circle((ball[0], ball[1]), ball[4]-radius, # 关键2:办好不能设置成radius,画不同颜色的同心圆圆顺序是先画大圆再画小圆
(random.randint(ball[5], 255), # 关键1:在画同心圆时,每层圆的颜色必须随机,才能体现出同心圆不同颜色的效果
random.randint(ball[6], 255),
random.randint(ball[7], 255))
)
def update(): # 更新模块,每帧重复操作
for ball in balls:
ball[0] += ball[2]
ball[1] += ball[3]
if ball[1] > HEIGHT - ball[4] or ball[1] < ball[4]: # 当小球碰壁时,分别讨论x和y坐标
ball[3] = -ball[3] # 速度反方向即可,如果只设置其中一个坐标,那么小球只会沿着直线反弹
if ball[0] > WIDTH - ball[4] or ball[0] < ball[4]:
ball[2] = -ball[2]
def on_mouse_move(pos, rel, buttons): # 当鼠标拖动
if mouse.LEFT in buttons: # 当按下鼠标左键时
x = pos[0]
y = pos[1]
speed_x = random.randint(2, 5)
speed_y = random.randint(2, 5)
r = random.randint(2, 50)
R = random.randint(0, 255)
G = random.randint(0, 255)
B = random.randint(0, 255)
ball = [x, y, speed_x, speed_y, r, R, G, B]
balls.append(ball)
pgzrun.go() # 使用pgzrun运行此脚本
2. flappybird
pgzrun模块
import pgzrun, random
WIDTH = 350
HEIGHT = 600
# 在当前的代码文件同级目录下建一个images的文件,用于存放图片
bg = Actor('background')
bird = Actor('bird')
bar_down = Actor('bar_down')
bar_up = Actor('bar_up')
# 设置鸟的位置
bird.x = 50
bird.y = HEIGHT / 2
# 设置木桩down的位置
bar_up.x = WIDTH
bar_up.y = 0
# 设置木桩up的位置
bar_down.x = WIDTH
bar_down.y = HEIGHT
# 设置木桩移动的速度
speed_x = 1
# 分数
score = 0
def draw():
bg.draw()
bird.draw()
bar_down.draw()
bar_up.draw()
screen.draw.text(str(score), (20, 20), fontsize=50, color='red') # 分数显示
def update():
global score, speed_x
bird.y += 3 # 在不受鼠标控制下,鸟自然下落,y坐标变大
# 障碍物移动,从右往左移动
bar_down.x -= speed_x
bar_up.x -= speed_x
if bar_down.x < -20 and bar_up.x < -20:
score += 1 # 记分数,木桩顺利移至x的左边,说明没有碰到鸟
bar_down.x = 370 # 木桩再现时位置要比画布坐标大
bar_up.x = 370
bar_up.y = random.randint(-100, 200)
bar_down.y = 500 + bar_up.y # 改500参数,可以修改空隙大小
if score % 5 == 0: # 注意此处是包含在上一级if内,因为在木桩不到最左侧情况下,木桩分数%5=0的状态有很多
speed_x += 1
# 鸟和木桩碰到了
if bird.colliderect(bar_up) or bird.colliderect(bar_down) or bird.y < 0 or bird.y > HEIGHT:
score = 0 # 分数归0
speed_x = 1 # 速度归1
bird.x = 50 # 鸟的坐标
bird.y = HEIGHT / 2
bar_up.x = WIDTH # 上下木桩的位置
bar_up.y = 0
bar_down.x = WIDTH
bar_down.y = HEIGHT
def on_mouse_down():
bird.y -= 50 # 按下鼠标只对鸟的y坐标起作用
pgzrun.go()
3. 见缝插针
import pgzrun
needle = Actor('needle', anchor=(170+50, 1.5)) # 设置锚点,needle长宽分别为170,3
needle.x = 200 # 以锚点设置坐标x,y
needle.y = 300
speed = 1
score = 0
needle_list = []
needle_list.append(needle)
def draw():
screen.fill('white')
screen.draw.filled_circle((400, 300), 80, 'red')
for n in needle_list: # 在同一个背景同一个圆圈的情况下,绘制列表中的所有针
n.draw()
screen.draw.text(str(score), (40, 40), fontsize=50, color='red') # 分数
if speed == 0:
screen.draw.text('GameOver', (200, 80), fontsize=120, color='red') # 分数
def on_mouse_down(): # 鼠标/按键 on_mouse/key_down
global needle, speed, score
needle = Actor('needle', anchor=(170 + 50, 1.5)) # 提示要生成一根新的针
needle.x = 400
needle.y = 300
music.play_once('弹簧') # 针插入的音效
for n in needle_list:
if needle.colliderect(n):
print('游戏失败')
speed = 0 # 游戏失败就停止转动,即速度为0
music.play_once('溜走') # 游戏失败的音效
needle_list.append(needle)
if speed > 0: # 每创建完一根,判断当前速度,若大于0,则分数+1
score += 1
def update():
for n in needle_list:
if n.x == 400:
n.angle += speed
pgzrun.go()