Skip to: Site menu | Main content


Welcome to PSP-Programming.com, a place for developers to get together.

Welcome to the forums. Here you can find other user tutorials as well as homebrew releases and the source code repository. You can also ask for help with your code here and post your own homebrew!

PSP-Programming.com Forums
February 10, 2012, 01:03:49 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

News: Join our IRC channel: ##psp-programming on freenode
Home Help Search Shop Login Register
Digg This!
Pages: [1]
Print
Author Topic: Help with my game  (Read 650 times)
patrizio
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 6
757.42 points

View Inventory
Send Money to patrizio

View Profile
« on: March 19, 2010, 02:28:06 PM »

This is my first post so let me know if im doing anything wrong

I copied a lot of the code from this guide http://www.psp-programming.com/forums/index.php/topic,961.html


it is supposed to show a little blue guy named blinky . you can move him around with the analog stick and shoot (by pressing cross) at the falling stars. It worked well before but now i tried to make the backround scroll when you move. when i start it the psp just freezes and turns off . here is the code
Please help  Smile
Code:
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include "graphics.h"

#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))

static int GetRandomNum(int lo, int hi)
{
 SceKernelUtilsMt19937Context ctx;
 sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
 u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
 rand_val = lo + rand_val % hi;
 return (int)rand_val;
}

