弹跳的小球
绘制一个圆
import pgzrun
def draw():
"""position radius color"""
screen.draw.circle((400,300),10,"yellow")
pgzrun.go()
#绘制实心圆
screen.draw.filled_circle((400,300),100,"white")
#设置背景填充色
screen.fill("white")
import pgzrun as pg
r = 1
#程序先运行update函数,再运行draw函数
def draw():
screen.fill("white")
screen.draw.circle((300,300),r,"yellow")
def update():
global r
r += 1
pg.go()
import pgzrun as pg
'''小球逐步下落'''
y = 1
#程序先运行update函数,再运行draw函数
#两个函数在每一帧都会执行
def update():
global y
y += 1
def draw():
screen.fill("white")
screen.draw.filled_circle((400,y),20,"yellow")
pg.go()
import pgzrun as pg
'''弹跳的小球'''
HEIGHT = 600
WIDTH = 800
direction_y = 5 #小球在Y方向前进的速度
direction_x = 3 #小球在X方向前进的速度
y,r,x = 20,20,WIDTH/2
#程序先运行update函数,再运行draw函数
#两个函数在每一帧都会执行
def update():
global y,direction_y,direction_x,x
y += direction_y
x += direction_x
#改变小球前进的方向
if y > HEIGHT - r or y < r:
direction_y = -direction_y
if x > WIDTH - r or x < r:
direction_x = - direction_x
def draw():
screen.fill("white")
screen.draw.filled_circle((x,y),r,"yellow")
pg.go()
美丽的圆圈画
import pgzrun
import random
HIGHT = 600
WIDTH = 800
r = 40
def draw():
screen.fill("white")
#循环X坐标
for k in range(r,WIDTH,2 * r):
#循环Y坐标
for j in range(r,HIGHT,2 * r):
#绘制同心圆
for i in range(r,0,-4):
screen.draw.filled_circle((k,j),i,(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
#检测鼠标事件,按下后执行,左键、中建和右键
def on_mouse_down():
draw()
pgzrun.go()
疯狂的小圆圈
import pgzrun as pg
import random
'''弹跳的小球'''
HEIGHT = 600
WIDTH = 800
balls = []
#程序先运行update函数,再运行draw函数
#两个函数在每一帧都会执行
def update():
global ball
#对列表中每个圆的位置信息进行更新
for ball in balls:
ball[1] += ball[3]
ball[0] += ball[2]
#改变小球前进的方向
if ball[1] > HEIGHT - ball[7] or ball[1] < ball[7]:
ball[3] = -ball[3]
if ball[0] > WIDTH - ball[7] or ball[0] < ball[7]:
ball[2] = -ball[2]
def draw():
screen.fill("white")
#遍历每个球的信息
for ball in balls:
#利用球的相关信息进行绘制最外面的大圆
screen.draw.filled_circle((ball[0],ball[1]),ball[7],(ball[4],ball[5],ball[6]))
#绘制该球的同心圆,每一帧的同心圆的颜色都不同
for x in range(ball[7],0,-2):
screen.draw.filled_circle((ball[0],ball[1]),x,(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
#如果鼠标移动,则执行该函数
def on_mouse_move(pos,rel,buttons):
#如果鼠标移动,并且按下了左键,那么则生成一个球
if mouse.LEFT in buttons:
r = random.randint(5,20)
x = random.randint(r,WIDTH - r)
y = random.randint(r,HEIGHT - r)
speed_y = random.randint(3,8)
speed_x = random.randint(3,8)
colorR = random.randint(0,255)
colorG = random.randint(0,255)
colorB = random.randint(0,255)
ball = [x,y,speed_x,speed_y,colorR,colorG,colorB,r]
balls.append(ball)
pg.go()
飞翔的小鸟
import pgzrun
import random
WIDTH = 350
HEIGHT = 600
background = Actor("C:/Users/Administrator/Desktop/images/background.jpg")
brid = Actor("C:/Users/Administrator/Desktop/images/bird.png")
bar_up = Actor("C:/Users/Administrator/Desktop/images/bar_up.png")
bar_down = Actor("C:/Users/Administrator/Desktop/images/bar_down.png")
#设定相关角色的初始位置
brid.x = 200
brid.y = HEIGHT/2
bar_up.x = 300
bar_up.y = 0
bar_down.x = 300
bar_down.y = 600
#设定分数和速度值
score = 0
speed = 2
def update():
global score,speed
brid.y += 1
bar_up.x = bar_up.x -speed
bar_down.x = bar_down.x -speed
#如果发现障碍物已经移动到最左边
if bar_up.x < 0 and bar_down.x < 0:
score += 1 #分数+1
#让障碍物跑到右边
bar_up.x = WIDTH
bar_down.x = WIDTH
#障碍物的垂直方向左边随机选择
bar_up.y = random.randint(-200,200)
bar_down.y = 600 + bar_up.y
#分数是5的倍数,则加快速度
if score % 5 == 0:
speed += 1
#如果小鸟碰到障碍物或者跑到屏幕的边缘,则游戏失败
if brid.colliderect(bar_up) or brid.colliderect(bar_down) or brid.y < 0 or brid.y >HEIGHT:
print("游戏失败")
def draw():
#绘制相关角色
background.draw()
bar_up.draw()
bar_down.draw()
brid.draw()
#显示分数
screen.draw.text(str(score),(30,30),fontsize = 50,color = "green")
#鼠标点击事件
def on_mouse_down():
brid.y -= 30
pgzrun.go()

见缝插针
import pgzrun
needle_player = Actor("C:/Users/Administrator/Desktop/images/needle.jpg")
WIDTH = 800
HEIGHT = 600
rotateSpeed = 1
score = 0
needle_player.x = 2
needle_player.y = 300
needles = [] #设置一个针的列表
#将第一根旋转针放在圆当中
needle = Actor("C:/Users/Administrator/Desktop/images/needle.jpg",anchor = (230,1.5))
needle.x = 400
needle.y = 300
needles.append(needle)
def update():
global needles
#遍历列表当中的内容,调整没根针的角度,进行旋转
for needle in needles:
needle.angle += rotateSpeed
def draw():
screen.fill("white") #填充屏幕背景色
needle_player.draw() #绘制玩家
#绘制列表当中每个针
for needle in needles:
needle.draw()
#绘制红色圆形
screen.draw.filled_circle((400,300),80,"red")
#显示分数
screen.draw.text("score:"+str(score),(100,30),fontsize = 50,color = "green")
#如果停止,则显示Game over
if rotateSpeed == 0:
screen.draw.text("Game over",(300,30),fontsize = 50,color = "green")
#当按下任意按键时
def on_key_down():
global rotateSpeed,score
#新建一个针
needle = Actor("C:/Users/Administrator/Desktop/images/needle.jpg",anchor = (230,1.5))
needle.x = 400
needle.y = 300
#遍历所有针,看看是否发生碰撞,如果碰撞,则游戏结束
for needle_a in needles:
if needle.colliderect(needle_a):
rotateSpeed = 0
#将新的针添加到列表当中
needles.append(needle)
#如果发现游戏没有结束,则将分数增加
if rotateSpeed != 0 :
score += 1
pgzrun.go()
![图片[2]-Python趣味游戏编程-四曲博客](https://pic.imgdb.cn/item/61e7731f2ab3f51d91bf5501.jpg)
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容