Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:43:43+00:00 2026-05-24T00:43:43+00:00

I’m hacking away at a simple game to teach myself C and I’ve come

  • 0

I’m hacking away at a simple game to teach myself C and I’ve come up against an infuriatingly simple problem that I haven’t been able to Google an answer to.

Code follows, apologies for its noobie terribleness (criticisms appreciated!):

#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>

#define AMOUNT_OF_ENEMIES 10
#define AMOUNT_OF_PIXELS_TO_MOVE 50.0

struct enemy
{
    int alive;
    SDL_Rect rect;
};

void create_enemy(struct enemy *position)
{
// Take a pointer to an array. Iterate through array looking for any 'dead' instances.
// (Re)initialise when found, ignore entirely if array is full of alive instances.

int j = 0;
while(position[j].alive == 1 && j < AMOUNT_OF_ENEMIES)
{
    ++j;
}

if(position[j].alive == 0)
{
    position[j].alive = 1;
    position[j].rect.y = 0;
}
}

void update_enemies(struct enemy *position)
{
// Iterate through a passed array looking for alive instances. If found increment     vertical position,
// unless instance is at bottom of screen in which case it's marked as dead.

int j = 0;
while(j < AMOUNT_OF_ENEMIES)
{
    if(position[j].alive == 1)
    {
        position[j].rect.y += 1;
        if(position[j].rect.y > 570)
        {
            position[j].alive = 0;
            }
        }
        ++j;
    }
}

int main(void)
{
// INITS *********************************************************************
int k;
int current_time = 0;
int previous_time = 0;
float difference_in_time = 0.0;

// Load SDL library
if(SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
    printf("Problem, yo\n");
    return 1;
}

// Setup event queue
SDL_Event event;

// Create array to store enemys, initialise it
struct enemy *enemy_array = malloc(sizeof(struct enemy) * AMOUNT_OF_ENEMIES);
int j;
for(j = 0; j < AMOUNT_OF_ENEMIES; ++j)
{
    enemy_array[j].alive = 0;
    enemy_array[j].rect.x = 150;
    enemy_array[j].rect.y = 0;
}

// Create an array to flag keypresses, initialise it
int pressed_keys[323];
int l;
for(l = 0; l < 323; ++l)
{
    pressed_keys[l] = 0;
}

// Create surfaces
SDL_Surface *screen = SDL_SetVideoMode(300, 600, 0, SDL_HWSURFACE);
int black = SDL_MapRGB(screen->format, 0, 0, 0);

SDL_Surface *tower = SDL_LoadBMP("tower.bmp");
SDL_Rect tower_rect;
tower_rect.x = 50;
tower_rect.y = 0;
tower_rect.w = 200;
tower_rect.h = 600;

SDL_Surface *dude = SDL_LoadBMP("dude.bmp");
float dude_x = 0.0;
SDL_Rect dude_rect;
dude_rect.x = 120;
dude_rect.y = 500;
dude_rect.w = 60;
dude_rect.h = 100;

SDL_Surface *enemy = SDL_LoadBMP("enemy.bmp");

// GAME LOOP *****************************************************************
while(1)
{
    current_time = SDL_GetTicks();
    difference_in_time = (float)(current_time - previous_time) / 1000;
    previous_time = current_time;

    if(SDL_PollEvent(&event))
    {
        if(event.key.keysym.sym == SDLK_DOWN)
        {   
            create_enemy(enemy_array);
        }
        else
        {
            switch(event.type)
            {
                case SDL_QUIT:
                    printf("NOOOOOO\n");
                    SDL_FreeSurface(screen);
                    SDL_FreeSurface(tower);
                    SDL_FreeSurface(enemy);
                    free(enemy_array);
                    SDL_Quit();
                return 0;

                case SDL_KEYDOWN:
                    pressed_keys[event.key.keysym.sym] = 1;
                    break;

                case SDL_KEYUP:
                    pressed_keys[event.key.keysym.sym] = 0;
                    break;
            }
        }
    }

    if(pressed_keys[SDLK_LEFT] && dude_rect.x > 50)
    {
        dude_rect.x -= (AMOUNT_OF_PIXELS_TO_MOVE * difference_in_time);
    }

    if(pressed_keys[SDLK_RIGHT] && dude_rect.x < 190)
    {
        dude_rect.x += (AMOUNT_OF_PIXELS_TO_MOVE * difference_in_time);
    }

    update_enemies(enemy_array);

    SDL_FillRect(screen, NULL, black);
    SDL_BlitSurface(tower, NULL, screen, &tower_rect);
    for(k = 0; k < AMOUNT_OF_ENEMIES; ++k)
    {
        if(enemy_array[k].alive == 1)
        {
            SDL_BlitSurface(enemy, NULL, screen, &enemy_array[k].rect);
        }
    }
    SDL_BlitSurface(dude, NULL, screen, &dude_rect);
    SDL_Flip(screen);
}
return 0;
}

The issue arises at this part:

if(pressed_keys[SDLK_LEFT] && dude_rect.x > 50)
{
    dude_rect.x -= (AMOUNT_OF_PIXELS_TO_MOVE * difference_in_time);
}

if(pressed_keys[SDLK_RIGHT] && dude_rect.x < 190)
{
    dude_rect.x += (AMOUNT_OF_PIXELS_TO_MOVE * difference_in_time);
}

The ‘dude’ object moves to the left correctly, but nothing happens when the right arrow key is pressed.

Adding a printf tells me the if statement is being executed correctly. Removing difference_in_time makes it work, so it’s either something to do with that variable or the operation of it and AMOUNT_OF_PIXELS_TO_MOVE.

I just can’t for the life of me figure out why the former block executes correctly and the latter (which is essentially the same thing) doesn’t. I’m sure it’s something simple I’ve overlooked but I’m going insane trying to find it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:43:44+00:00Added an answer on May 24, 2026 at 12:43 am

    Your problem is due to rounding.

    For your “dude” you are using a SDL_Rect, that uses integer coordinates (short int if I remember correct).

    You configured your dude speed to 50 and if your game is running at 60fps (probably due to its simplicity and it may be much more if vsync is off) you will get each frame a movement value of 0.83333.

    This value will be truncated to a int and the result will be zero, for example, if dude.x is 10 and you press right, the calculated value will be 10.83 and when truncated this will result in 10.

    For left, it works because the value is rounded down, assuming again dude.x is 10, when left is pressed, on the first iteration the calculated value would be 9.17, truncating this will give you 9.

    Simple, bad and Hack Solution

    Increase AMOUNT_OF_PIXELS_TO_MOVE to a higher value that forces the int to increase, this will fix the problem.

    Good Solution

    Does not use SDL_Rect for storing your characters position, create a “MyRect” and use float values in it and only does rounding when drawing the character. Actually you only need to store the character position, so I would create a Point2D struct with only x and y and use this to keep track of characters position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.