Pradana AUMARS
e10599f19a
* Movement of ball and rackets are correct * Score is recorded and displayed on terminal, not on the window * Ball is represented as a square and not a disc * Window is fixed and not scalable * Player controls are E-D (up-down) for Left, I-K (up-down) for Right * Ball reset parameters are fixed (except for direction)
55 lines
1.3 KiB
C
55 lines
1.3 KiB
C
#include "racket.h"
|
|
|
|
Racket*
|
|
Racket_init(int screen_width, int screen_height, int posX, int posY, SDL_Keycode up, SDL_Keycode down)
|
|
{
|
|
Racket *r = malloc(sizeof(Racket));
|
|
*(int *)&r->RACKET_WIDTH = 20;
|
|
*(int *)&r->RACKET_HEIGHT = 100;
|
|
*(int *)&r->RACKET_VELOCITY = 10;
|
|
*(int *)&r->SCREEN_WIDTH = screen_width;
|
|
*(int *)&r->SCREEN_HEIGHT = screen_height;
|
|
*(int *)&r->posX = posX;
|
|
r->posY = posY;
|
|
*(int *)&r->vel = 5;
|
|
r->score = 0;
|
|
*(SDL_Keycode *)&r->up = up;
|
|
*(SDL_Keycode *)&r->down = down;
|
|
r->updown = 0;
|
|
return r;
|
|
}
|
|
|
|
void
|
|
Racket_free(Racket *r)
|
|
{
|
|
free(r);
|
|
}
|
|
|
|
int
|
|
Racket_render(const Racket *r, SDL_Renderer *renderer) {
|
|
SDL_Rect rect = { r->posX, r->posY, r->RACKET_WIDTH, r->RACKET_HEIGHT };
|
|
if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255) < 0) {
|
|
printf("SDL_SetRenderDrawColor failed! SDL_Error: %s\n", SDL_GetError());
|
|
return 1;
|
|
}
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
Racket_handle_event(Racket *r, const Uint8 *keystate) {
|
|
if (keystate[r->up]) r->updown = -1;
|
|
else if (keystate[r->down]) r->updown = 1;
|
|
else r->updown = 0;
|
|
}
|
|
|
|
void
|
|
Racket_move(Racket *r) {
|
|
r->posY += r->updown * r->vel;
|
|
if ((r->posY < 0) || (r->posY + r->RACKET_HEIGHT > r->SCREEN_HEIGHT))
|
|
{
|
|
r->posY -= r->updown * r->vel;
|
|
}
|
|
r->updown = 0;
|
|
}
|