void Game()
{
 SceCtrlData pad;
 int BlinkyPosX = 170;
 int BlinkyPosY = 170;
 int G = 0;
 int F = 480;
 int BX = 0;
 int BY = 0;
 int AY = 0;
 int AX = 0;
 int O = 0;
 int BlinkTimer = 0;
 int Score = 0;
 char ScoreAsChar[3];
 int DeadStars = 0;
 Color White = RGB(255, 255, 255);
 Color Black = RGB(0, 0, 0);
 sceCtrlSetSamplingCycle(0);
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);

 Image* BlinkyImages[10];
 BlinkyImages[0] = loadImage("./Images/Idle.png");
 BlinkyImages[1] = loadImage("./Images/IdleBlink.png");
 BlinkyImages[2] = loadImage("./Images/Left.png");
 BlinkyImages[3] = loadImage("./Images/LeftBlink.png");
 BlinkyImages[4] = loadImage("./Images/Right.png");
 BlinkyImages[5] = loadImage("./Images/RightBlink.png");
 BlinkyImages[6] = loadImage("./Images/Up.png");
 BlinkyImages[7] = loadImage("./Images/UpBlink.png");
 BlinkyImages[8] = loadImage("./Images/Down.png");
 BlinkyImages[9] = loadImage("./Images/DownBlink.png");

 Image* Star = loadImage("./Images/Star.png");
 Image* B = loadImage("./Images/b.png");
 Image* T = loadImage("./Images/T.png");
 int FrameCounter = 0;

 //ADDITION
 int StarPosX = GetRandomNum(G, F);
 int StarPosY = 0;
 int StarTimer = 0;
 
 extern int DifficultySetting;

 while(1)
 {
 BY = BlinkyPosY;
 BX = BlinkyPosX+53;
 sceCtrlReadBufferPositive(&pad, 1);

 FrameCounter = 0;
 StarTimer++;
 BlinkTimer++;

     
 if(pad.Buttons & PSP_CTRL_CROSS) {
  O = 1;
  }
 if(pad.Lx < 50)
      {
      BlinkyPosX -= 4;
      FrameCounter = 2;
      }

 if(pad.Lx > 180)
      {
      BlinkyPosX += 4;
      FrameCounter = 4;
      }
 if(pad.Ly < 50)
      {
      BlinkyPosY -= 4;
      FrameCounter = 6;
      }

 if(pad.Ly > 180)
      {
      BlinkyPosY += 4;
      FrameCounter = 8;
      }
 if (BlinkTimer > 100)
    {
     FrameCounter++;
    }

 if (BlinkTimer > 120)
    {
    BlinkTimer = 0;
    }

 if (BlinkyPosX < 0)
    {
      BlinkyPosX = 0;
    }
 else if (BlinkyPosX > (480 - 106))
    {
     BlinkyPosX = (480 - 106);
    }
if (BlinkyPosY < 0)
    {
      BlinkyPosY = 0;
    }
 else if (BlinkyPosY > (272 - 72))
    {
     BlinkyPosY = (272 - 72);
    }
if (BlinkyPosX < 20)
    {
    if (G > 0)
    {
    G--;
F--;
    }
}
 else if (BlinkyPosX > (460 - 106))
    {
     if (F < 960)
{
F++;
G++;
}
    }   
 
 if (StarPosX < AX)
    {
     if (StarPosX + Star->imageWidth > AX + B->imageWidth)
        {
if (StarPosY < AY)
    {
     if (StarPosY + Star->imageWidth > AY + B->imageWidth)
        {
        if (StarTimer > 60)
           {
   StarPosY = 0;
           Score++;
           DeadStars++;
           StarPosX = GetRandomNum(G, F);
           StarTimer = 0;
   O = 0;
           }
        }
    }
}
}
 blitAlphaImageToScreen(G, 0, F, 272, T, 0, 0);
 blitAlphaImageToScreen(0, 0, 106, 72, BlinkyImages[FrameCounter], BlinkyPosX, BlinkyPosY);
 sceCtrlReadBufferPositive(&pad, 1);
  if(pad.Buttons & PSP_CTRL_CROSS) {
  O = 1;
  AY = BY;
  AX = BX;
  }
  if (O == 1) {
  blitAlphaImageToScreen(0, 0, 10, 10, B, AX, AY);
  AY -=5;
  }
 if (StarPosY > 272)
    {
     DeadStars++;
     StarPosX = GetRandomNum(G, F);
StarPosY = 0;
     StarTimer = 0;
    }
 else if (StarTimer > 60)
      {
       blitAlphaImageToScreen(0, 0, 40, 35, Star, StarPosX, StarPosY);
      }
  if (StarTimer > 60)
  {
  if (DifficultySetting == 1)
  {
  StarPosY += 1.5;
  }
  if (DifficultySetting == 2)
  {
  StarPosY += 2;
  }
  if (DifficultySetting == 3)
  {
  StarPosY += 2.5;
  }
  }
 sprintf(ScoreAsChar, "%d", Score);
 printTextScreen(460, 252, ScoreAsChar, Black);

 
 if (DeadStars == 30)
    {
     fillScreenRect(White, 0, 0, 480, 272);
     printTextScreen(150, 130, "Finished! You Scored", Black);
     printTextScreen(230, 140, ScoreAsChar, Black);
printTextScreen(248, 140, "/30", Black);
if (Score == 30)
{
printTextScreen(150, 150, "You won!", Black);
}
     flipScreen();
     sceKernelDelayThread(5000000);
fillScreenRect(White, 0, 0, 480, 272);
     printTextScreen(150, 130, "Finished! You Scored", Black);
     printTextScreen(230, 140, ScoreAsChar, Black);
printTextScreen(248, 140, "/30", Black);
if (Score == 30)
{
printTextScreen(150, 150, "You won!", Black);
}
printTextScreen(150, 160, "Press [x] to continue", Black);
flipScreen();
while(1) {
  sceCtrlReadBufferPositive(&pad, 1);
  if(pad.Buttons & PSP_CTRL_CROSS) {
     break;
}
}
break;
    }
 sceDisplayWaitVblankStart();
    flipScreen();
 }
 
 int i;
 for (i = 0;i < 10;i++)
     freeImage(BlinkyImages[i]);
     
 freeImage(Star);
 freeImage(B);
 
  sceKernelSleepThread();
    return 0;
}
/code]
« Last Edit: March 20, 2010, 10:23:47 AM by patrizio » Logged


rosera
Newbie
*

Karma: +0/-2
Offline Offline

Posts: 15
801.42 points

View Inventory
Send Money to rosera

View Profile
« Reply #1 on: March 20, 2010, 03:51:24 PM »

Dude, that is some bad code. I assume you are on Windows (Linux is case sensitive). To fix place comment out code segments. As you have letters for variable names its not helping  Rolling Eyes

example
Code:

#if 0
// This code will not run with the #if set to 0
if (badcode)
{
}
#endif


Try and comment the code as you go along and use sensible variable names, so if you do hit a wall, its more obvious what your are trying to achieve. First thing I would check is the graphics files are named correctly and in the right place (make it all the same case as well), then the code.
Logged
patrizio
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 6
757.42 points

View Inventory
Send Money to patrizio

