python小游戏学习笔记2(speed 轨迹)

1,画同心圆

import pgzrun

def draw():
    screen.fill('white')
    screen.draw.filled_circle((400, 300), 300, 'black')
    screen.draw.filled_circle((400, 300), 250, 'white')
    screen.draw.filled_circle((400, 300), 200, 'black')
    screen.draw.filled_circle((400, 300), 150, 'white')
    screen.draw.filled_circle((400, 300), 100, 'black')

pgzrun.go()

2,让小球按照方形轨迹运动

import pgzrun
y = 200
x = 200
speed_y = 20
speed_x = 14
r = 10

def draw(): #每帧重复执行
    #screen.fill('white') 
    screen.draw.filled_circle((x, y), r, 'red')

def update():#每帧重复操作

    global x, y, speed_y, speed_x  # global的意思是把y和speed_y强制成为全局变量
    x = x + speed_x

    if x >= 600:
       y = y + speed_y
       x = x - speed_x
    if y >= 400:
        x = x - speed_x - speed_x
    if x <= 200:
        y = y - speed_y
        x = x - speed_x
    if y <= 200:
        y = y + speed_y
        x = x + speed_x
   #python的中心点在screen的左上角, python窗口默认800*600,30是半径
pgzrun.go()

3,小球弹跳有轨迹,只要把 screen.fill('white')去掉

import pgzrun


y = 100
x = 100
speed_y = 20
speed_x = 20 #通过调节这里的数值改变弹跳地点


def draw():
    screen.fill('white')
    screen.draw.filled_circle((x, y), 30, 'red')#python的中心点在screen的左上角, python窗口默认800*600,30是半径


def update():
    global x, y,speed_y,speed_x#global的意思是把y和speed_y强制成为全局变量
    y = y + speed_y
    x = x + speed_x

    if x>=770 or x<=30:#因为求的半径是30
        speed_x = - speed_x
    if y <= 30 or y>=570:
        speed_y = - speed_y


pgzrun.go()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。