Author Topic: Air Hockey - a NaaLaa coffee time game  (Read 5312 times)

kevin

  • Guest
Air Hockey - a NaaLaa coffee time game
« on: October 14, 2013, 07:00:13 PM »
Hi - here's a version of a very old game I used to play on an old Palm PDA in the 1990's.....I'm not sure how the phrase "coffee time game" translates, but I'm using it here to excuse a game that really seemed more fun back then! I don't see that this will keep anyone's attention for more than a few minutes to be honest, but I had fun writing it......
« Last Edit: October 14, 2013, 07:12:08 PM by kevin »

cvirus

  • Guest
Re: Air Hockey - a NaaLaa coffee time game
« Reply #1 on: October 14, 2013, 08:18:51 PM »
It looks cool, i do play some times in real machines this time of game....it's cool. :-)

Mopz

  • Guest
Re: Air Hockey - a NaaLaa coffee time game
« Reply #2 on: October 17, 2013, 09:12:37 AM »
Sorry for not being on the forum lately. I'll check this one out as soon as work is over :)

Mopz

  • Guest
Re: Air Hockey - a NaaLaa coffee time game
« Reply #3 on: October 18, 2013, 04:32:47 AM »
This implementation is awesome :) Love that you're using image transformations for the 3d stuff. Been playing it for a while now. Do you mind if I add it to the games section of naalaa.com? With your permission I'd also like to rewrite some of the drawing using the raster commands so that it'd work as an applet.

Good work!
« Last Edit: October 18, 2013, 04:41:30 AM by Mopz »

Mopz

  • Guest
Re: Air Hockey - a NaaLaa coffee time game
« Reply #4 on: October 18, 2013, 05:03:05 AM »
Here's a modified version that'd run as an applet.

Code: [Select]
rem ---------import required libraries----------
rem --------------------------------------------
 import "Speed.lib"
 import "Keycodes.lib"
rem -----------------------------------------------------------
 constant:

WIDTH 640
HEIGHT 480

PUCK_IMAGE 0
BAT_IMAGE 1
BAT2_IMAGE 2
BAT_CLEAR_IMAGE 3
PUCK_CLEAR_IMAGE 4

rem ===========================================================
rem -----------------------------------------------------------
 hidden:

set caret 320,140
center "AIR HOCKEY"
center ""
center "Loading - please wait ...."


 set window 16, 16, WIDTH,HEIGHT
 set redraw off
create font 0,"Arial Black",24,true,false,false,true
save font 0,"Arial Black24.txt","Arial Black24.bmp"

create font 1,"Arial Black",48,true,false,false,true
save font 1,"Arial Black48.txt","Arial Black48.bmp"

create font 2,"Arial Black",12,true,false,false,true
save font 2,"Arial Black12.txt","Arial Black12.bmp"





load image PUCK_IMAGE, "data/puck.bmp"
set image colorkey PUCK_IMAGE,0,0,0

load image PUCK_CLEAR_IMAGE, "data/puck_clear.bmp"
set image colorkey PUCK_CLEAR_IMAGE,0,0,0


load image BAT_IMAGE, "data/bat1.bmp"
set image colorkey BAT_IMAGE,0,0,0

load image BAT2_IMAGE, "data/bat2.bmp"
set image colorkey BAT2_IMAGE,0,0,0

load image BAT_CLEAR_IMAGE, "data/bat_clear.bmp"
set image colorkey BAT_CLEAR_IMAGE,0,0,0


rem ===========================================================
rem -----------------------------------------------------------
visible:
rem initialise variables


rem ------------   puck   -----------------
puck_x# = 580.0
puck_y# = 380.0

puck_dy# = 4.0
puck_dx# = 0.1
puck_can_move = true
rem to INCREASE puck speed use a LOWER number 5 fast... 10 slow
puck_speed# = 7.5


rem ------------   bat   -------------------

bat_x# = 320.0
bat_y# = 360.0
bat_dx#
bat_dy#
bat1_timer = 0

rem ------------   bat2 - the enemy  -------------------

bat2_x# = 320.0
bat2_y# = 120.0
bat2_dx# = 0.4
bat2_dy# = 0.4
bat2_can_hit = true
bat2_timer = 0
bat2_timer2 = 0
bat2_speed# = 0.05
rem - a higher number for bat_2_difficulty INCREASES DIFFICULTY .... 1 IS EASY, ABOVE 2 DIFFICULT

bat_2_difficulty#

rem ===========================================================

