Commit 9966591d authored by Max Kobelt's avatar Max Kobelt
Browse files

Draw basic aliens

parent 09509a4d
...@@ -3,13 +3,13 @@ target_link_libraries(${PROJECT_NAME} raylib) ...@@ -3,13 +3,13 @@ target_link_libraries(${PROJECT_NAME} raylib)
target_sources(${PROJECT_NAME} target_sources(${PROJECT_NAME}
PRIVATE PRIVATE
main.c main.c
menu.h menu.h
menu.c menu.c
game.h game.h
game.c game.c
position.h alien.h
alien.h alien.c
projectile.h projectile.h
projectile.c projectile.c
) )
#include "alien.h"
#include <stdlib.h>
#define ALIEN_SIZE 8
#define ALIEN_OFFSET 2
extern const int GAME_SCREEN_WIDTH;
Aliens spawnAliens() {
Aliens aliens = malloc(sizeof *aliens * ALIEN_ROWS);
for (int i = 0; i < ALIEN_ROWS; ++i) {
aliens[i] = malloc(sizeof **aliens * ALIEN_COLS);
for (int j = 0; j < ALIEN_COLS; ++j) {
aliens[i][j] = malloc(sizeof ***aliens);
Alien *alien = aliens[i][j];
alien->x = j * (ALIEN_SIZE + ALIEN_OFFSET);
alien->y = i * (ALIEN_SIZE + ALIEN_OFFSET);
}
}
return aliens;
}
void drawAliens(Aliens aliens) {
for (int i = 0; i < ALIEN_ROWS; ++i) {
for (int j = 0; j < ALIEN_COLS; ++j) {
Alien *alien = aliens[i][j];
if (alien) {
DrawRectangle(alien->x, alien->y, ALIEN_SIZE, ALIEN_SIZE, WHITE);
}
}
}
}
#pragma once #pragma once
#include "position.h" #include "raylib.h"
typedef struct { #define ALIEN_ROWS 3
Position pos; #define ALIEN_COLS 5
} Alien;
typedef Alien** Aliens; typedef Vector2 Alien;
typedef Alien*** Aliens;
Aliens spawnAliens();
void drawAliens();
#include "game.h" #include "game.h"
#include "projectile.h" #include "projectile.h"
#include "alien.h"
#include "raylib.h" #include "raylib.h"
...@@ -12,13 +13,14 @@ ...@@ -12,13 +13,14 @@
extern const int GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT, FRAME_RATE; extern const int GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT, FRAME_RATE;
Projectile *playerProj; Projectile *playerProj;
Aliens aliens;
Rectangle laserCanon; Rectangle laserCanon;
Rectangle gameBorder; Rectangle gameBorder;
Vector2 laserCanonPos; Vector2 laserCanonPos;
int laserCanonSize; int laserCanonSize;
void initGame() { void initGame() {
aliens = spawnAliens();
laserCanonSize = GAME_SCREEN_WIDTH / 20; laserCanonSize = GAME_SCREEN_WIDTH / 20;
laserCanonPos = (Vector2) {(float) GAME_SCREEN_WIDTH / 2, (float) GAME_SCREEN_HEIGHT - (laserCanonSize * 2)}; laserCanonPos = (Vector2) {(float) GAME_SCREEN_WIDTH / 2, (float) GAME_SCREEN_HEIGHT - (laserCanonSize * 2)};
laserCanon = (Rectangle) {(float) laserCanonPos.x, (float) laserCanonPos.y, (float) laserCanonSize, (float) laserCanonSize}; laserCanon = (Rectangle) {(float) laserCanonPos.x, (float) laserCanonPos.y, (float) laserCanonSize, (float) laserCanonSize};
...@@ -53,6 +55,7 @@ void drawGame() { ...@@ -53,6 +55,7 @@ void drawGame() {
if (playerProj) { if (playerProj) {
DrawRectangleRec((Rectangle) { playerProj->pos.x, playerProj->pos.y, PROJECTILE_WIDTH, PROJECTILE_HEIGHT }, WHITE); DrawRectangleRec((Rectangle) { playerProj->pos.x, playerProj->pos.y, PROJECTILE_WIDTH, PROJECTILE_HEIGHT }, WHITE);
} }
drawAliens(aliens);
} }
void spawnPlayerProjectile() { void spawnPlayerProjectile() {
......
#pragma once
#include <stddef.h>
typedef struct {
size_t x, y;
} Position;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment