03-13-2025, 03:45 AM
click the images to zoom in
Code:
'====================================
' Primitive Angry Bird
' It's just a demo of simple physics
'
'
'====================================
' Screen dimensions
set window "Primitive Angry Bird", 1000, 600,false
set redraw off
'color definition
black = [0,0,0]
white = [255,255,255]
green = [0,100,0]
lightgreen = [0,255,0]
brown = [255,100,10]
gray = [100,100,100]
red = [255,0,0]
' Gravity, Friction, Ground
gravity = 0.1
friction = 0.99 ' Friction to slow down movement
groundLevel = 350
' Bird,Pig,Boxes
visible bird = []
visible pig = []
visible boxes = []
Initialize(groundLevel)
' Mouse
startX = 0
startY = 0
'----------------
' Main game loop
'----------------
while not keydown(KEY_ESCAPE,true)
' clear screen
set color black
cls
' info box
set color white
set caret 10,420
wln "--------------------"
wln "Primitive Angry Bird"
wln "--------------------"
wln
wln "Drag the red cirle by pressing your left mouse button"
wln "Then hit the green circle !"
wln
wln "Control Keys :"
wln "- SPACE BAR = restart"
wln "- ESCAPE = quit"
' Draw ground
set color green
draw rect 0, groundLevel, 1000, 50,true
set color white
draw rect 0, groundLevel, 1000, 50
' Draw bird
set color red
draw ellipse bird.x, bird.y, bird.size,bird.size,true
set color white
draw ellipse bird.x, bird.y, bird.size,bird.size
' Draw pig
set color lightgreen
draw ellipse pig.x, pig.y, pig.size, pig.size,true
set color white
draw ellipse pig.x, pig.y, pig.size, pig.size
' Draw boxes
for i = 0 to 2
set color brown
draw rect boxes[i].x, boxes[i].y, boxes.size, boxes.size, true
set color white
draw rect boxes[i].x, boxes[i].y, boxes.size, boxes.size
next
' Handle mouse input
if not mousebutton(0) then
' Start dragging
startX = mousex()
startY = mousey()
else
' Show trajectory line while dragging
set color gray
draw line bird.x, bird.y, mousex(), mousey()
' Release bird and calculate velocity
bird.vx = (startX - mousex()) / 10
bird.vy = (startY - mousey()) / 10
endif
'Handle keyboard input to restart the game
if keydown(KEY_SPACE,true) then Initialize(groundLevel)
' Update bird position
' Apply gravity
bird.vy = bird.vy + gravity
' Update position
bird.x = bird.x + bird.vx
bird.y = bird.y + bird.vy
' Apply friction
bird.vx = bird.vx * friction
bird.vy = bird.vy * friction
' Check for ground collision
if bird.y >= groundLevel - bird.size then
bird.y = groundLevel - bird.size
bird.vy = -bird.vy * 0.7 ' Bounce with reduced energy
endif
' Check for pig collision
distance = ((bird.x - pig.x)^2 + (bird.y - pig.y)^2)^0.5
if distance <= bird.size + pig.size then
pig.vx = bird.vx * 0.5 ' Transfer some velocity to the pig
pig.vy = bird.vy * 0.5
pig.hit = true
' Update position
bird.x = width()+500
endif
' Check for boxes and bird collisions
for i = 0 to 2
if bird.x + bird.size > boxes[i].x and bird.x - bird.size < boxes[i].x + boxes.size and
bird.y + bird.size > boxes[i].y and bird.y - bird.size < boxes[i].y + boxes.size then
' Resolve overlap by moving bird away from box
dx = bird.x - (boxes[i].x + boxes.size / 2)
dy = bird.y - (boxes[i].y + boxes.size / 2)
distance = (dx^2 + dy^2)^0.5
if distance < bird.size + boxes.size / 2 then
' Move bird away from box
overlap = (bird.size + boxes.size / 2) - distance
bird.x = bird.x + (dx / distance) * overlap
bird.y = bird.y + (dy / distance) * overlap
' Transfer velocity between bird and box
tempVx = bird.vx
tempVy = bird.vy
bird.vx = boxes[i].vx * 0.5
bird.vy = boxes[i].vy * 0.5
boxes[i].vx = tempVx * 0.5
boxes[i].vy = tempVy * 0.5
endif
boxes.hit = true
endif
next
if boxes.hit or pig.hit then
' Update pig position
pig.y = pig.y + pig.vy
pig.x = pig.x + pig.vx
' Apply gravity to pig
pig.vy = pig.vy + gravity
endif
' Check for ground collision for pig
if pig.y >= groundLevel - pig.size then
pig.y = groundLevel - pig.size
pig.vy = -pig.vy * 0.5 ' Bounce with reduced energy
pig.vx = -pig.vx * 0.5
endif
' Update box positions
if boxes.hit then
for i = 0 to 2
boxes[i].y = boxes[i].y + boxes[i].vy
boxes[i].x = boxes[i].x + boxes[i].vx
' Apply gravity to boxes
boxes[i].vy = boxes[i].vy + gravity
' Check for ground collision for boxes
if boxes[i].y >= groundLevel - boxes.size then
boxes[i].y = groundLevel - boxes.size
boxes[i].vy = -boxes[i].vy * 0.01 ' Bounce with reduced energy
boxes[i].vx = -boxes[i].vx * 0.01
endif
next
endif
'Check for collisions between boxes
for i = 0 to 2
for j = 0 to 2
' Check if boxes overlap
if boxes[i].x < boxes[j].x + boxes.size and boxes[i].x + boxes.size > boxes[j].x and
boxes[i].y < boxes[j].y + boxes.size and boxes[i].y + boxes.size > boxes[j].y then
' Resolve overlap by moving boxes apart
dx = boxes[j].x - boxes[i].x
dy = boxes[j].y - boxes[i].y
overlapX = boxes.size - abs(dx)
overlapY = boxes.size - abs(dy)
if overlapX < overlapY then
' Resolve horizontally
if dx < 0 then
boxes[i].x = boxes[i].x - overlapX+rnd(2,6)
boxes[j].x = boxes[j].x + overlapX+rnd(2,6)
else
boxes[i].x = boxes[i].x + overlapX+rnd(2,6)
boxes[j].x = boxes[j].x - overlapX+rnd(2,6)
endif
else
' Resolve vertically
if dy < 0 then
boxes[i].y = boxes[i].y - overlapY
boxes[j].y = boxes[j].y + overlapY
else
boxes[i].y = boxes[i].y + overlapY
boxes[j].y = boxes[j].y - overlapY
endif
endif
endif
next
next
'Check for collisions between pig and boxes
for i = 0 to 2
'Check if pig overlaps with box
if pig.x + pig.size > boxes[i].x and pig.x - pig.size < boxes[i].x + boxes.size and
pig.y + pig.size > boxes[i].y and pig.y - pig.size < boxes[i].y + boxes.size then
' Resolve overlap
dx = (pig.x - (boxes[i].x + boxes.size / 2))
dy = (pig.y - (boxes[i].y + boxes.size / 2))
distance = (dx^2 + dy^2)^0.5
if distance < pig.size + boxes.size / 2 then
' Move pig away from box
overlap = (pig.size + boxes.size / 2) - distance
pig.x = pig.x + (dx / distance) * overlap
pig.y = pig.y + (dy / distance) * overlap
' Transfer velocity between pig and box
tempVx = pig.vx
tempVy = pig.vy
pig.vy = boxes[i].vx * 0.5
pig.vy = boxes[i].vy * 0.5
boxes[i].vx = tempVx * 0.5
boxes[i].vy = tempVy * 0.5
endif
endif
next
' Reset bird if it goes off-screen
if bird.x < 0 or bird.x > width() or bird.y > height() or bird.y < 0 then
bird.x = 100
bird.y = groundLevel - bird.size
bird.vx = 0
bird.vy = 0
endif
' Refresh screen
fwait 60
redraw
wend
'----------
' FUNCTION
'----------
function Initialize(groundLevel)
' Bird properties
bird.size = 10
bird.x = 100
bird.y = groundLevel - bird.size
bird.vx = 0 ' Horizontal velocity
bird.vy = 0 ' Vertical velocity
' Pig properties
pig.size = 15
pig.x = 700 ' Pig's x position
pig.y = groundLevel - pig.size - 3 * 30 ' Pig's y position (on top of boxes)
pig.vx = 0 ' Pig's horizontal velocity
pig.vy = 0 ' Pig's vertical velocity
pig.hit = false
' Box properties (stack of 3 boxes)
boxes.size = 30 ' Size of each box (width and height)
for i = 0 to 2
boxes[i] = []
boxes[i].x = pig.x-pig.size ' Staggered x positions
boxes[i].y = groundLevel - (i + 1) * boxes.size ' Stacked y positions
boxes[i].vx = 0 ' Horizontal velocity
boxes[i].vy = 0 ' Vertical velocity
next
boxes.hit = false
endfunc