rem  ------------   GOALS  ------------------

goal_x_p1 = 250
goal_width_p1 = (WIDTH/2 - goal_x_p1)  * 2


goal_x_p2 = 267
goal_width_p2 = 105
rem =========================================================

score_p1
score_p2


 shouldDraw = true
proc MenuScreen

rem ====================================================================================================
rem =====================================   start of game loop   =======================================
rem ====================================================================================================





 do

if keydown("p",true) then wait 5000

rem  -----------------   some timers  ------------------------------

rem block_timer=(block_timer+1)%5

if puck_y < 240.0
bat2_timer = bat2_timer + 1
else
bat2_timer = 0
endif

if puck_y < 75.0
bat2_timer2 = bat2_timer2 + 1
else
bat2_timer2 = 0
endif

if puck_y > 405.0

bat1_timer = bat1_timer + 1
else
bat1_timer = 0
endif


rem ================================================================

rem ----------------  call procedures  ------------------------------

proc move_puck
proc controls
proc move_bat2
proc bat1_collision
proc bat2_collision
proc check_for_p1_goal
proc check_for_p2_goal

rem =================================================================

rem ---------------  game logic   ----------------------------------


rem ================================================================

rem ---------------  drawing routines  -----------------------------

if shouldDraw
set font 0
set color 32,32,96
 cls
 set color 255,255,255




rem ----------  hockey table  ----------------------

draw line 0,HEIGHT - 60,WIDTH,HEIGHT - 60

draw line 134,38,0,HEIGHT - 60
draw line WIDTH - 134,38,WIDTH,HEIGHT - 60


draw line 150,60,40,HEIGHT - 90
draw line WIDTH  - 150,60,WIDTH - 40,HEIGHT - 90

draw rect 0,HEIGHT - 60,WIDTH,30

draw rect 150,60,118, 22
draw rect 373,60,118, 22


draw line 150,82,50,390
draw line WIDTH  - 150,82,590,390

draw line 134,38,506,38

draw line 268,60,272,38
draw line 272,60,272,38
draw line 272,60,268,80

draw line 373,60,368,38
draw line 368,60,368,38
draw line 368,60,373,80

draw line 368,60,272,60


draw line 10,390,250,390
draw line 390,390,630,390



rem ----  goals  ----------

draw line 250,390,245,420
draw line 390,390,395,420

draw line 395,420,395,450
draw line 245,420,245,450

draw line 250,390,250,420
draw line 390,390,390,420


set color 32,96,255
set caret 320,455
center "PLAYER 1"
set color 255,255,255

set color 0,255,0

set caret 320,10
center "PLAYER 2"
set color 255,255,255