View Profile
« Reply #2 on: March 20, 2010, 04:09:23 PM »

Sorry about the bad code  Sad

The graphics files are in the right place and named correctly so thats not why it isn't working

Any other idea's?
Logged
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #3 on: March 20, 2010, 08:57:10 PM »

This is very convoluted, but I can see what some problems are, structurally speaking.
It looks like you put a lot of work into this.  Too much, to be completely honest, lol.  Smile

There are some techniques that you can use to make this far easier to read and, more importantly, far easier to write. 

...

I don't see what's causing it not to run at first glance, and it's too confusing to try to read over it again.  Also, I don't see the entry point here, so it may be that the error is elsewhere.

First and foremostly you need to use less ambiguous variable names, like rosera said.

Image* B //do not do this
Image* bullet_image //do this instead (B is the bullet, right?)

This will help us a lot in understanding what the hell is going on here.  It will also help you a lot when you're working with a large project, or in a year or so when you decide to come work on this project again.

Secondly, COMMENT YOUR CODE.  Overcomment if you have to.  Always write code as though you're going to show it off to someone and the comments are your running commentary on how it works.  When you come back to a piece of code that you haven't messed with for a little while the comments will help you recognize it instantly.  And if the code doesn't work and you have to post it here we'll know right away what on earth is gong on.

Thirdly, you have a scene running here, but its order is all mashed up.  You check the control pad more than once per frame and respond to the same input more than once per frame.  Try to rewrite it so that it follows this pattern:

Code:
int game() {
  //declare and initialize variables for this scene
  //allocate images, etc (CHECK THE RETURN VALUES FOR ERRORS)

  int running = 1; //just set this to zero to quit the scene

  while(running) {
    //increment ALL counters here

    sceCtrlPeekBufferPositive(/*etc*/);

    //react to control pad state and counter state by setting the
    //values of all the variables that determine what to draw (ALL of them)

    //draw ALL images

    //draw ALL text
   
    //swap screen buffers
  }
 
  //free allocated memory

  //return idnum of next scene so main control loop knows what scene to start next
}

You nearly have this, but it looks like you took a left turn somewhere.  You have strange things like this:

Code:
    if (StarPosY > 272) { //star reached screen bottom, spawn new star
      DeadStars++; //apparently 30 of these ends the game
      StarPosX = GetRandomNum(G, F);
      StarPosY = 0;
      StarTimer = 0;
    } else if (StarTimer > 60) { //blit star if timer exceeds 60 (wtf?)
      blitAlphaImageToScreen(0, 0, 40, 35, Star, StarPosX, StarPosY);
    }
    if (StarTimer > 60) { //star moves down at speed according to difficulty
      //(where is the star timer reset?)
      if (DifficultySetting == 1) {StarPosY += 1.5;}
      if (DifficultySetting == 2) {StarPosY += 2;}
      if (DifficultySetting == 3) {StarPosY += 2.5;}
      //this method is wrong.  StarPosY is an int, not a float
      //also, it should move by 1 per motion and just move more often for higher diff
    }

Why are you moving the star after you've drawn it?  (You can see I've put some comments in there trying to figure out what you're doing as well.)

You also have a 'nested' scene at the bottom there that handles the gameover condition.  It's a pretty small one, but personally I'd make it a completely new scene, just for the sake of simplicity.

This is a lot to deal with at first, so I'll leave it at that for now.  If you rewrite this according to what I described I'll bet any errors in the code will leap out at you.

There are some other things that you can do here to make your life a lot easier, but those are the most important for now.  If there's anything you don't understand about what I said please ask.  Once you've got the code rehashed please show the new code.  There's some other things I'd like to tell you about that can help out a lot, such as using a spritesheet (saves RAM, impresses girls) and using structs to create a more object oriented approach (makes code more conceptual and easier to work with).
« Last Edit: March 20, 2010, 09:53:51 PM by AlphaDingDong » Logged
patrizio
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 6
757.42 points

View Inventory
Send Money to patrizio

View Profile
« Reply #4 on: March 21, 2010, 03:20:35 PM »

i did what you said but i still cant figure out whats wrong  Sad

here is the new code
Code:
#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdio.h>
#include "graphics.h"
#include "Over.h"

#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))

static int GetRandomNum(int lo, int hi)
{
 SceKernelUtilsMt19937Context ctx;
 sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
 u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
 rand_val = lo + rand_val % hi;
 return (int)rand_val;
}

void Game()
{
 SceCtrlData pad;
 int BlinkyPosX = 170;//blinkys x position
 int BlinkyPosY = 170;//blinkys y position
 int BackroundBegin = 0;//the start of where the backround is shown on the screen
 int BackroundEnd = 480;//the end of where the backround is shown on the screen
 int GunPosX = 0;//where the bullet shoots from
 int GunPosY = 0;//where the bullet shoots from
 int BulletPosY = 0;//the bullets y position
 int BulletPosX = 0;//the bullets y position
 int BulletShot = 0;//turned on when you shoot
 int BlinkTimer = 0;//tells blinky when to blink
 int Score = 0; //your score
 char ScoreAsChar[3];
 int DeadStars = 0; //tells when to end the game
 int FrameCounter = 0;//tells what picture of blinky to show
 int StarPosX = GetRandomNum(BackroundBegin, BackroundEnd);//randomly picks the stars x position
 int StarPosY = 0;//the y position of the star
 int Running = 1;//tells when the loop should run
 extern int DifficultySetting;//the difficulty
 Color White = RGB(255, 255, 255);
 Color Black = RGB(0, 0, 0);
 
 Image* BlinkyImages[10];
 BlinkyImages[0] = loadImage("./Images/Idle.png");
 BlinkyImages[1] = loadImage("./Images/IdleBlink.png");
 BlinkyImages[2] = loadImage("./Images/Left.png");
 BlinkyImages[3] = loadImage("./Images/LeftBlink.png");
 BlinkyImages[4] = loadImage("./Images/Right.png");
 BlinkyImages[5] = loadImage("./Images/RightBlink.png");
 BlinkyImages[6] = loadImage("./Images/Up.png");
 BlinkyImages[7] = loadImage("./Images/UpBlink.png");
 BlinkyImages[8] = loadImage("./Images/Down.png");
 BlinkyImages[9] = loadImage("./Images/DownBlink.png");
 Image* Star = loadImage("./Images/Star.png");
 Image* Bullet = loadImage("./Images/b.png");
 Image* Backround = loadImage("./Images/T.png");
 

 while(Running)
 {
 //-----------------------------increment and set variables
 GunPosY = BlinkyPosY;//Makes where the bullet shoots from be where blinky is
 
 GunPosX = BlinkyPosX+53;//+53 so the bullet shoots from the middle of blinky
 
 FrameCounter = 0;//Resets blinky's blinking
 
 BlinkTimer++;//Adds to the timer that causes him to blink
 
 if (BlinkTimer > 100)//If it is time for him to blink
    {
     FrameCounter++;//Makes blinky blink
    }
 sceCtrlReadBufferPositive(&pad, 1);
 //-----------------------------get input
 
 if(pad.Buttons & PSP_CTRL_CROSS)//X is pressed
    {
BulletShot = 1;//Shoots bullet
BulletPosY = GunPosY;//makes bullet be at where it shoots from
BulletPosX = GunPosX;//makes bullet be at where it shoots from
}
 
 if(pad.Lx < 50)//analog left
    {
    BlinkyPosX -= 4;//moves him left
    FrameCounter = 2;//makes him look left
    }

 if(pad.Lx > 180)//analog right
    {
    BlinkyPosX += 4;//moves him right
    FrameCounter = 4;//makes him look right
    }
 
 if(pad.Ly < 50)//analog up
    {
    BlinkyPosY -= 4;//moves him up
    FrameCounter = 6;//makes him look up
    }

 if(pad.Ly > 180)//analog down
    {
    BlinkyPosY += 4;//moves him down
    FrameCounter = 8;//makes him look down
    }
//---------------------------other stuff with variables
 if (BlinkTimer > 120)//makes blinky stop blinking
    {
    BlinkTimer = 0;
    }

 if (BlinkyPosX < 0)//stops him from going of screen
    {
      BlinkyPosX = 0;
    }

 else if (BlinkyPosX > (480 - 106))//stops him from going of screen
    {
     BlinkyPosX = (480 - 106);
    }

if (BlinkyPosY < 0)//stops him from going of screen
    {
      BlinkyPosY = 0;
    }

 else if (BlinkyPosY > (272 - 72))//stops him from going of screen
    {
     BlinkyPosY = (272 - 72);
    }

if (BlinkyPosX < 20)//blinky is close to left side of screen
    {
    if (BackroundBegin > 0)//if backround isnt all the way to the left
    {
     BackroundBegin--;//moves backround left
BackroundEnd--;//moves backround left
    }
}

 else if (BlinkyPosX > (460 - 106))//blinky is close to right side of screen
    {
    if (BackroundEnd < 960)//if backround isnt all the way to the right
{
BackroundEnd++;//moves backround right
BackroundBegin++;//moves backround right
}
    }   
    if (StarPosY > 272)//if star goes off screen
    {
     DeadStars++;//adds to timer that eventualy ends the game
     StarPosX = GetRandomNum(BackroundBegin, BackroundEnd);//puts the star at a random x position
StarPosY = 0;//puts star at top of screen
    }

if (DeadStars == 30)//if the game is over
    {
     Running = 0;
    }

  if (DifficultySetting == 1)
  {
  StarPosY += 1.5;
  }
  if (DifficultySetting == 2)
  {
  StarPosY += 2;
  }
  if (DifficultySetting == 3)
  {
  StarPosY += 2.5;
  }
 
 if (StarPosX < BulletPosX)
     {
      if (StarPosX + Star->imageWidth > BulletPosX + Bullet->imageWidth)
       {
        if (StarPosY < BulletPosY)
         {
           if (StarPosY + Star->imageWidth > BulletPosY + Bullet->imageWidth)
            {
           //runs if the bullet hits the star
   
   StarPosY = 0;//puts star at top of screen
           Score++;//adds to score
           DeadStars++;//adds to timer that eventualy ends the game
           StarPosX = GetRandomNum(BackroundBegin, BackroundEnd);//puts the star at a random x position
   BulletShot = 0;//makes bullet go away
           
            }
         }
   }
}
//-----------------------------draw images

blitAlphaImageToScreen(BackroundBegin, 0, BackroundEnd, 272, Backround, 0, 0);//show backround

blitAlphaImageToScreen(0, 0, 106, 72, BlinkyImages[FrameCounter], BlinkyPosX, BlinkyPosY);//show blinky

blitAlphaImageToScreen(0, 0, 40, 35, Star, StarPosX, StarPosY);//show star

if (BulletShot == 1) //if the bullet has been shot
{
blitAlphaImageToScreen(0, 0, 10, 10, Bullet, BulletPosX, BulletPosY);//show bullet
}

//-------------------------------print text

sprintf(ScoreAsChar, "%d", Score); //show score

 printTextScreen(460, 252, ScoreAsChar, Black); //show score
 
//-------------------------------finish up

 sceDisplayWaitVblankStart();
 
    flipScreen();

 }
 //------------------------------free images
 int i;
 for (i = 0;i < 10;i++)
     freeImage(BlinkyImages[i]);
     
 freeImage(Star);
 freeImage(Bullet);
 freeImage(Backround);
 Over();//The end menu
 }
