init
This commit is contained in:
commit
a4b37d49e9
|
@ -0,0 +1 @@
|
|||
/cong
|
|
@ -0,0 +1,7 @@
|
|||
SOURCES := cong.c
|
||||
TARGET := cong
|
||||
CFLAGS := -O2 -lraylib
|
||||
|
||||
.PHONY: all
|
||||
all:
|
||||
$(CC) -o $(TARGET) $(SOURCES) $(CFLAGS)
|
|
@ -0,0 +1,142 @@
|
|||
#include <raylib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
} IVector2;
|
||||
|
||||
typedef struct {
|
||||
IVector2 position;
|
||||
IVector2 size;
|
||||
} ITransform;
|
||||
|
||||
typedef struct {
|
||||
ITransform transform;
|
||||
} GameObject;
|
||||
|
||||
#define WIDTH 640
|
||||
#define HEIGHT 480
|
||||
|
||||
void initPlayer(GameObject *player);
|
||||
void initBall(GameObject *ball);
|
||||
void initWall(GameObject *wall);
|
||||
|
||||
int main(void) {
|
||||
GameObject player;
|
||||
GameObject ball;
|
||||
GameObject wall;
|
||||
|
||||
IVector2 ballVel;
|
||||
ballVel.x = 250;
|
||||
ballVel.y = 250;
|
||||
|
||||
int playerSpeed = 250;
|
||||
int score = 0;
|
||||
float dt = 0.0f;
|
||||
char scoreText[2048];
|
||||
char isPaused = 0;
|
||||
|
||||
initPlayer(&player);
|
||||
initBall(&ball);
|
||||
initWall(&wall);
|
||||
|
||||
InitWindow(WIDTH, HEIGHT, "cong");
|
||||
SetWindowMonitor(0);
|
||||
SetTargetFPS(60);
|
||||
|
||||
while (!WindowShouldClose()) {
|
||||
/**
|
||||
* Update
|
||||
*/
|
||||
|
||||
if (IsKeyPressed(KEY_SPACE)) {
|
||||
if (isPaused)
|
||||
isPaused = 0;
|
||||
else
|
||||
isPaused = 1;
|
||||
}
|
||||
|
||||
dt = GetFrameTime();
|
||||
|
||||
if (isPaused == 0) {
|
||||
// Ball physics
|
||||
ball.transform.position.x += ballVel.x * dt;
|
||||
ball.transform.position.y += ballVel.y * dt;
|
||||
|
||||
if (ball.transform.position.y + ball.transform.size.y >= HEIGHT || ball.transform.position.y <= 0) {
|
||||
ballVel.y = -ballVel.y;
|
||||
}
|
||||
|
||||
if (ball.transform.position.x + ball.transform.size.x >= wall.transform.position.x && ballVel.x > 0) {
|
||||
ballVel.x = GetRandomValue(250, 250 + (score * 50));
|
||||
ballVel.x = -ballVel.x;
|
||||
// ballVel.y = GetRandomValue(250, score * 1000);
|
||||
}
|
||||
|
||||
if (ball.transform.position.x < player.transform.position.x + player.transform.size.x && ballVel.x < 0 && ball.transform.position.y + ball.transform.size.y > player.transform.position.y && ball.transform.position.y < player.transform.position.y + player.transform.size.y) {
|
||||
ballVel.x = -ballVel.x;
|
||||
if (ballVel.x < 250) ballVel.x = 250;
|
||||
score++;
|
||||
}
|
||||
|
||||
if (ball.transform.position.x + ball.transform.size.x < 0) {
|
||||
ball.transform.position.x = (WIDTH / 2) - (ball.transform.size.x / 2);
|
||||
ballVel.x = -ballVel.x;
|
||||
score = 0;
|
||||
}
|
||||
|
||||
// Player movement
|
||||
if (IsKeyDown(KEY_W) && player.transform.position.y > 0) {
|
||||
player.transform.position.y -= playerSpeed * dt;
|
||||
} else if (IsKeyDown(KEY_S) && player.transform.position.y + player.transform.size.y < HEIGHT) {
|
||||
player.transform.position.y += playerSpeed * dt;
|
||||
}
|
||||
}
|
||||
|
||||
// Score text
|
||||
sprintf(scoreText, "%d", score);
|
||||
|
||||
/**
|
||||
* Draw
|
||||
*/
|
||||
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
DrawRectangle(player.transform.position.x, player.transform.position.y, player.transform.size.x, player.transform.size.y, BLACK);
|
||||
DrawRectangle(ball.transform.position.x, ball.transform.position.y, ball.transform.size.x, ball.transform.size.y, BLACK);
|
||||
DrawRectangle(wall.transform.position.x, wall.transform.position.y, wall.transform.size.x, wall.transform.size.y, BLACK);
|
||||
DrawText(scoreText, 50, 10, 50, BLACK);
|
||||
if (isPaused) DrawText("PAUSED", 10, HEIGHT - 50, 50, BLACK);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void initPlayer(GameObject *player) {
|
||||
player->transform.position.x = 10;
|
||||
player->transform.position.y = 10;
|
||||
player->transform.size.x = 16;
|
||||
player->transform.size.y = 96;
|
||||
}
|
||||
|
||||
void initBall(GameObject *ball) {
|
||||
ball->transform.size.x = 16;
|
||||
ball->transform.size.y = 16;
|
||||
ball->transform.position.x = (WIDTH / 2) - (ball->transform.size.x / 2);
|
||||
ball->transform.position.y = (HEIGHT / 2) - (ball->transform.size.y / 2);
|
||||
}
|
||||
|
||||
void initWall(GameObject *wall) {
|
||||
wall->transform.size.x = 16;
|
||||
wall->transform.size.y = HEIGHT;
|
||||
wall->transform.position.x = WIDTH - wall->transform.size.x - 10;
|
||||
wall->transform.position.y = 0;
|
||||
}
|
Loading…
Reference in New Issue