1
Name:
Anonymous
2011-02-12 18:16
//compile with:
//gcc -std=gnu99 name_of_file.c -lncurses
//this kind of shows how similar JAVA and C are. I wrote this in 4.5 hours without ever reading any tutorial whatsoever;
//i.e. learning through trial and error and stackoverflow (because of ncurses)
//Controls: WASD
#include <stdio.h>
#include <ncurses.h>
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
void shift(int pos[25][2], int dir);
int main()
{
int h = 0, k = 0, l = 0, x = 5, y = 5, pos[25][2] = {{9,12}, {9,13}, {9,14}, {9,15}, {9,16}, {9,17}, {9,18}, {9,19}, {9,20}, {9,21}, {9,22}, {9,23}, {9,24}, {9,25}, {9,26}, {9,27}, {9,28}, {9,29}, {9,30}, {9,31}, {9,32}, {9,33}, {9,34}, {9,35}, {9,36}};
char j = ' ';
initscr();
cbreak();
noecho();
timeout(0);
while(j != 'q'){
//highscore
k++;
if (k == 7){
k = 0;
h++;
}
erase();
move(0,0);
printw("****************************************");
move(20,0);
printw("*****************************************");
int i = 0;
for(;i<20;i++){
mvaddch(i,0,'*');
mvaddch(i,40,'*');
}
//mine
l++;
if (l > 10){
l = 0;
if ((x > pos[0][1]) && (x>1))
x--;
else if (x < 39)
x++;
if ((y > pos[0][0]) && (y>1))
y--;
else if (y < 19)
y++;
}
mvaddch(y,x,'X');
//snake
i = 0;
for(; i<20; i++){
if (pos[i][0] == 0)
break;
else
mvaddch(pos[i][0], pos[i][1], 'o');
}
refresh();
j = getch();
if ((j == 'a') || (j == 's') || (j == 'd') || (j == 'w'))
switch(j){
case 'a': if (pos[0][1]<=pos[1][1]) shift(pos, 1); break;
case 'd': if (pos[0][1]>=pos[1][1]) shift(pos, 2); break;
case 's': if (pos[0][0]>=pos[1][0]) shift(pos, 4); break;
case 'w': if (pos[0][0]<=pos[1][0]) shift(pos, 3);
}
else
if ((pos[0][1]==pos[1][1]) && (pos[0][0]>=pos[1][0]))
shift(pos, 4);
else if ((pos[0][1]==pos[1][1]) && (pos[0][0]<=pos[1][0]))
shift(pos, 3);
else if ((pos[0][1]>=pos[1][1]) && (pos[0][0]==pos[1][0]))
shift(pos, 2);
else
shift(pos, 1);
bool yes = false;
for(int i = 1; i < 25; i++)
if(((pos[0][1]==pos[i][1])&&(pos[0][0]==pos[i][0]))||((x==pos[i][1])&&(y==pos[i][0]))){
yes = true;
break;
}
//lose
if ((pos[0][0] < 1 )||( pos[0][0] > 19)||( pos[0][1] < 1)||( pos[0][1] > 39)||yes){
endwin();
j = 'q';
}
for(int i = 0; i < 2000; i++)
getch();
usleep(210000);
}
endwin();
printf("Highscore: %d \n", h);
return 0;
}
void shift(int pos[20][2], int dir){
for(int i = 19; i>0; i--){
if (pos[i][0] != 0){
pos[i][0] = pos[i-1][0];
pos[i][1] = pos[i-1][1];
}
}
switch(dir){
case 1: //left
--pos[0][1];
break;
case 2: //right
++pos[0][1];
break;
case 3: //up
--pos[0][0];
break;
case 4: //down
++pos[0][0];
}
}
4
Name:
VIPPER
2011-02-12 18:42
Cool story bro.
Go back to the imageboards.
5
Name:
Anonymous
2011-02-12 19:26
curs_set(0) will hide cursor
movement keys are still hjkl
your timing should be rewritten like this:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define MAX_FPS 3
#define MSECS_PER_FRAME (1000000/MAX_FPS)
int main()
{
struct timeval last_time, current_time;
long utime, seconds, useconds;
do {
gettimeofday( &last_time, NULL);
usleep(58);
gettimeofday( ¤t_time, NULL);
seconds = current_time.tv_sec - last_time.tv_sec;
useconds = current_time.tv_usec - last_time.tv_usec;
utime = seconds * 1000000 + useconds + 0.5;
printf("%ld\n", utime);
if ( utime < MSECS_PER_FRAME )
usleep(MSECS_PER_FRAME - utime);
} while (1);
}
6
Name:
Anonymous
2011-02-12 20:05
>>4
fuck off you cock sucking autistic vipper
7
Name:
Anonymous
2011-02-12 20:42
I COMPILED AND EXECUTED THIS CODE AND IT GAVE ME A VIRUS!! BE CAREFUL, PEOPLE!!
8
Name:
Anonymous
2011-02-12 23:09
You should make an enumeration for the keys or define left, right up and down as macros.
9
Name:
Anonymous
2011-02-13 17:53
I wrote this in 4.5 hours