processing游戏开发-移动的小球

原理

setup

这里负责初始化一些重要的变量, 实际上 在 外面初始化也可以 ,但是setup更加有保证

draw

这个是每一帧的更新 会执行的函数, 可以理解成渲染游戏 和执行游戏逻辑

代码

w,h = 600,400
a_ball = {'x':w//2,'y':h//2 }
w,h = 600,400
color_bg = 255
            
def setup():
    # window  size 
    # w 600 , h 400
    size(w,h)
    
def move(ball):
    # go right ,move 1 pixel
    ball['x'] +=1
    # if hit the wall (horizontal border at the right)
    if ball['x'] > w:
         # move the ball to the leftest side 
         ball['x'] = 0
    # to do 
    # question 1 
    # how to move left
    
    # question 2 
    # how to move up or down 
    
    
    # question 3 
    # how to move up and left (NW) at the same time  
    
    # question 4
    # how to move up and right (NE) at the same time  
    
def draw():
    # bg color  255     
    background(color_bg)
    move(a_ball)
    rect(a_ball['x'],a_ball['y'] , 10,10)
    

初始化

w,h = 600,400
a_ball = {'x':w//2,'y':h//2 } 
color_bg = 255

窗口大小, 小球坐标 , 背景色

移动小球

def move(ball):
    # go right ,move 1 pixel
    ball['x'] +=1
    # if hit the wall (horizontal border at the right)
    if ball['x'] > w:
         # move the ball to the leftest side 
         ball['x'] = 0

小球每次右边移动 一个pixel ,当触碰到右边墙壁时, 把它移动到最左边,看上去是 穿越的效果

动画原理

每一帧会执行 move,所以 小球会一直在移动, 看起来就变成动画了

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

推荐阅读更多精彩内容