Store › Forums › Hackvision › Game Development › Help needed: Cleaning up code + collision check
- This topic has 5 replies, 2 voices, and was last updated 12 years, 2 months ago by trodoss.
-
AuthorPosts
-
October 10, 2012 at 7:48 pm #442moeMember
(Since neither txt nor ino for file upload is allowed I’m giving you the dropbox link)
So, it’s been a while and now I’ve got something you can call a pre-version of a game… It features 3 menu items: a shop where you can buy upgrades, the game to get money, and a reset function to reset the game save.
I’m now working on my game, so far I’ve got a moving space ship which can shoot bullets. Additionally I’ve got 3 hearts to show the user’s lives. Here comes one of the many problems, I somehow can’t display the hearts under the ship (there are now in the top left corner). My ship isn’t moving while the bullet is displayed, only one bullet can be shot at a time, the bullet and the ship are flickering (really annoying). And I’ve no idea how I could check if my bullet(s) collide with an enemy (with the code I have right now).
My idea is to shoot an random at the top appearing enemy -> explosion -> points + 1 -> next enemy(s).
My code is bad, annoying, ugly, inefficient, complicated. I really want to clean it up but I don’t know where to start. I would like to make it smaller and more efficient. The code itself works (you can compile it with Arduino 1.0.1 and upload it without any syntax errors).
Yeah its kinda messy so please feel free to ask if you do not know what I’ve done there.
Thanks very much!
October 11, 2012 at 1:48 pm #1007trodossMember
void drawShip(int x, int y, boolean nein){
tv.fill(0);
...
It looks like you are blanking the screen each time you are doing things like drawing the ship or other actions.
Something you might want to consider is drawing a filled, black box over the ship image to “erase” it, so that it is not deleting the entire screen.
I use that trick a lot in the code I have, as it is a little more efficient (keeps from wiping the entire buffer between screen retracing).If you are looking for a simple example of how that would work, you can see the following:
http://code.google.com/p/trodoss-arduino/downloads/detail?name=moving_square.zip&can=2&q=I have a code example “Moving Square” that I originally wrote for the VGS that could be converted to the Hackvision fairly easily. The relevant part though is how it is handling the movement (it is just erasing what it needs to).
If you haven’t looked at Michael’s Asteroid code, you can find where he blogged about it here:
http://nootropicdesign.com/projectlab/2011/02/06/asteroids-on-hackvision/There are a few techniques in that code that you might want to consider. He uses a kind of collision detection that you will probably want as you are doing vector-based graphics.
Hopefully some of that helps. I am in a bit of a rush right now, however I will look back over your code a little later and see if there are specific things that I can give you pointers on.
Thanks,
–trodossOctober 11, 2012 at 4:49 pm #851trodossMemberI somehow can’t display the hearts under the ship (there are now in the top left corner).
It looks like you are tracking the position of the ship, so setting the x,y coordinates of the “hearts” would get them to display under your ship.
That being said, it will require lots of “redraw” processing to handle that sort of display though, as every time the position of the ship changed,
an enemy crosses their path, or another screen “event” happens you would want to re-draw these hearts.My ship isn’t moving while the bullet is displayed, only one bullet can be shot at a time, the bullet and the ship are flickering (really annoying).
You have a routine for the bullet that looks like the following:
void bullet(int currentX, int currentY){
int laserX = currentX;
int laserY = currentY;
while (laserY > -50) {
drawShip(currentX, currentY, true);
tv.set_pixel(laserX+5, laserY+5, 1);
tv.set_pixel(laserX+5, laserY+6, 1);
}
}
In that code, there is a loop that is going to loop through until the laser is finished with it’s animation before moving on. What you are after though
is “concurrency”, so that your bullet is “fired” and then the ship continues to move.In order to do that, you are going to want to do the following:
1) track the X, Y of each bullet outside the scope of the “bullet” loop
2) track the “state” of each bullet (not active, fired, hit, etc)You want to think of your bullets as independent objects that you can manage in a structure/collection. So… How familar are you with how structures work?
It seems like that would be the best way of handling how to mananage them. If you are then, great!In your main loop you would want to then handle each of your active bullets (aninmate, and check for collision). That way, they can all act independently,
and the main loop can manage all the game events.
And I've no idea how I could check if my bullet(s) collide with an enemy (with the code I have right now).
…Going back to structures, you are going to want to track “state” of your bullet.
If you are independently tracking the X,Y location of every “object” in your game, you can compare each of their locations (and size) to see if a collision
has occured. If everything is the same “size” it is a little easier. You can use a “bounding box” style of collision detection. If not, then Michael’s
implementation of collision detection is probably the way you would want to go.October 11, 2012 at 4:53 pm #1443trodossMember(Since neither txt nor ino for file upload is allowed I’m giving you the dropbox link)
I understand how that goes.
I use a Google Code site for my Arduino projects, and that at least makes the code available when forum sites will not allow certain attachments.
…So, hopefully the suggestions made will help. If you need anything cleared up just let me know and I will try to do so as best as possible.
Thanks,
–trodossOctober 13, 2012 at 7:16 pm #1444moeMemberWoah this sounds pretty heavy, I’ll try to implement the things you told me, I hope you can help me for further question!
Thanks really much!
October 15, 2012 at 2:04 pm #1447trodossMemberI found a decent explanation of structures here: http://jan.newmarch.name/OS/l5_2.html If you already know what they are and how to use them then great! Just wanted to make sure you had that.
Here are some code fragments to give you an idea of how to use a structure in your code:
At the top of your code (or in a .h file) you would want to have these sort of constants defined
#define MAX_BULLET_RECORDS 3
#define BULLET_HIDDEN 0
#define BULLET_FIRED 1
struct Bullet
{
unsigned char x; //x position
unsigned char y; //y position
unsigned char state; //bullet state
}
At the top of your program you would want to have something like the following:
...
char ship_x; //represent ship X position
char ship_y; //represent ship y position
char bullet_count;
Bullet bullets[MAX_BULLET_RECORDS];
....
I put in fake variables in the code example for the ship to give you an idea, however you would want to keep track of it however you are doing it in your own code.
In your main loop you would want to have something like:
....
//animate bullets, if they are active
handleBullets();
if (Controller.FirePressed())
{
fireBullet();
}
....
The code to these is the following:
void fireBullet()
{
if (bullet_count < MAX_BULLET_RECORDS)
{
bullets[bullet_count].x = ship_x;
bullets[bullet_count].y = ship_y - 10; //assuming the ship is 8 pixels high on the screen, with 2 px of extra space
bullets[bullet_count].state = BULLET_FIRED;
bullet_count++;
}
}
void handleBullets()
{
for (byte i=0; i{
if (bullets.state > BULLET_HIDDEN)
{
//if we have moved off the top of the screen, then hide the bullets
if (bullets.y > 0)
{
bullets.y--;
} else {
bullets.state = BULLET_HIDDEN;
}
}
}
That should be a little more to go on. I know that examples do wonders for my understanding so hopefully this will help as well.
This is just one type of implementation (with a structure). You could do the same thing many different ways. I chose structures since it was easier for me to think of them. The important part however is understanding that you are setting up “state” in the independent bullet objects.
–trodoss
-
AuthorPosts
- You must be logged in to reply to this topic.