Jump to content
  • Advertisement
Sign in to follow this  
RedRabbit

More Ball Problems...

This topic is 5393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Arg! If you've been following my last thread I got my ball, and its moving! But now when it reaches a certain point on the y axis (both up and down) it starts to trail itself for no apparent reason...Ill provide code this time :) itll prolly help a little more: [main.cpp]
#include "draw.h"

int main(int argc, char **argv)
{
	Uint8* keys;

	SDL_Init(SDL_INIT_VIDEO);

	screen = SDL_SetVideoMode(800,600,32, SDL_HWSURFACE | SDL_DOUBLEBUF);

	SDL_WM_SetCaption("My Pong Clone v0.1",NULL);
	InitImages();
	DrawBG();

	int done=0;

	while (done == 0)
	{
		SDL_Event event;

		while (SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT) { done = 1; }
			if(event.type == SDL_KEYDOWN)
			{
				if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; }
			}
		}
			
		keys = SDL_GetKeyState(NULL);
		if(keys[SDLK_UP])   { ypos2 -= 4; }
		if(keys[SDLK_DOWN]) { ypos2 += 4; }
		if(keys['w'])		{ ypos -= 4; }
		if(keys['s'])		{ ypos += 4; }

		ballx += ball_vx;
		bally += ball_vy;


		CheckPaddleYCol();
		DrawScene();
	}

	return 0;
}

[draw.cpp]
#include "draw.h"

int xpos=20;
int ypos=220;

int xpos2=735;
int ypos2=220;

int ballx = 370;
int bally = 235;
int ball_vx = 4;
int ball_vy = -4;

SDL_Surface *screen;
SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *ball;

int InitImages()
{
	image = SDL_LoadBMP("paddle.bmp");
	back  = SDL_LoadBMP("bg.bmp");
	ball  = SDL_LoadBMP("ball.bmp");
	return 0;
}

void DrawIMG(SDL_Surface *img, int x, int y)
{
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;
	SDL_BlitSurface(img, NULL, screen, &dest);
}

void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
	SDL_Rect dest;
	SDL_Rect dest2;

	dest.x = x;
	dest.y = y;

	dest2.x  = x2;
	dest2.y  = y2;
	dest2.w  = w;
	dest2.h  = h;

	SDL_BlitSurface(img, &dest2, screen, &dest);
}

void DrawBG()
{
	DrawIMG(back, 0,0);
}

void DrawScene()
{
	DrawIMG(back, xpos-8, ypos-8, 56, 166, xpos-8, ypos-8); //original image is 40x150
	DrawIMG(image, xpos,ypos);
	DrawIMG(back, xpos2-8, ypos2-8, 56, 166, xpos2-8, ypos2-8);
	DrawIMG(image, xpos2,ypos2);
	SDL_SetColorKey(ball, SDL_SRCCOLORKEY,
						SDL_MapRGB(ball->format, 0, -1, 0));
	DrawIMG(back, ballx-8, bally-8, 56,56,ballx-8,ballx-8);
	DrawIMG(ball, ballx, bally);

	SDL_Flip(screen);
}

void CheckPaddleYCol()
{
		if(ypos <= 10)
		{
			ypos = 10;
		}
		else if(ypos >= 440)
		{
			ypos = 440;
		}

		if(ypos2 <= 10)
		{
			ypos2 = 10;
		}
		else if(ypos2 >= 440)
		{
			ypos2 = 440;
		}
}

/*void CheckBallCol()
**{
**	if (ballx <= 200)
**	{
**		ballx -= ball_vx;
**	}
**	if(ballx >= 740)
**	{
**		ballx = 440;					WORK IN PROGRESS (lol)
**	}
**	if(bally <= 10)
**	{
**		bally -= ball_vy;
**	}
**	if(bally >= 440)
**	{
**		bally = 440;
**	}
**}
**
/*

[draw.h]
#ifndef _DRAW_H_
#define _DRAW_H_

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

extern int xpos;
extern int ypos;
extern int xpos2;
extern int ypos2;
extern int ballx;
extern int bally;
extern int ball_vx;
extern int ball_vy;

extern SDL_Surface *screen;
extern SDL_Surface *image;
extern SDL_Surface *back;

int InitImages();
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dest, SDL_Rect *destrect);
void DrawIMG(SDL_Surface *img, int x, int y);
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);
void DrawBG();
void DrawScene();
void CheckPaddleYCol();

#endif

Share this post


Link to post
Share on other sites
Advertisement
The problem is that the "back" image is drawn outside the screen and in that situation it won't be drawn at all.

So you need to check that. The problem occurs when the ball is 8pixels from y = 0 or y = (600-ballsize-8)

so check if the back image is out side the screen and then either change the draw coordinate or size of the rect that is drawn.

/MindWipe

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!