Sort of overselling this as a project... Arkanoid physics toy

User projects written in or related to FreeBASIC.
Post Reply
SJ Zero
Posts: 139
Joined: Jun 19, 2005 4:12

Sort of overselling this as a project... Arkanoid physics toy

Post by SJ Zero »

I wanted to play with the physics of a pong paddle. Many games just treat the paddle as a wall, this is a test seeing what looking at one simple method of adding predictable variability to the paddle behaviour. Thought I'd share, but I don't think I'm done playing.

Code: Select all

'Pong physics test 1
'by SJ Zero
'09 Oct 2018

#include "fbgfx.bi"

#if __FB_LANG__ = "fb"
Using FB '' Scan code constants are stored in the FB namespace in lang FB
#endif

screen 13,8,2

Const PI As Double = 3.1415926535897932

dim quit as Integer
dim Active_Screen as Integer
dim Passive_Screen as Integer


dim Blocks (0 to 10, 0 to 5) as Integer

dim x as Integer
dim y as integer

   for x = 0 to 10
      for y = 0 to 5
      
      blocks (x,y) = int(rnd(x+y+timer) * 5)
   
      next
      
   next
   

type ball
   x as Double
   y as Double
   xaccel as Double
   yaccel as Double
   mass as Double
   radius as Double
   refractAngleX as double
   refractAngleY as double
end type

Type paddle
   x as Double
   y as Double
   length as double
   Angle as double
end type

dim ball as ball
dim paddle as paddle
dim distance as double

ball.x = 100
ball.y = 100
ball.xaccel = 1
ball.yaccel = 1
ball.mass = 1
ball.radius = 4

paddle.y = 180
paddle.x = 150
paddle.length = 40

while quit = 0
   
   
  
   'The high tech controls
   dim keykey as String
   dim lastkey as String
   
   lastkey = trim(inkey)
   while inkey <> ""
      
   wend
    If MultiKey(SC_LEFT ) Then
      if paddle.x > 10 then paddle.x -= 4 
    end if
    
    If MultiKey(SC_RIGHT) Then
      if paddle.x + paddle.length < 310 then paddle.x += 4
    end if

   select case lastkey
      case "q"
         quit = 1
      
      case ""
         
      case chr(255)+"K" 'Left arrow
            
      case chr(255)+"M" 'right arrow
            
      
      case else
         print lastkey
         print asc(lastkey,1),asc(lastkey,2)
            
   end select


   ' The high tech graphics
   cls
   line (paddle.x,paddle.y)-(paddle.x + paddle.length,paddle.y),2
   circle (ball.x,ball.y),ball.radius,1,,,,f
   
   ball.refractAngleX = Ball.xAccel + ((ball.x - (paddle.x + (paddle.length / 2))) * 0.06)
   ball.refractAngleY = -Ball.yAccel

   line (10,10)-(10,310)
   line (10,10)-(10,200)
   line (310,200)-(310,10)
   line (10,10)-(310,10)
   
   
   for x = 0 to 10
      for y = 0 to 5
      if blocks (x,y) > 0 then
         line (50 + (x * 20),(50 + (y * 10)))-(50 + ((x+1) * 20),(50 + ((y+1) * 10))), blocks (x,y),bf
         line (50 + (x * 20),(50 + (y * 10)))-(50 + ((x+1) * 20),(50 + ((y+1) * 10))), ,b   
      end if
         
   
      next
      
   next
   
   
   
   if Active_Screen = 1 then Active_Screen = 0 else Active_Screen = 1
   if Active_Screen = 1 then Passive_Screen = 0 else Passive_Screen = 1
   Screen , Active_Screen, Passive_Screen
   
   
   'The high tech physics
   for x = 0 to 10
      for y = 0 to 5
      if blocks (x,y) > 0 then
         dim blockx as Double
         dim blocky as double
         dim blockx1 as Double
         dim blocky1 as Double
         
         blockX = 50 + (x * 20)
         BlockY = 50 + (y * 10)
         BlockX1 = 50 + ((x+1) * 20)
         BlockY1 = 50 + ((y+1) * 10)
         
         if ball.x + ball.xaccel + ball.radius > blockX and ball.x + ball.xaccel - ball.radius < BlockX1 then
            if ball.y + ball.yaccel + ball.radius > blocky and ball.y + ball.yaccel - ball.radius < Blocky1 then
               blocks(x,y) -= 1
               
               if ball.x + ball.xaccel + ball.radius > blockX and ball.x + ball.xaccel - ball.radius < BlockX1 and _
                  ball.y + ball.radius > blocky and ball.y - ball.radius < Blocky1 then            
                     
                     Ball.xaccel = -ball.xaccel
               
               end if
               
                if ball.x + ball.radius > blockX and ball.x - ball.radius < BlockX1 and _
                  ball.y + ball.yaccel + ball.radius > blocky and ball.y + ball.yaccel - ball.radius < Blocky1 then            
                     
                     ball.yaccel = -ball.yaccel
               
               end if
                             
               exit for   
            end if
         end if                      
      end if
         
   
      next
      
   next 
      
   ball.x = ball.x + ball.xaccel
   ball.y = ball.y + ball.yaccel
   
   distance = abs(paddle.x + (paddle.length / 2) - ball.x)
   if (distance < (ball.radius + (paddle.length / 2))) and (ball.y + ball.radius) > paddle.y then
      ball.xaccel = ball.refractAngleX
      ball.yAccel = Ball.RefractAngleY
   end if
   
   if (ball.x - ball.radius) < 10 then
      ball.xaccel = -ball.xaccel
   end if

   if (ball.y - ball.radius) < 10 then
      ball.yaccel = -ball.yaccel
   end if

   if (ball.x + ball.radius) > 310 then
      ball.xaccel = -ball.xaccel
   end if

   if (ball.y + ball.radius) > 200 then
      screen ,0, 0
      cls
      print "You died."
      sleep
      end 'dead!
   end if
   
   dim won as Integer
   won = 1
   for x = 0 to 10
      for y = 0 to 5
            if blocks(x,y) > 0 then won = 0
      next
   next
   
   if won = 1 then
         screen ,0, 0
      cls
      print "You won. You monster."
      sleep
      end 'dead!
      
   end if

   
   'The high tech timing
   
   sleep 15
   
wend

Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: Sort of overselling this as a project... Arkanoid physics toy

Post by Imortis »

Hey, haven't seen you around here in forever! Glad to see you back!
h4tt3n
Posts: 698
Joined: Oct 22, 2005 21:12
Location: Denmark

Re: Sort of overselling this as a project... Arkanoid physics toy

Post by h4tt3n »

Nice! An obvious way of expanding this would be to add rotation to the ball. You could then make it spin using the friction of the paddle, allowing some insane shots. I've got all the physics stuff lying around if you'd like to see some examples.
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Re: Sort of overselling this as a project... Arkanoid physics toy

Post by Lachie Dazdarian »

This plays smooth and nice. Thanks.

But yeah, I'm not sure if I ever seen anyone trying to actually make a "physics" Arkanoid game, with the spin shots and what not. Really opens up the concept to many ideas. You could even consider a completely different types of goals for levels. I always wanted to try this type of game where you would catch the ball by deafult and then throw it back (maybe some robot hand), using various tricks, to knock off some contraption above to do the right thing, and then maybe knock it again on another place in the right moment, triggered by the previous knock.
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Sort of overselling this as a project... Arkanoid physics toy

Post by BasicCoder2 »

The problem I had with it was the size of the screen (window).
Have you looked as this thread? Much nicer to use the mouse instead of the keys to move the bat.
viewtopic.php?f=15&t=21663&hilit
My version had no physics involved but I think some use the position the ball hits the bat to vary the angle of reflection.
mrminecrafttnt
Posts: 131
Joined: Feb 11, 2013 12:23

Re: Sort of overselling this as a project... Arkanoid physics toy

Post by mrminecrafttnt »

change

Code: Select all

dim Active_Screen as Integer
dim Passive_Screen as Integer
to

Code: Select all

dim Active_Screen as Integer = 1
dim Passive_Screen as Integer = 0
and

Code: Select all

 if Active_Screen = 1 then Active_Screen = 0 else Active_Screen = 1
if Active_Screen = 1 then Passive_Screen = 0 else Passive_Screen = 1
to

Code: Select all

swap  Active_Screen, Passive_Screen
for performanceboost..

This Graphics is beutifull but very old.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Sort of overselling this as a project... Arkanoid physics toy

Post by D.J.Peters »

Code: Select all

screenres w,h,bpp,2
screenset 1,0

while inkey()<>27
  draw_new_scene() ' on hidden work page
  flip ' make hidden work page visible
  sleep 1000\fps
wend
Post Reply