Bouncing balls animation

Microsoft Windows
Post Reply
User avatar
Manna5
Posts: 9
Joined: 2021-Aug-03, 7:54 am

Bouncing balls animation

Post by Manna5 »

I want to show you my CMD script that animates four balls bouncing by window borders using BG sprites. Example screenshot:
Image
You can hide the metadata or show it again by pressing any key, Esc terminates the script. Code:

Code: Select all

@echo off
title Animated balls using sprites 
set ball_1_x=34
set ball_1_y=17
set ball_1_dx=-1
set ball_1_dy=-1
set ball_2_x=43
set ball_2_y=24
set ball_2_dx=-1
set ball_2_dy=1
set ball_3_x=48
set ball_3_y=4
set ball_3_dx=1
set ball_3_dy=-1
set ball_4_x=76
set ball_4_y=18
set ball_4_dx=-1
set ball_4_dy=-1
set metadata=1
if not exist bg.exe goto :no_bg
bg cursor 0
color F0
:draw_balls
cls
bg _kbd
set key=%errorlevel%
if %key% gtr 0 set /a metadata=1-metadata
if %key% equ 27 exit
if %metadata% equ 0 goto :skip_metadata
echo X: %ball_1_x% Y: %ball_1_y% DX: %ball_1_dx% DY: %ball_1_dy%
echo X: %ball_2_x% Y: %ball_2_y% DX: %ball_2_dx% DY: %ball_2_dy%
echo X: %ball_3_x% Y: %ball_3_y% DX: %ball_3_dx% DY: %ball_3_dy%
echo X: %ball_4_x% Y: %ball_4_y% DX: %ball_4_dx% DY: %ball_4_dy%
:skip_metadata
bg sprite %ball_1_y% %ball_1_x% A2 1
bg sprite %ball_2_y% %ball_2_x% B3 2
bg sprite %ball_3_y% %ball_3_x% C4 3
bg sprite %ball_4_y% %ball_4_x% D5 4
bg sleep 150
if %ball_1_x% gtr 78 set ball_1_dx=-1
if %ball_1_x% lss 1 set ball_1_dx=1
if %ball_1_y% gtr 23 set ball_1_dy=-1
if %ball_1_y% lss 1 set ball_1_dy=1
set /a ball_1_x=ball_1_x+ball_1_dx
set /a ball_1_y=ball_1_y+ball_1_dy
if %ball_2_x% gtr 78 set ball_2_dx=-1
if %ball_2_x% lss 1 set ball_2_dx=1
if %ball_2_y% gtr 23 set ball_2_dy=-1
if %ball_2_y% lss 1 set ball_2_dy=1
set /a ball_2_x=ball_2_x+ball_2_dx
set /a ball_2_y=ball_2_y+ball_2_dy
if %ball_3_x% gtr 78 set ball_3_dx=-1
if %ball_3_x% lss 1 set ball_3_dx=1
if %ball_3_y% gtr 23 set ball_3_dy=-1
if %ball_3_y% lss 1 set ball_3_dy=1
set /a ball_3_x=ball_3_x+ball_3_dx
set /a ball_3_y=ball_3_y+ball_3_dy
if %ball_4_x% gtr 78 set ball_4_dx=-1
if %ball_4_x% lss 1 set ball_4_dx=1
if %ball_4_y% gtr 23 set ball_4_dy=-1
if %ball_4_y% lss 1 set ball_4_dy=1
set /a ball_4_x=ball_4_x+ball_4_dx
set /a ball_4_y=ball_4_y+ball_4_dy
goto :draw_balls
:no_bg
cls
color 0c
echo BG is required!
pause >nul
User avatar
Simon Sheppard
Posts: 49
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: Bouncing balls animation

Post by Simon Sheppard »

The post for BG.exe can be found here:
oldforum/viewtopic.php?id=1624
Post Reply