Skip to content

Instantly share code, notes, and snippets.

@Gi133
Created December 1, 2011 16:13
Show Gist options
  • Select an option

  • Save Gi133/1417877 to your computer and use it in GitHub Desktop.

Select an option

Save Gi133/1417877 to your computer and use it in GitHub Desktop.
Sonic Prototype for Computers and Graphics Architecture class
/*
TO DO:
Maybe change map
It currently fits nicely in assignment requirements as moving around is a game mechanic.
*/
#include <stdint.h>
#include <stdlib.h>
#include "gba.h"
int main()
{
REG_DISPCNT = MODE4 | BG2_ENABLE;
int ground[239][20];
int player[20][16];
const int line=239;
int PLAYER_POSITION=0;
int PLAYER_SPEED=0;
int SPEED_MODIFIER=0;
int GROUND_SPEED=0;
int JUMP_HEIGHT=0;
const int MAX_HEIGHT=20;
bool REACHED_MAX=0;
SetPaletteBG(1, RGB(31, 31, 31));
SetPaletteBG(2, RGB(0, 0, 0));
SetPaletteBG(3, RGB(31,16,0));
//Assign values inside the arrays//
//Ground Array
for (int row=0; row<=22; row++)
{
for (int cell=0; cell<=line; cell++)
{
if (cell %4==0)
{
ground[row][cell]=2;
}
else
{
ground [row][cell]=1;
}
}
}
//Player Array
for (int row=0; row<=20; row++) //Drawing the main body
{
for (int cell=4; cell<=8; cell++)
{
player[row][cell]=1;
}
}
for (int row=0; row<=4; row++) //Drawing the arms
{
for (int cell=0; cell<=10; cell++)
{
player[16-row][1+cell]=1;
}
}
player[18][7]=2; //Eye
while(1)
{
//Player Controls
if ((REG_P1 & KEY_RIGHT) == 0)
{
PLAYER_POSITION++;
PLAYER_SPEED++;
if ((REG_P1 & KEY_UP) == 0)
{
if ((JUMP_HEIGHT<MAX_HEIGHT) && (REACHED_MAX ==0))
{
JUMP_HEIGHT+=10;
}
else if (JUMP_HEIGHT>MAX_HEIGHT)
{
JUMP_HEIGHT=MAX_HEIGHT;
REACHED_MAX=1;
}
}
if (PLAYER_SPEED<0)
{
PLAYER_SPEED+=20;
}
}
else if ((REG_P1 & KEY_LEFT) == 0)
{
PLAYER_POSITION--;
PLAYER_SPEED--;
if ((REG_P1 & KEY_UP) == 0)
{
if ((JUMP_HEIGHT<MAX_HEIGHT) && (REACHED_MAX ==0))
{
JUMP_HEIGHT+=10;
}
else if (JUMP_HEIGHT>MAX_HEIGHT)
{
JUMP_HEIGHT=MAX_HEIGHT;
}
}
if (PLAYER_SPEED>0)
{
PLAYER_SPEED-=20;
}
}
else if ((REG_P1 & KEY_UP) == 0)
{
if (JUMP_HEIGHT<MAX_HEIGHT)
{
JUMP_HEIGHT+=10;
}
else if (JUMP_HEIGHT>MAX_HEIGHT)
{
JUMP_HEIGHT=MAX_HEIGHT;
}
}
else
{
if (PLAYER_SPEED>0)
{
PLAYER_SPEED-=10;
}
else if (PLAYER_SPEED<0)
{
PLAYER_SPEED+=10;
}
else
{
PLAYER_SPEED=0;
}
}
if (JUMP_HEIGHT>0)
{
JUMP_HEIGHT-=4;
}
SPEED_MODIFIER=PLAYER_SPEED/12;
if (SPEED_MODIFIER>30)
{
SPEED_MODIFIER=30;
}
//draw pixels on GBA screen
ClearScreen8(2);
for (int row=0; row<=22; row++)
{
for (int cell=0; cell<=line; cell++)
{
PlotPixel8(cell-GROUND_SPEED, (138+row), ground[row][cell]);
}
}
for (int i=0; i<=239; i++)
{
PlotPixel8(i, 138, 3); //Brown/Orange strip of dirt
//Black to address the wrap-around problem. Just cover the array with black.
PlotPixel8(i, 137, 2);
PlotPixel8(i, 136, 2);
}
PLAYER_POSITION=PLAYER_POSITION+SPEED_MODIFIER;
for (int row=0; row<=20; row++)
{
for (int cell=0; cell<=12; cell++)
{
PlotPixel8(PLAYER_POSITION+cell, 137-row - JUMP_HEIGHT, player[row][cell]);
}
}
if(PLAYER_SPEED>60)
{
GROUND_SPEED+=10;
if (GROUND_SPEED>200)
{
GROUND_SPEED=0;
}
}
if ((PLAYER_POSITION+1)>235)
{
PLAYER_POSITION=0;
}
if (PLAYER_POSITION<=0)
{
PLAYER_POSITION=0;
}
if (JUMP_HEIGHT == 0)
{
REACHED_MAX = 0;
}
//In the freak case player get's drawn one line below
if (JUMP_HEIGHT < 0)
{
JUMP_HEIGHT = 0;
}
WaitVSync();
FlipBuffers();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment