pong

General FreeBASIC programming questions.
bluatigro
Posts: 434
Joined: Apr 25, 2012 10:35
Location: netherlands

pong

Postby bluatigro » Apr 21, 2017 11:36

i m trying to build a set of lessons for the HCC programming groop
[ https://programmeren.hcc.nl/ ]
i hit a snag whit pong
i m tying to use sprites

error :
the bal does not bonce Always when it needs to

Code: Select all

''les 14 pong

#include "_sprite.bas"
#include "_color.bas"

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB
#endif

dim shared as integer winx , winy , winbits , mousex , mousey
screeninfo winx , winy , winbits
SCREENRES winx, winy, 32, 2, FB.GFX_FULLSCREEN


dim as sprite top , bottum , bal , human , comp

top.create winx , 30
top.x = winx / 2
top.y = 30
paint top.img , ( 1 , 1 ) , green
bottum.create winx , 30
bottum.x = winx / 2
bottum.y = winy - 30
paint bottum.img , ( 1 , 1 ) , green
bal.create 30 , 30
bal.x = winx / 2
bal.y = winy / 2
paint bal.img , ( 1 , 1 ) , green
human.create 30 , 100
human.x = winx - 30
human.y = winy / 2
paint human.img , ( 1 , 1 ) , green
comp.create 30 , 100
comp.x = 30
comp.y = winy / 2
paint comp.img , ( 1 , 1 ) , green

dim as integer baldx = 5 , baldy = 5

do
  screenlock
  cls
  top.draw_it 1
  bottum.draw_it 1
  bal.draw_it 1
  human.draw_it 1
  comp.draw_it 1
  screenunlock
 
  if not getmouse( mousex , mousey ) then
    if mousey <> -1 then
      human.y = mousey
    end if
  end if
 
  if comp.y < bal.y then comp.move 0 , 5
  if comp.y > bal.y then comp.move 0 , -5
 
  if bal.hit( top ) then baldy = abs( baldy )
  if bal.hit( bottum ) then baldy = -abs( baldy )
  if bal.hit( human ) then
    baldx = -abs( baldx )
    baldy = ( human.y - bal.y ) / 10
  end if
  if bal.hit( comp ) then
    baldx = abs( baldx )
    baldy = ( comp.y - bal.y ) / 10
  end if
 
  if bal.x < 0 then
    bal.x = winx / 2
    bal.y = winy / 2
  end if
  if bal.x > winx then
    bal.x = winx / 2
    bal.y = winy / 2
  end if
 
  bal.move baldx , baldy
 
  sleep 40
  flip
loop until inkey = chr( 27 )

Code: Select all

''_sprite.bas

type sprite
public :
  dim as integer x , y , w , h
  dim as any ptr img = 0
  declare sub create( a as integer , b as integer )
  declare sub move( a as integer , b as integer )
  declare function hit( spr as sprite ) as integer
  declare sub draw_it( c as integer )
end type

sub sprite.create( a as integer , b as integer )
  w = a
  h = b
  img = imagecreate( w , h , rgba(0,0,0,0) , 32 )
end sub

sub sprite.move( a as integer , b as integer )
  x += a
  y += b
end sub

function sprite.hit( spr as sprite ) as integer
  dim as integer uit = 1
  if x + w <  spr.x then uit = 0
  if y + h <  spr.y then uit = 0
  if x > spr.x + spr.w then uit = 0
  if y > spr.y + spr.h then uit = 0
  return uit
end function

sub sprite.draw_it( c as integer )
  if c then
    put ( x - w / 2 , y - h / 2 ) , img , trans
  else
    put ( x , y ) , img , trans
  end if
end sub

Code: Select all

''_color.bas

#ifndef color_h
#define color_h

#include "_math.bas"

''primary colors
const as ulong black = rgb( 0 , 0 , 0 )
const as ulong red = rgb( 255 , 0 , 0 )
const as ulong green = rgb( 0 , 255 , 0 )
const as ulong yellow = rgb( 255 , 255 , 0 )
const as ulong blue = rgb( 0 , 0 , 255 )
const as ulong magenta = rgb( 255 , 0 , 255 )
const as ulong cyan = rgb( 0 , 255 , 255 )
const as ulong white = rgb( 255 , 255 , 255 )
''mixed colors
const as ulong gray = rgb( 127 , 127 , 127 )
const as ulong pink = rgb( 255 , 127 , 127 )
const as ulong orage = rgb( 255 , 127 , 0 )
const as ulong purple = rgb( 127 , 0 , 127 )

function mix( kla as ulong , f as double , klb as ulong ) as ulong
  dim as integer r1 , g1 , b1 , r2 , g2 , b2 , r , g , b
  r1 = ( kla shr 16 ) and 255
  g1 = ( kla shr 8 ) and 255
  b1 = kla and 255
  r2 = ( klb shr 16 ) and 255
  g2 = ( klb shr 8 ) and 255
  b2 = klb and 255
  r = r1 + ( r2 - r1 ) * f
  g = g1 + ( g2 - g1 ) * f
  b = b1 + ( b2 - b1 ) * f
  return rgb( r , g , b )
end function
 
#endif 
Last edited by bluatigro on Apr 21, 2017 12:43, edited 1 time in total.
bluatigro
Posts: 434
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: pong

Postby bluatigro » Apr 21, 2017 12:34

i think i got it

Code: Select all

''les 14 pong

#include "_sprite.bas"
#include "_color.bas"

#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB
#endif

dim shared as integer winx , winy , winbits , mousex , mousey
screeninfo winx , winy , winbits
SCREENRES winx, winy, 32, 2, FB.GFX_FULLSCREEN


dim as sprite top , bottum , bal , human , comp

top.create winx , 30 , 1
top.x = winx / 2
top.y = 30
paint top.img , ( 1 , 1 ) , green
bottum.create winx , 30 , 1
bottum.x = winx / 2
bottum.y = winy - 30
paint bottum.img , ( 1 , 1 ) , green
bal.create 30 , 30 , 1
bal.x = winx / 2
bal.y = winy / 2
paint bal.img , ( 1 , 1 ) , green
human.create 30 , 100 , 1
human.x = winx - 30
human.y = winy / 2
paint human.img , ( 1 , 1 ) , green
comp.create 30 , 100 , 1
comp.x = 30
comp.y = winy / 2
paint comp.img , ( 1 , 1 ) , green

dim as integer baldx = 5 , baldy = 5

do
  screenlock
  cls
  top.draw_it
  bottum.draw_it
  bal.draw_it
  human.draw_it
  comp.draw_it
  screenunlock
 
  if not getmouse( mousex , mousey ) then
    if mousey <> -1 then
      human.y = mousey
    end if
  end if
 
  if comp.y < bal.y then comp.move 0 , 5
  if comp.y > bal.y then comp.move 0 , -5
 
  if bal.hit( top ) then baldy = abs( baldy )
  if bal.hit( bottum ) then baldy = -abs( baldy )
  if bal.hit( human ) then
    baldx = -abs( baldx )
    baldy = ( human.y - bal.y ) / 10
  end if
  if bal.hit( comp ) then
    baldx = abs( baldx )
    baldy = ( comp.y - bal.y ) / 10
  end if
 
  if bal.x < 0 then
    bal.x = winx / 2
    bal.y = winy / 2
  end if
  if bal.x > winx then
    bal.x = winx / 2
    bal.y = winy / 2
  end if
 
  bal.move baldx , baldy
 
  sleep 40
  flip
loop until inkey = chr( 27 )

Code: Select all

''_sprite.bas

type sprite
public :
  dim as integer x , y , w , h , centered
  dim as any ptr img = 0
  declare sub create( a as integer , b as integer , c as integer )
  declare sub move( a as integer , b as integer )
  declare function hit( spr as sprite ) as integer
  declare sub draw_it
end type

sub sprite.create( a as integer , b as integer , c as integer )
  w = a
  h = b
  centered = c
  img = imagecreate( w , h , rgba(0,0,0,0) , 32 )
end sub

sub sprite.move( a as integer , b as integer )
  x += a
  y += b
end sub

function sprite.hit( spr as sprite ) as integer
  dim as integer uit = 1
  if centered then
    if x + w / 2 < spr.x - spr.w / 2 then uit = 0
    if y + h / 2 < spr.y - spr.h / 2 then uit = 0
    if x - w / 2 > spr.x + spr.w / 2 then uit = 0
    if y - h / 2 > spr.y + spr.h / 2 then uit = 0
  else
    if x + w <  spr.x then uit = 0
    if y + h <  spr.y then uit = 0
    if x > spr.x + spr.w then uit = 0
    if y > spr.y + spr.h then uit = 0
  end if
  return uit
end function

sub sprite.draw_it
  if centered then
    put ( x - w / 2 , y - h / 2 ) , img , trans
  else
    put ( x , y ) , img , trans
  end if
end sub


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest