Lol, here's the code:
[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;
if(bally <= 10)
{
ball_vy = -ball_vy;
}
if(ballx >= 747)
{
ball_vx = -ball_vx;
}
if(bally >= 551)
{
ball_vy = -ball_vy;
}
if(ballx <= 10)
{
ball_vx = -ball_vx;
}
if(ballx <= xpos) //WHY DOESNT THIS WORK!?
{
ball_vx = -ball_vx;
}
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,bally-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;
}
}
[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();
void CheckBallCol();
#endif
i cant make the ball bounce off the paddles...any help? thanks!
*oh and i know this wont matter when my paddle-to-ball detection works but when the ball moves over the paddle (the ball is a BMP image as you can see) i can see blue space in the shape of a square around the ball. does anybody know why? it should be transparent from the SDL_SetColorKey()
//-----------------------------------------One short sleepe past, wee wake eternally, And Death shall be no more, Death thou shalt die.