Language: C Project Overview: Computer games are excellent way to stretch your p
ID: 3842996 • Letter: L
Question
Language: C
Project Overview: Computer games are excellent way to stretch your programming ability, and practice both problem solving and developing your own solutions and algorithms. The aim of this project is to recreate an absolute arcade classic released by Atari in 1980 - Centipede!
Centipede (see Figure 1) is a vertically oriented shooter by Ed Logg and Dona Bailey. Designed intentionally to engage female players, the game consists of a centipede that winds it’s way from top to bottom of the screen (where the player is located). The screen is populated with some number of mushrooms. When the centipede hits a mushroom, it drops one row towards the player and changes direction. The more mushrooms, the faster the centipede typically descends towards the player. The player can fire and must shoot the centipede. The shot segment becomes a mushroom. If the player shoots a segment other than the head, the centipede splits into two, effectively gaining a new head and both descend towards the player. Mushrooms can be destroyed, but take four shots to destroy. The arcade version of the game also features fleas, spiders and scorpions. See the Wikipedia description for more detailed discussion of the gameplay.
If you are unfamiliar with the game, watch the following youtube video demo: https://www.youtube.com/watch?v=dxoK8hosHjA, and read this overview: https://en.wikipedia.org/wiki/Centipede_(video_game)
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#define W 320
#define H 256
#define M 255
uint8_t r[H][W], g[H][W], b[H][W];
void
create_image ( void )
{
for ( int x=0; x<W; x++ )
{
int y = (H/2) +
(H/2) * sin ( 2 * 3.141593 * (x/(double)W) );
r[y][x] = 255, g[y][x] = 255, b[y][x] = 255;
}
}
void
write_ppm ( const char *filename )
{
FILE *out = fopen ( filename, "w" );
fprintf ( out, "P6 %d %d %d ", W, H, M );
for ( size_t i=0; i<H; i++ )
for ( size_t j=0; j<W; j++ )
{
fwrite ( &r[i][j], sizeof(uint8_t), 1, out );
fwrite ( &g[i][j], sizeof(uint8_t), 1, out );
fwrite ( &b[i][j], sizeof(uint8_t), 1, out );
}
fclose ( out );
}
int
main ()
{
create_image();
write_ppm ( "sine.ppm" );
exit ( EXIT_SUCCESS );
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.