python趣味小游戏之多彩小球球
程序员文章站
2022-06-22 09:33:34
用PYTHON写出多彩小球球,编写这个程序主要是练习**list[]**的运用#coding = utf - 8import pgzrunimport randomheight = 600 #屏幕的高weith = 800 #屏幕的宽balls = []for i in range(100): x = random.randint(100,weith-100) #小球横坐标x y = random.randint(100,height-100) #小球....
用PYTHON写出多彩小球球,编写这个程序主要是练习**list[]**的运用
#coding = utf - 8
import pgzrun
import random
height = 600 #屏幕的高
weith = 800 #屏幕的宽
balls = []
for i in range(100):
x = random.randint(100,weith-100) #小球横坐标x
y = random.randint(100,height-100) #小球纵坐标y
speed_x = random.randint(1,5) #小球横向速度
speed_y = random.randint(1,5) #小球纵向速度
r = random.randint(5,50) #小球半径
color1 = random.randint(10,255) #小球3个颜色分量
color2 = random.randint(10,255)
color3 = random.randint(10,255)
ball = [x,y,speed_x,speed_y,r,color1,color2,color3] #小球列表信息
balls.append(ball) #将第i个小球的信息添加的ball中
def draw(): #绘制模块
screen.fill((255,255,255)) #白色背景
for ball in balls: #绘制所有的球
screen.draw.filled_circle((ball[0],ball[1]),ball[5],(ball[-1],ball[-2],ball[-3]))
def update(): #更新模块
for ball in balls:
ball[0] = ball[0] + ball[2] #更新x坐标
ball[1] = ball[1] + ball[3] #更新y坐标
if ball[0] >= weith - ball[5] or ball[0] <= ball[5]: #判断左右边界,x方向速度反向
ball[2] = -ball[2]
if ball[1] >= height - ball[5] or ball[1] <= ball[5]:
ball[3] = -ball[3]
pgzrun.go()
用鼠标增加互动操作。
#coding = utf - 8
import pgzrun
import random
height = 600 #屏幕的高
weith = 800 #屏幕的宽
balls = [] #储存所有小球的信息,初始化为空列表
def draw(): #绘制模块
screen.fill((255,255,255)) #白色背景
for ball in balls: #绘制所有的球
screen.draw.filled_circle((ball[0],ball[1]),ball[5],(ball[-1],ball[-2],ball[-3]))
def update(): #更新模块
for ball in balls:
ball[0] = ball[0] + ball[2] #更新x坐标
ball[1] = ball[1] + ball[3] #更新y坐标
if ball[0] >= weith - ball[5] or ball[0] <= ball[5]: #判断左右边界,x方向速度反向
ball[2] = -ball[2]
if ball[1] >= height - ball[5] or ball[1] <= ball[5]:
ball[3] = -ball[3]
def on_mouse_move(pos,rel,buttons):
if mouse.LEFT in buttons:
x = pos[0]
y = pos[1]
speed_x = random.randint(1,5) #小球横向速度
speed_y = random.randint(1,5) #小球纵向速度
r = random.randint(5,50) #小球半径
color1 = random.randint(10,255) #小球3个颜色分量
color2 = random.randint(10,255)
color3 = random.randint(10,255)
ball = [x,y,speed_x,speed_y,r,color1,color2,color3] #小球列表信息
balls.append(ball) #将第i个小球的信息添加的ball中
pgzrun.go()
本文地址:https://blog.csdn.net/SYSZ520/article/details/109242465