Logged
mowglisanu

C/C++ Developer
Hero Member
*

Karma: +36/-11
Offline Offline

Posts: 787
0.00 points

View Inventory
Send Money to mowglisanu


View Profile
« Reply #5 on: March 21, 2010, 07:18:22 PM »

graphics.h\c doesn't load images greater than 512 pixels(width or height)
you should check if your resources load
Logged

Check out my:
 Audio lib
 Pmf Viewer
AlphaDingDong
Combat Muskrat
All-Around Dev
Hero Member
*

Karma: +32/-3
Offline Offline

Posts: 882
2044.69 points

View Inventory
Send Money to AlphaDingDong
Hey... Are you gonna eat that?


View Profile
« Reply #6 on: March 22, 2010, 01:59:56 AM »

I don't see anything that would cause a crash, apart from what mowgli mentioned.  There's some minor logical errors and a lot of things that can be simplified, but nothing fatal.  In fact, it looks like your background image is 960 wide, just judging from your code, so that's probably the error.

Here's some advice.

Write a raise() function that prints an error message and sleeps the thread, then:
Code:
  //-----------------------------error check
  int i;
  for(i = 0; i < 10; ++i) {
    if(BlinkyImages == NULL) {raise("Failed to load Blinky image file.\n");}
  }
  if(Star == NULL) {raise("Failed to load Star image file.\n");}
  if(Bullet == NULL) {raise("Failed to load Bullet image file.\n");}
  if(Background == NULL) {raise("Failed to load Background image file.\n");}

Check this out:
Code:
    GunPosY = BlinkyPosY; //~~~ these two variables are not necessary (see below)
    GunPosX = BlinkyPosX+53;

...

    if(pad.Buttons & PSP_CTRL_CROSS) { //fire
      BulletShot = 1; //~~~ can fire bullets while one is already onscreen!
      BulletPosY = BlinkyPosY;
      BulletPosX = BlinkyPosX + 53;
    }

This line comes too early.  Blinky will never blink!:
Code:
    if(BlinkTimer++ > 100) {FrameCounter++;} //~~~ think this should maybe come later
Just put it after your control logic.

In fact, it can go just before this:
Code:
if(BlinkTimer > 120) {BlinkTimer = 0;} //makes blinky stop blinking

However, that line can be replaced with:
Code:
BlinkTimer %= 121;
Which is faster and makes you look cool.

You have bounds checking for blinky:
Code:
    //~~~ these checks should be done during input checking
    if(BlinkyPosX < 0) {BlinkyPosX = 0;}
    else if(BlinkyPosX > (480 - 106)) {BlinkyPosX = (480 - 106);}
    if(BlinkyPosY < 0) {BlinkyPosY = 0;}
    else if(BlinkyPosY > (272 - 72)) {BlinkyPosY = (272 - 72);}

But you can put that in the input check like this:
Code:
    if(pad.Lx < 50) { //left
      BlinkyPosX -= 4;
      FrameCounter = 2;
      if(BlinkyPosX < 0) {BlinkyPosX = 0;}
    }
Which looks cleaner and will save you from making useless checks every frame. (faster)

Also:
Code:
if(DeadStars == 30) {Running = 0;} //~~~ put this inside the (StarPosY > 272)
For the same reason.

Next is this:
Code:
    //~~~ this should be done mathematically, also _StarPosY_is_integral_
    if(DifficultySetting == 1) {StarPosY += 1.5;}
    if(DifficultySetting == 2) {StarPosY += 2;}
    if(DifficultySetting == 3) {StarPosY += 2.5;}
StarPosY is an integer.  You can't use floating point values with it.  For now I'd recommend just:
Code:
StarPosY += DifficultySetting;
But really I think you should have a motion counter, because even just +1 every frame is going to be very fast. (60 pixels per second!)

Add a new variable 'MotionTimer' and initialize it to zero.  Increment it once every frame.  Reverse your difficulty setting numbers, so that 3 is easy and 1 is hard.  Then do this instead:
Code:
if(MotionTimer > (12 * DifficultySetting)) {
  ++StarPosY;
  MotionTimer = 0;
}

Note:
Code:
    //collision test
    if((StarPosX < BulletPosX) && ((StarPosX + Star->imageWidth) > (BulletPosX + Bullet->imageWidth))) {
      //~~~ this next line is incorrect.  height should be checked but width is checked
      if((StarPosY < BulletPosY) && ((StarPosY + Star->imageWidth) > (BulletPosY + Bullet->imageWidth))) {
        //collision
        BulletShot = 0; //makes bullet go away
        Score++; //adds to score
        DeadStars++; //adds to timer that eventualy ends the game
        StarPosX = GetRandomNum(BackroundBegin, BackroundEnd); //puts the star at a random x position
        StarPosY = 0; //puts star at top of screen
      }
    }
I just cleaned up the syntax a little, but there is an error in there.  I'll leave it for you to fix.

You have variables 'BackroundBegin' and 'BackroundEnd', but you only really need 'BackroundScroll'.  Wink

Overall this is a good start now that it's cleaned up a bit.  You can see I changed some of the formatting in terms of where the brackets are and stuff, but that's just my personal style.

Now I'd like to tell you about using a spritesheet.  Get the program working before you try this, but this is a good thing to know.

Currently you have 10 different images for your blinky sprite.  That's fine, but a spritesheet can make it easier to handle.  What you do is put them all into one image file like this:

Code:
---------------------
|Idle    |IdleBlink |
|Left    |LeftBlink |
|Right   |RightBlink|
|Up      |UpBlink   |
|Down    |DownBlink |
---------------------

