Writing a simple game in C for practice. For whatever reason, the ball doesn't move the correct direction when it should. Here's the code:

Code:
#include <os.h>
#include "utils.h"
#include "graphics3.h"
#include <common.h>

asm(".string \"PRG\"\n");

int main(void)
{

   char paddle1_sprite[200] = {
      0xFF,0x00,0x00,0x00,0x00,
      0xF0,0xFF,0xFF,0xFF,0xFF,
      0x0F,0xFF,0xFF,0xFF,0xFF,
      0x0F,0xFF,0xFF,0xFF,0xFF,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xFF,
      0x0F,0xFF,0xFF,0xFF,0xFF,
      0xF0,0xFF,0xFF,0xFF,0xFF,
      0xFF,0x00,0x00,0x00,0x00
   };
   
      char paddle2_sprite[200] = {
      0x00,0x00,0x00,0x00,0xFF,
      0xFF,0xFF,0xFF,0xFF,0x0F,
      0xFF,0xFF,0xFF,0xFF,0xF0,
      0xFF,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0x0F,0xFF,0xFF,0xFF,0xF0,
      0xFF,0xFF,0xFF,0xFF,0xF0,
      0xFF,0xFF,0xFF,0xFF,0xF0,
      0xFF,0xFF,0xFF,0xFF,0x0F,
      0x00,0x00,0x00,0x00,0xFF
   };
   
      char ball_sprite[50] = {
      0xFF,0xF0,0x00,0x0F,0xFF,
      0xFF,0x0F,0xFF,0xF0,0xFF,
      0xF0,0x0F,0x00,0xF0,0x0F,
      0x0F,0xF0,0xFF,0x0F,0xF0,
      0x0F,0x0F,0x00,0xF0,0xF0,
      0x0F,0x0F,0x00,0xF0,0xF0,
      0x0F,0xF0,0xFF,0x0F,0xF0,
      0xF0,0x0F,0x00,0xF0,0x0F,
      0xFF,0x0F,0xFF,0xF0,0xFF,
      0xFF,0xF0,0x00,0x0F,0xFF
   };

   char* scrbuf = malloc(SCREEN_BYTES_SIZE);
   int ball_vel_x = -1;
   int ball_x = 175;
   int ball_y = 0;
   int ball_vel_y = 1;
   int paddle1 = 100;
   int paddle2 = 100;
   int a;
   int Lookuptable[10] = {1,3,6,3,1,-5,-3,0,-3,5};
   int Lost_The_Game = 0;
   while (!isKeyPressed(KEY_NSPIRE_ESC) && !Lost_The_Game)
   {
      memset(scrbuf,0xFF,SCREEN_BYTES_SIZE);
      sprite(scrbuf,paddle1_sprite,0,paddle1,10,40);
      sprite(scrbuf,paddle2_sprite,310,paddle2,10,40);
      sprite(scrbuf,ball_sprite,ball_x,ball_y,10,10);
      for (a=0;a<10;a++){
         if (isKeyPressed(KEY_NSPIRE_UP) && paddle1 > 0)
            paddle1--;
         if (isKeyPressed(KEY_NSPIRE_DOWN) && paddle1 < 200)
            paddle1++;
         if (a % 2)
         {
            if (ball_y > paddle2+20 && paddle2 > 0)
               paddle2--;
            if (ball_y < paddle2+20 && paddle2 < 201)
               paddle2++;
         }
      }
      
      ball_x += ball_vel_x;
      ball_y += ball_vel_y;
      
      if (ball_y < 0 || ball_y > 230)
      {
         ball_vel_y *= -1;
         ball_y += ball_vel_y;
      }
      
      if (ball_x < 11)
      {
         a = ball_y - paddle1;
         if (a < 0 || a > 40)
            Lost_The_Game = 1;
         ball_vel_x = Lookuptable[a/8];
         ball_vel_y = Lookuptable[a/8+5];
      }
      if (ball_x > 309)
      {
         a = ball_y - paddle2;
         if (a<0 || a>40)
            Lost_The_Game = 1;
         ball_vel_x = - Lookuptable[a/8];
         ball_vel_y = Lookuptable[a/8+5];
      }
      refresh(scrbuf);
   }
   free(scrbuf);
   for (a=0;a<300;a++){}
   return 0;
}
The first thing I notice is this code for the enemy paddle:


Code:
            if (ball_y > paddle2+20 && paddle2 > 0)
               paddle2--;
            if (ball_y < paddle2+20 && paddle2 < 201)
               paddle2++;


That's going to move the paddle away from the ball in both cases. I think this is more logically correct:


Code:
            if (ball_y < paddle2 && paddle2 > 0)
               paddle2--;
            if (ball_y > paddle2+20 && paddle2 < 201)
               paddle2++;


Also note where I've changed paddle2+20 to paddle2 in one of the conditional statements.
The plus 20 was for the midpoint, the whole paddle is 40. Should it still be changed to that?
_player1537 wrote:
The plus 20 was for the midpoint, the whole paddle is 40. Should it still be changed to that?
Then may I recommend something like the following? I just think it looks a bit nicer, such that the enemy paddle will stay still sometimes instead of constantly moving.

cf. http://www.ticalc.org/archives/files/fileinfo/342/34299.html


Code:
            if (ball_y > paddle2+30 && paddle2 > 0)
               paddle2--;
            if (ball_y < paddle2+10 && paddle2 < 201)
               paddle2++;


Does that seem like a good idea? If so, let's also discuss the ball moving problems. Can you be any more specific.
Does sound like a good idea Smile

Oh, and there was no ball moving problem, only the paddle moving one
_player1537 wrote:
Does sound like a good idea Smile

Oh, and there was no ball moving problem, only the paddle moving one
Oh ok, then test this and let me know if it works. Do you want to toss in some extras, since you're working with C, like difficulty levels or something? IE, make the AI miss sometimes or something?
Was actually just working on the AI Razz

What aresome cool effects for a pong game? My friend suggested maybe like lightning strikes (have to figure out exactly what it'd do). Whatever it is, I want it to be very graphical Smile

Edit: Oh, and that worked very well Very Happy I changed it to 5 and 35 respectivly though
Take a look at my DCS Pong game for some ideas. I think powerups for different paddle sizes are always fun, speed up and down, some kind of weapons of course...
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement