Skip to content

Commit

Permalink
Cleaning up, added explosion assets
Browse files Browse the repository at this point in the history
  • Loading branch information
carrierdown committed Feb 18, 2020
1 parent b2917d5 commit 8c05680
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 74 deletions.
Binary file added eship.pcx
Binary file not shown.
Binary file added exp01.pcx
Binary file not shown.
Binary file added exp02.pcx
Binary file not shown.
Binary file added exp03.pcx
Binary file not shown.
Binary file added exp04.pcx
Binary file not shown.
187 changes: 113 additions & 74 deletions shooter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,56 @@

const short TRUE = 1;
const short FALSE = 0;
short quit = FALSE;
const short SHOT_DELAY = 10;
const short SHIP_WIDTH = 16;
const short SHIP_HEIGHT = 32;
const short SHIP_ANIMATION_TRESHOLD = 2;

short smallestNumber(short a, short b);
short largestNunber(short a, short b);
short largestNumber(short a, short b);
void loadAssets();
void initBullets();
void initVgaGraphicsMode();
void initMouse();
short checkShotFired(short shotDelay, short curMouseX, short curMouseY);
short updateBullets(short shotDelay, short shotVelocity);
void clearShip(short curMouseX, short curMouseY, signed short deltaX, signed short deltaY);

struct Bullet {
short x;
short y;
};

byte ship[5][512];
byte bullet[12];
signed char palette[768];
Bullet bullets[20];

int main(int argc, char *argv[])
{
static byte ship[5][512];
static byte bullet[12];
static signed char palette[768];
Bullet bullets[20];

for (short i = 0; i < 20; i++) {
bullets[i].x = 0;
bullets[i].y = 0;
}

short key = 0;

pcxRead("bullets.pcx", bullet, palette, 4, 3);
pcxRead("ship_ll.pcx", ship[0], palette, 16, 32);
pcxRead("ship_l.pcx", ship[1], palette, 16, 32);
pcxRead("ship.pcx", ship[2], palette, 16, 32);
pcxRead("ship_r.pcx", ship[3], palette, 16, 32);
pcxRead("ship_rr.pcx", ship[4], palette, 16, 32);

mode_13h();
set_pal(palette);
set_mouse_sens(20,16);
xlimit_mouse(8,300);
ylimit_mouse(167,167);
get_mouse_status();
initBullets();
loadAssets();
initVgaGraphicsMode();
initMouse();

short curMouseX = MOUSE_X, curMouseY = MOUSE_Y;
signed short deltaX = 0, deltaY = 0;
short moveWithAnimTreshold = 2;
unsigned short deltaX = 0, deltaY = 0;
short targetState = 2;
short curState = 2;
short freezeFrames = 0;

short SHOT_DELAY = 10;
short shotDelay = 0;
short shotVelocity = 4;

const short SHIP_WIDTH = 16;
const short SHIP_HEIGHT = 32;

short xOffset = 0;
short yOffset = 0;
short quit = FALSE;

while (quit == FALSE) {
if (shotDelay == 0 && BUTTON_STATE == 1)
{
short freeBulletSlot = -1;
for (short i = 0; i < 20; i++) {
if (bullets[i].y <= 0) {
freeBulletSlot = i;
break;
}
}
if (freeBulletSlot >= 0) {
bullets[freeBulletSlot].x = curMouseX + 6;
bullets[freeBulletSlot].y = curMouseY - 4;
shotDelay = SHOT_DELAY;
}
}
// update bullets
if (shotDelay > 0) shotDelay--;
for (short i = 0; i < 20; i++) {
if (bullets[i].y > 0) {
draw_box(bullets[i].x, bullets[i].y, 4, 3, 0);
bullets[i].y -= shotVelocity;
}
if (bullets[i].y > 0) {
put_block(bullets[i].x, bullets[i].y, 4, 3, bullet);
}
}
short key;

shotDelay = checkShotFired(shotDelay, curMouseX, curMouseY);
shotDelay = updateBullets(shotDelay, shotVelocity);

put_block(curMouseX, curMouseY, SHIP_WIDTH, SHIP_HEIGHT, ship[curState]);

Expand All @@ -99,25 +67,13 @@ int main(int argc, char *argv[])
deltaX = MOUSE_X - curMouseX;
deltaY = MOUSE_Y - curMouseY;

if (deltaX < 0) {
xOffset = SHIP_WIDTH;
} else {
xOffset = 0;
}
if (deltaY < 0) {
yOffset = SHIP_HEIGHT;
} else {
yOffset = 0;
}

draw_box(smallestNumber(curMouseX, MOUSE_X) + xOffset, curMouseY, abs(deltaX) + 1, SHIP_HEIGHT, 0);
draw_box(curMouseX, smallestNumber(curMouseY, MOUSE_Y) + yOffset, SHIP_WIDTH, abs(deltaY) + 1, 0);
clearShip(curMouseX, curMouseY, deltaX, deltaY);

curMouseX = MOUSE_X;
curMouseY = MOUSE_Y;

if (freezeFrames == 0) {
if (abs(deltaX) >= moveWithAnimTreshold) {
if (abs(deltaX) >= SHIP_ANIMATION_TRESHOLD) {
targetState = deltaX > 0 ? 0 : 4;
} else {
targetState = 2;
Expand Down Expand Up @@ -148,6 +104,89 @@ short smallestNumber(short a, short b) {
return a < b ? a : b;
}

short largestNunber(short a, short b) {
short largestNumber(short a, short b) {
return a > b ? a : b;
}

void loadAssets()
{
pcxRead("bullets.pcx", bullet, palette, 4, 3);
pcxRead("ship_ll.pcx", ship[0], palette, 16, 32);
pcxRead("ship_l.pcx", ship[1], palette, 16, 32);
pcxRead("ship.pcx", ship[2], palette, 16, 32);
pcxRead("ship_r.pcx", ship[3], palette, 16, 32);
pcxRead("ship_rr.pcx", ship[4], palette, 16, 32);
}

void initBullets()
{
for (short i = 0; i < 20; i++) {
bullets[i].x = 0;
bullets[i].y = 0;
}
}

void initVgaGraphicsMode()
{
mode_13h();
set_pal(palette);
}

void initMouse()
{
set_mouse_sens(20,16);
xlimit_mouse(8,300);
ylimit_mouse(167,167);
get_mouse_status();
}

short checkShotFired(short shotDelay, short curMouseX, short curMouseY)
{
if (shotDelay == 0 && BUTTON_STATE == 1)
{
short freeBulletSlot = -1;
for (short i = 0; i < 20; i++) {
if (bullets[i].y <= 0) {
freeBulletSlot = i;
break;
}
}
if (freeBulletSlot >= 0) {
bullets[freeBulletSlot].x = curMouseX + 6;
bullets[freeBulletSlot].y = curMouseY - 4;
shotDelay = SHOT_DELAY;
}
}
return shotDelay;
}

short updateBullets(short shotDelay, short shotVelocity)
{
if (shotDelay > 0) shotDelay--;
for (short i = 0; i < 20; i++) {
if (bullets[i].y > 0) {
draw_box(bullets[i].x, bullets[i].y, 4, 3, 0);
bullets[i].y -= shotVelocity;
}
if (bullets[i].y > 0) {
put_block(bullets[i].x, bullets[i].y, 4, 3, bullet);
}
}
return shotDelay;
}

void clearShip(short curMouseX, short curMouseY, signed short deltaX, signed short deltaY)
{
short xOffset = 0,
yOffset = 0;

if (deltaX < 0) {
xOffset = SHIP_WIDTH;
}
if (deltaY < 0) {
yOffset = SHIP_HEIGHT;
}

draw_box(smallestNumber(curMouseX, MOUSE_X) + xOffset, curMouseY, abs(deltaX) + 1, SHIP_HEIGHT, 0);
draw_box(curMouseX, smallestNumber(curMouseY, MOUSE_Y) + yOffset, SHIP_WIDTH, abs(deltaY) + 1, 0);
}

0 comments on commit 8c05680

Please sign in to comment.