All pastes #443957 Raw Edit

Asteroids

public c v1 · immutable
·published 2007-04-17 03:22 UTC
rendered paste body
// Includes#include <PA9.h>#include <math.h>// PAGfxConverter Include#include "gfx/all_gfx.c"#include "gfx/all_gfx.h"#define MAXMOVE 25#define UNIT 256// Screen borders#define BORDERLEFT -31*UNIT #define BORDERRIGHT 265*UNIT#define BORDERTOP -31*UNIT#define BORDERBOTTOM 200*UNITtypedef enum{   MT_BULLET,   MT_LARGEASTEROID,   MT_MEDIUMASTEROID,   MT_SMALLASTEROID,}type_t;typedef struct mobj_s{   struct mobj_s* prev;   struct mobj_s* next;   s32 x;   s32 y;   u16 angle;   s32 momx;   s32 momy;   s32 radius;      type_t* type;}mobj_t;typedef struct{	int score; // Player' score.	mobj_t* mobj;}player_t;mobj_t* mobjhead;void PlayerThink(player_t* player);void MovePlayer(player_t* player);void DebugInfo(void);void MoveMobj(mobj_t* mobj);void ShootBullet(player_t* player);void InitPlayer(player_t* player);void AddObject(mobj_t *node);int main(void){	player_t* player;	PA_Init();	PA_InitVBL();		PA_InitText(1,0); // On the top screen	PA_LoadSpritePal(0, 1, (void*)vaisseau_Pal);	PA_LoadSpritePal(0, 0, (void*)bullet_Pal);	PA_CreateSprite(0, 1,(void*)vaisseau_Sprite, OBJ_SIZE_32X32,1, 1, 128-16, 96-16); // Create the ship in the center...	PA_SetSpriteRotEnable(0,1,0);// Enable rotations and use Rotset 0... 		while(1)	{	   mobj_t* node;	   	   InitPlayer(player);	   PlayerThink(player);		MovePlayer(player);			DebugInfo();				for(node = mobjhead; node; node = node->next)			MoveMobj(node);					PA_WaitForVBL();	}	return 0;}void InitPlayer(player_t* player){   malloc (sizeof(player_t));   player->mobj = malloc(sizeof(mobj_t));   AddObject(player->mobj);   	player->mobj->x = 128*UNIT; // ship x position in 8bit fixed point	player->mobj->y = 96*UNIT; // Y	player->mobj->momx = 0;	// X momentum	player->mobj->momy = 0;	// Y momentum	player->mobj->angle = 0; // direction in which to move !	player->score = 0; // Player' score.   }void PlayerThink(player_t* player){   if(Pad.Newpress.A)   	ShootBullet(player);}void MovePlayer(player_t* player){   s32 newx = 0, newy = 0, dist = 0;   s32 x, y, momx, momy;   u16 moveangle = 0, angle = 0;      x = player->mobj->x;   y = player->mobj->y;   momx = player->mobj->momx;   momy = player->mobj->momy;   angle = player->mobj->angle;      if (Stylus.Held) // Move forward		{ 			angle = PA_GetAngle(x>>8, y>>8, Stylus.X, Stylus.Y);			PA_SetRotsetNoZoom(0, 0, angle); // Turn the ship in the correct direction						// Divide it so it's not instant acceleration			momx += PA_Cos(angle)/40;			momy -= PA_Sin(angle)/40;		}		newx = x + momx;		newy = y + momy;				if(newx>x && newy>y)			dist = sqrt((((newx-x)^2)+((newy-y)^2)));		else if(newx>x && newy<y)			dist = sqrt((((newx-x)^2)+((y-newy)^2)));		else if(newx<x && newy>y)			dist = sqrt((((x-newx)^2)+((newy-y)^2)));		else if(newx<x && newy<y)			dist = sqrt((((x-newx)^2)+((y-newy)^2)));					if(dist > MAXMOVE)		{		   moveangle = PA_GetAngle(newx>>8, newy>>8, x>>8, y>>8);		   momx += PA_Cos(moveangle)/40;		   momy -= PA_Sin(moveangle)/40;		   x += momx;		   y += momy;		   			dist = MAXMOVE;		}							else		{			x = newx;			y = newy;			}						// Teleport to the other side of the map.				if(x > BORDERRIGHT)			x = BORDERLEFT;		else if(x < BORDERLEFT)			x = BORDERRIGHT;					if(y < BORDERTOP)			y = BORDERBOTTOM;		else if(y > BORDERBOTTOM)			y = BORDERTOP;				player->mobj->x = x;		player->mobj->y = y;		player->mobj->momx = momx;		player->mobj->momy = momy;		player->mobj->angle = angle;				PA_SetSpriteXY(0, 1, (x>>8)-16, (y>>8)-16); // Sprite position converted to normal...}void DebugInfo(void){ /*  	PA_OutputText(1, 5, 10, "Angle : %d  ", angle);		PA_OutputText(1, 5, 11, "momx: %d ", momx);		PA_OutputText(1, 5, 12, "momy: %d ", momy);		PA_OutputText(1, 5, 13, "x: %d ", (x>>8)-16);		PA_OutputText(1, 5, 14, "y: %d ", (y>>8)-16);*/}void ShootBullet(player_t* player){   PA_CreateSprite(0, 0,(void*)bullet_Sprite, OBJ_SIZE_8X8,1, 0, 64-16, 64-16);   /*	mobj->x = player->mo->x;	mobj->y = player->mo->y;	mobj->angle = player->mo->angle;	mobj->radius = 1;		mobj->momx = PA_Cos(mobj->angle);	mobj->momy = PA_Sin(mobj->angle);	mobj->type = MT_BULLET;	*/}void MoveMobj(mobj_t* mobj){				mobj->x += PA_Cos(mobj->angle)/-mobj->momx;	mobj->y -= PA_Sin(mobj->angle)/-mobj->momy;	// Teleport to the other side of the map.			if(mobj->x > BORDERRIGHT)		mobj->x = BORDERLEFT;	else if(mobj->x < BORDERLEFT)		mobj->x = BORDERRIGHT;			if(mobj->y < BORDERTOP)		mobj->y = BORDERBOTTOM;	else if(mobj->y > BORDERBOTTOM)		mobj->y = BORDERTOP;		PA_SetSpriteXY(0, 0, (mobj->x>>8)-16, (mobj->y>>8)-16); // Sprite position converted to normal...  }void AddObject(mobj_t *node){ 	if (mobjhead == NULL) 	{ 		mobjhead = node; 		(mobjhead)->prev = (mobjhead)->next = NULL; 	} 	else 	{ 		mobj_t *tail;		tail = mobjhead; 		while (tail->next != NULL) 			tail = tail->next; 		tail->next = node; 		tail->next->prev = tail;		node->next = NULL;	}}