rem mopz mod.
rem clear transformation
rem translate bat_x, bat_y
rem scale 1.0 - (abs#(bat_y - 480.0)/1000.0), 1.0 - (abs#(bat_y - 480.0)/1000.0)
s# = 1.0 - (abs#(bat_y - 480.0)/1000.0)

if bat_y < 390.0
rem draw image BAT_IMAGE
proc DrawImageScaled BAT_IMAGE, int(bat_x), int(bat_y), s
elseif bat_x > 250.0 and bat_x < 390.0
rem draw image BAT_IMAGE
proc DrawImageScaled BAT_IMAGE, int(bat_x), int(bat_y), s
else
rem draw image BAT_CLEAR_IMAGE
proc DrawImageScaled BAT_CLEAR_IMAGE, int(bat_x), int(bat_y), s
endif

rem mopz mod.
rem clear transformation
rem translate bat2_x, bat2_y
rem scale 1.0 - (abs#(bat2_y - 480.0)/1000.0), 1.0 - (abs#(bat2_y - 480.0)/1000.0)
rem draw image BAT2_IMAGE
s# = 1.0 - (abs#(bat2_y - 480.0)/1000.0)
proc DrawImageScaled BAT2_IMAGE, int(bat2_x), int(bat2_y), s


rem -----------------   puck   ----------------------

rem mopz mod.
rem clear transformation
rem translate puck_x, puck_y
rem scale 1.0 - (abs#(puck_y - 480.0)/1000.0), 1.0 - (abs#(puck_y - 480.0)/1000.0)
s# = 1.0 - (abs#(puck_y - 480.0)/1000.0)

if puck_y < 390.0
rem draw image PUCK_IMAGE
proc DrawImageScaled PUCK_IMAGE, int(puck_x), int(puck_y), s
elseif puck_x > 250.0 and puck_x < 390.0
rem draw image PUCK_IMAGE
proc DrawImageScaled PUCK_IMAGE, int(puck_x), int(puck_y), s
else
rem draw image PUCK_CLEAR_IMAGE
proc DrawImageScaled PUCK_CLEAR_IMAGE, int(puck_x), int(puck_y), s
endif


set caret 60,10
center "PLAYER 1"
center score_p1

set caret 580,10
center "PLAYER 2"
center score_p2

redraw

endif
rem =================================================================
shouldDraw = SPD_HoldFrameDraw(60)
 until keydown(27) or not running()

rem procedures  ------------------------------    procedures   ------
 
rem ================================================================================
procedure move_puck()
rem slow it down
puck_dx = puck_dx * 0.998
puck_dy = puck_dy * 0.998

proc check_collision

if puck_can_move = true
puck_y = puck_y + puck_dy
puck_x = puck_x + puck_dx
endif

rem ---- this is for testing - sometimes the puck gets caught / stuck ----
rem ----  at the top of the screen  -  pressing space will shift it   ----
if keydown(VK_SPACE,true)
if puck_y < 240.0
puck_y = puck_y + 10.0
else
puck_y = puck_y - 10.0
endif
endif
endproc

rem ================================================================================
procedure check_collision()
puck_can_move = true


rem ---
if puck_y >= 405.0
if puck_x - 30.0 <= float(goal_x_p1) or puck_x + 30.0 >= float(goal_x_p1 + goal_width_p1)
puck_can_move = false
puck_dy = - puck_dy
puck_y = puck_y + puck_dy
endif
endif




if puck_y <= 75.0
if puck_x - 22.0 <= float(goal_x_p2) or puck_x + 22.0 >= float(goal_x_p2 + goal_width_p2)
puck_can_move = false
puck_dy = - puck_dy
puck_y = puck_y + puck_dy
endif
endif




if puck_x + 30.0 >= float(WIDTH) - ((420.0 - puck_y)/ 3.0) - 30.0;rem and puck_dx > 0.0
puck_can_move = false

puck_dx = - puck_dx  - 1.4
puck_x = puck_x + (puck_dx)
endif

if puck_x - 30.0 <= ((420.0 - puck_y)/ 3.0) + 30.0
puck_can_move = false

puck_dx = - puck_dx  + 1.4
puck_x = puck_x + (puck_dx * 2.0)

endif

rem - limit the puck_dx speed
if puck_dx > 7.0 then puck_dx = 7.0
if puck_dx < - 7.0 then puck_dx = - 7.0


endproc
rem =================================================================================
  procedure controls()

rem Slow down.
bat_dx = bat_dx*0.95
bat_dy = bat_dy*0.95
rem Speed up.
if keydown(VK_LEFT) then bat_dx = max#(bat_dx - 0.5, -4.0)
if keydown(VK_RIGHT) then bat_dx = min#(bat_dx + 0.5, 4.0)
if keydown(VK_UP) then bat_dy = max#(bat_dy - 0.5, -4.0)
if keydown(VK_DOWN) then bat_dy = min#(bat_dy + 0.5, 4.0)
rem Move.
bat_x = bat_x + bat_dx
bat_y = bat_y + bat_dy
rem Stay on screen.
bat_x = min#(max#(bat_x, ((420.0 - bat_y)/ 3.0) + 54.0), float(WIDTH) - ((420.0 - bat_y)/ 3.0) - 54.0 )
bat_y = min#(max#(bat_y, 240.0), float(height(primary) - height(BAT_IMAGE)))
if bat_y > 410.0 then bat_y = 410.0


if bat1_timer > 160 ;rem and puck_y <= 61.0
set caret 320,240
center "PRESS SPACEBAR"
redraw
puck_y = puck_y - 10.0
endif




endproc
rem =================================================================================
procedure bat1_collision()
rem --- can't use width(PUCK_IMAGE) in the formulas
rem ----  as it changes --------------------------
rem puck_x and bat_x are the MIDDLE of the image
if puck_x + ((48.0 / 480.0 * puck_y) / 2.0) > bat_x - ((60.0 / 480.0 * bat_y) / 2.0)
if puck_x - ((48.0 / 480.0 * puck_y)  / 2.0) < bat_x + ((60.0 / 480.0 * bat_y) / 2.0)
 
if puck_y + ((24.0 / 480.0 * puck_y) / 2.0) > bat_y - ((30.0 / 480.0 * bat_y) / 2.0)

if puck_y - ((24.0 / 480.0 * puck_y) / 2.0) < bat_y + ((30.0 / 480.0 * bat_y) / 2.0)

puck_dx = (puck_x - bat_x) / puck_speed
puck_dy = (puck_y - bat_y) / puck_speed

puck_y = puck_y + (bat_dy * 1.15)
puck_x = puck_x + (bat_dx * 1.05)

endif
endif
endif
endif
endproc
rem =================================================================================
procedure bat2_collision()
rem --- can't use width(PUCK_IMAGE) in the formulas
rem ----  as it changes --------------------------
rem puck_x and bat_x are the MIDDLE of the image

if puck_x + ((48.0 / 480.0 * puck_y) / 2.0) > bat2_x - ((60.0 / 480.0 * bat2_y) / 2.0)
if puck_x - ((48.0 / 480.0 * puck_y)  / 2.0) < bat2_x + ((60.0 / 480.0 * bat2_y) / 2.0)
 
if puck_y + ((24.0 / 480.0 * puck_y) / 2.0) > bat2_y - ((30.0 / 480.0 * bat2_y) / 2.0)

if puck_y - ((24.0 / 480.0 * puck_y) / 2.0) < bat2_y + ((30.0 / 480.0 * bat2_y) / 2.0)
puck_dx = (puck_x - bat2_x) / 6.0
puck_dy = (puck_y - bat2_y) / 6.0

bat2_can_hit = false
puck_y = puck_y + (bat2_dy * 1.15)
puck_x = puck_x + (bat2_dx * 1.05)

endif
endif
endif
endif
endproc
rem =====================================================================================
procedure move_bat2()

if puck_dy < 0.0 then bat2_can_hit = true
if bat2_timer > 250  then bat2_can_hit = true
rem Slow down.
bat2_dx = bat2_dx*0.95
bat2_dy = bat2_dy*0.95

rem Move.
bat2_x = bat2_x + bat2_dx
bat2_y = bat2_y + bat2_dy
if bat2_can_hit = true
if puck_x < bat2_x
bat2_dx = max#(bat2_dx - bat2_speed, - 6.0)
else
bat2_dx = min#(bat2_dx + bat2_speed, 6.0)
endif

if puck_y > bat2_y
rem the +0.05 below dictates the forward speed of bat2 - a larger number increases speed but makes it more choppy
bat2_dy = min#(bat2_dy + 0.05, 6.0)
bat2_y = bat2_y - 0.2
else
rem this line dictates the speed that the bat2 can move backwards.....
bat2_dy = - bat_2_difficulty
endif
endif

if bat2_y < 65.0 then bat2_y = 65.0
if bat2_y > 240.0 then bat2_y = 240.0

rem this stops the puck getting trapped at the top

if bat2_timer2 > 160 ;rem and puck_y <= 61.0
set caret 320,240
center "PRESS SPACEBAR"
redraw
puck_y = puck_y + 10.0
endif

endproc
rem ==================================================================================
rem ==================================================================================
procedure check_for_p1_goal()

if puck_y <= 75.0
if puck_x - 22.0 > float(goal_x_p2) and puck_x + 22.0 < float(goal_x_p2 + goal_width_p2)
set caret 320,90
set font 1
score_p1 = score_p1 + 1
center "GOAL!"
set caret 320,220
center score_p1," - ",score_p2

redraw
wait 3000

if score_p1 > 4
proc EndGame
endif


puck_x = 320.0
puck_y = 240.0
puck_dy = 0.5
bat_x = 320.0
bat_y = 360.0
bat2_x = 320.0
bat2_y = 120.0
endif
endif




endproc
rem ==================================================================================

procedure check_for_p2_goal()

if puck_y >= 405.0
if puck_x - 30.0 >= float(goal_x_p1) and puck_x + 30.0 <= float(goal_x_p1 + goal_width_p1)
rem _y = puck_y + 64.0
set caret 320,350
set font 1
score_p2 = score_p2 + 1
center "GOAL!"
set caret 320,220
center score_p1," - ",score_p2
redraw
wait 3000

if score_p2 > 4
proc EndGame
endif


puck_x = 320.0
puck_y = 240.0

puck_dy = - 0.5


bat_x = 320.0
bat_y = 390.0
bat2_x = 320.0
bat2_y = 90.0
endif
endif




endproc

rem ======================================================================================
procedure EndGame()

rem clear previous message
set color 0,0,0
cls
set color 255,255,255


set caret 320,220
center "Game Over"
redraw
wait 3000

proc MenuScreen
endproc
rem ==================================================================================
rem ==================================================================================
rem ==================================================================================
rem ==================================================================================
rem =====================         MENU SCREEN          ===============================
rem ==================================================================================
rem ==================================================================================
rem ==================================================================================
rem ==================================================================================






procedure MenuScreen()
set font 0

rem ---- reset all variables so that the option to "Start Game" will work ....
rem ---- ....if not, that option will just trigger the EndGame menu again ..

score_p1 = 0
score_p2 = 0
puck_x = 320.0
puck_y = 240.0
do
menuItem = 0
do
if keydown(VK_DOWN,true) and menuItem < 5 then menuItem = menuItem + 1
if keydown(VK_UP,true) and menuItem > 0 then menuItem = menuItem - 1
set color 0,0,0
cls
set color 255,255,255
set caret 320, 170
set color 0,255,255
center "Use Keys To Move in The Menu"
center "Press Space to Select an Item"
set caret 320, 240
rem ---------
rem -----------------------  start of menu display   ---------------------------------
rem ---------

rem if menuItem = 0; set color 0,255,255; else; set color 0,96,128;
rem endif
rem center "START THE FULL GAME"
rem ---------

if menuItem = 1; set color 0,255,255; else; set color 0,96,128;
endif
center "HOW TO PLAY"
rem ---------

if menuItem = 2; set color 0,255,255; else; set color 0,96,128;
endif
center "PLAY A HARD GAME"


rem --------------
if menuItem = 3; set color 0,255,255; else; set color 0,96,128;
endif
center "PLAY AN EASY GAME"

rem ---------
if menuItem = 4; set color 0,255,255; else; set color 0,96,128;
endif
center"QUIT"


rem ---------
if menuItem = 0; set color 0,255,255; else; set color 0,96,128;
endif
redraw
wait 10
until keydown(VK_SPACE,true)

if menuItem = 1
set color 0,0,0
cls
set color 255,0,255
set caret 320,50
center "++++++++++++++++++++++++++++++++++++"
center " HOW TO PLAY AIR HOCKEY "
center "++++++++++++++++++++++++++++++++++++"
center
set color 0,255,255
center "You control the BLUE bat using"
center
center "the arrow keys. Your aim is to"
center
center "hit the RED puck into the opponents"
center ""
center "goal and defend your own goal."
center""

center "First to 5 wins ...."
center ""
set color 255,255,255
center "Press any key to continue..."
redraw
wait keydown

rem ------------------------
rem elseif menuItem = 0
rem donotreturn = true
elseif menuItem = 2
bat_2_difficulty# = 1.8
puck_speed# = 6.0

donotreturn = true
elseif menuItem = 3
bat_2_difficulty# = 1.2
puck_speed# = 9.0

donotreturn = true
rem ------------
elseif menuItem = 4
end

endif

until menuItem = 2 or menuItem = 3; rem or menuItem = 3

endproc

rem ==================================================================
rem for java applets, mopz mod.
rem ==================================================================

procedure DrawImageScaled(img, x, y, sc#)
set color 255, 255, 255, 0
w = int(float(width(img))*sc)
h = int(float(height(img))*sc)
x = x - w/2
y = y - h/2
vstep# = 1.0/float(h)
v# = 0.0
for iy = 0 to h - 1
draw hraster img, y + iy, x, x + w, 0.0, v, 1.0, v
v = v + vstep
next
set color 255, 255, 255
endproc

kevin

  • Guest
Re: Air Hockey - a NaaLaa coffee time game
« Reply #5 on: October 18, 2013, 07:36:23 AM »
Hi....I'd be happy for you to add it to the games section Mopz......thank you for the kind words. I'm constantly impressed with how quickly/smoothly games run using NaaLaa, and how relatively simple routines like image transformations are to use by amateurs like me.

Mopz

  • Guest
Re: Air Hockey - a NaaLaa coffee time game
« Reply #6 on: November 01, 2013, 05:27:13 PM »
Sorry, once again, for being late to respond.

Oracle are about to change their rules for java applets quite a lot (because of all those security issues). Soon users won't even be ABLE to run applets that hasn't been trustfully signed. So there goes naalaa to the bucket, as I assume most of us can't afford a certificate :(

Couldn't they make, like, a free indie certificate thingie that doesn't allow the applet to do anything more advanced than load files, read the keyboard and put some graphics in a window?

I'll add the game to the games download section however.