美文网首页
python小游戏学习笔记2(speed 轨迹)

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

作者: Bis_12e2 | 来源:发表于2020-11-06 13:33 被阅读0次

    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()
    

    相关文章

      网友评论

          本文标题:python小游戏学习笔记2(speed 轨迹)

          本文链接:https://www.haomeiwen.com/subject/ktjtbktx.html