In other words, you divide the image into 'cells', each of the exact same width and height as the others.  Then when you want to draw blinky in his left facing blink state, you can select the cell logically/mathematically like this:

Code:
tex_x = 0;
if(blinking_right_now) {tex_x = cell_width;}
//earlier you used 'FrameCounter' to specify a direction.  I'll use that here:
tex_y = (FrameCounter / 2);
tex_y *= cell_height;
blitAlphaImageToScreen(tex_x, tex_y, 106, 72, BlinkyImage, BlinkyPosX, BlinkyPosY);
Make sense?  Smile

Also, I'd like to tell you about object-oriented-programming.

In the OOP model you make each object handle its own 'business'.  For instance, in this case you have the objects: blinky, star, and bullet.  Each one is handled by the procedural code.

What you could do is give each one a struct to hold its own properties:

Code:
typedef struct {
  int x;
  int y;
  int facing;
  int blink_counter;
  Image* spritesheet;
} BlinkyObject;
Get the idea?  All of the variables that deal directly with blinky himself are kept inside his struct.  Then when you set up the scene you just:

Code:
BlinkyObject* blinky = (BlinkyObject*)malloc(sizeof(BlinkyObject));
if(blinky == NULL) {raise("Failed to allocate Blinky!");}
blinky->spritesheet = loadImage("./Images/Blinky.png");
if(blinky->spritesheet == NULL) {raise("Failed to load Blinky's image!");}
blinky->x = 170;
blinky->y = 170;
blinky->facing = 0;

Get the idea?

So why do this?  Simple.  Because then when you want to handle blinky's logic for the frame you can make a seperate function to handle it:

Code:
void updateBlinky(BlinkyObject* blinkyObj) {
  if(pad.Lx < 50) { //'pad' should be global for this to work.
    blinkyObj->x -= 4;
    blinkyObj->facing = 2;
  }
  //etc...
}

If you write the code this way you can just have a few 'update' functions in your control loop that will handle all the logic.  Also, say you wanted to modify the game so that there's more than one star on the screen at once.  In the current code that would get messy, but if stars are just objects then all you have to do is make a new variable for a star object, set it up and call the update function on it once per frame.  You can even have a different number of stars at once depending on difficulty and just use 'for' loops to set them all up and call their update functions, etc.  It means the difference between spending two hours on the code and spending 10 minutes on the code.

Finally, getting used to OOP will prepare you to step into C++, which is basically C except with some upgrades and some neat goodies that make OOP much easier to do.

I'd recommend getting your game to work in its current incarnation, then trying out a spritesheet, and then rewriting it in an OOP flavor.

As always, any questions can be asked here.

You're doing well.  I look forward to seeing your game.  Wink
« Last Edit: March 22, 2010, 02:14:41 AM by AlphaDingDong » Logged
patrizio
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 6
757.42 points

View Inventory
Send Money to patrizio

View Profile
« Reply #7 on: March 22, 2010, 04:21:38 PM »

Thanks for all the useful info   Very Happy

Anyway the image size was the problem!

Thank you guys so much

I attached the finished game
Logged
patrizio
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 6
757.42 points

View Inventory
Send Money to patrizio

View Profile
« Reply #8 on: March 22, 2010, 04:26:37 PM »

having trouble posting my game  Sad

here is the message i get:

Cannot access attachments upload path!
Logged
mowglisanu

C/C++ Developer
Hero Member
*

Karma: +36/-11
Offline Offline

Posts: 787
0.00 points

View Inventory
Send Money to mowglisanu


View Profile
« Reply #9 on: March 22, 2010, 07:43:48 PM »

uploads died a whiles ago
you can use a free file hosting site like mediafire
Logged

Check out my:
 Audio lib
 Pmf Viewer
patrizio
Newbie
*

Karma: +0/-0
Offline Offline

Posts: 6
757.42 points

View Inventory
Send Money to patrizio

View Profile
« Reply #10 on: March 24, 2010, 01:15:00 PM »

heres the link http://www.mediafire.com/file/ld2vmmmqjmt/Blinky!!!.zip
Logged
Pages: [1]
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.255 seconds with 35 queries.
Sister Sites: Guitar Hero 4   BrokeniTouch.com