Tuesday, May 31, 2016

Back to the game code

After over a week of learning about random spawning, collisions and those confusing Lua tables, I finally came back to working on the game proper. Today, I added game states (so I can have a splash screen), and implemented the improved random spawner with a pretty solid collision between the ball and the bricks/enemies. This marks the basic play mechanics as complete, but there is still a lot to do for this game. The next step is coming up with a selection of enemies that will create interesting gameplay, then implementing a good algorithm for spawning them in such way to make the game challenging and fun. It will be interesting to actually try my hand at some video game design after learning  about gamedev and programming for over two weeks... oh and trying to make some passable pixel art.


In the meantime, I'll break down the code for the spawner used in the game:

function create_bug()
--responsible for assigning
--random values to enemies
 local sprites={17,19,21,23}
 local sn = flr(rnd(4)+1) 
 local o={}
 o.sprite=sprites[sn]
 o.x=flr(rnd(110))+1
 o.y=flr(rnd(60))+10
 o.w=2
 o.h=o.w
 o.x2=o.x+o.w*8
 o.y2=o.y+o.h*8
 o.xdir=flr(rnd(2)+1)
 o.ydir=flr(rnd(2)+1)

 overlapped = true
  while overlapped do
   overlapped = false
   foreach(bugs, function(bugs)
    if bugs.x<=o.x+o.w*8+1 and bugs.x+bugs.w*8>=o.x-1 and bugs.y<=o.y+o.h*8+1 and bugs.y+bugs.h*8>=o.y-1
    then
     test+=1
     overlapped = true
     o.x=flr(rnd(110))+1
     o.y=flr(rnd(60))+10
    end
   end)
  end
   add(bugs, o)
end

function col_bugball()
 for a=1,#bugs do
  if bugs[a]!=nil then
   if bugs[a].x<ballx+ballsize
   and bugs[a].x+bugs[a].w*8>ballx 
   and bugs[a].y<bally+ballsize
   and bugs[a].y+bugs[a].h*8>bally then 
    test+=1
    --bounce horizontal
    if ballcenterx<bugs[a].x
    or ballcenterx>bugs[a].x+bugs[a].w*8 then
     ballxdir= -ballxdir
     sfx(01)
    --bounce vertical  
    elseif ballcentery<bugs[a].y
    or ballcentery>bugs[a].y+bugs[a].h*8 then
     ballydir= -ballydir
     sfx(01)
    end 
    del (bugs, bugs[a])
   end
  end
 end  
end

The first part of the code is pretty similar to the one in the post before, but it has few important changes - mainly optimization and changing the enemies from simple rectangles to actual sprites (who are simple rectangles for time being, but that will change). The most important change is in the approach to spawning objects. Instead of running the whole function as in the previous version, I only change the X and Y of the spawned object if the collision is detected. This approach is less memory intensive, although can still manage to crash Pico8, if pushed too hard. Once I implement some enemy types, I will limit the amount of times the program will try to spawn something. Right now, it is a good tool for testing. The other change, swapping IF to WHILE when checking for collisions, solves the issue I had when a the function would check for object N, then spawn object N+1, but it didn't take into account any objects spawned before N so the new object would sometimes overlap the older objects.

The second part function col_bugball() takes care of the collision between the enemies and the ball. It cycles through every enemy in the table bugs and checks for their collision. I am using the formula for collision between squares, as the ball sprite is effectively a 5x5 pixels square and my enemies will stay in roughly rectangular shape. Once it checks for collision, the function checks if the ball hit the block from the side, or top/bottom. This then reverses the ball direction accordingly. I have some small issues with the ball behaving odd here (changing X and Y direction on the same block), which I am still to fix. I will look into it after I have made couple enemy types and see if the issue prevails. Finally, the object that was hit by the ball gets destroyed. This part will get changed to a hitpoint system, once the enemy types will start rolling in. Having some enemies take more than one hit is a staple of breakout games after all.

No comments:

Post a Comment