Base game
This commit is contained in:
parent
e227801b7e
commit
2e6ec34afb
1 changed files with 186 additions and 0 deletions
186
main.cpp
Normal file
186
main.cpp
Normal file
|
@ -0,0 +1,186 @@
|
|||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <curses.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
using namespace std;
|
||||
|
||||
#define termMaxX 145
|
||||
#define termMaxY 36
|
||||
#define upArrow 65
|
||||
#define downArrow 66
|
||||
#define rightArrow 67
|
||||
#define leftArrow 68
|
||||
|
||||
|
||||
void drawVoid(){
|
||||
|
||||
printf("\033[1;32m\033[40m \033[0m"); //\033[1;32m green text,\033[40m black bg, display " ",\033[0m set default
|
||||
}
|
||||
void drawSnake(){
|
||||
printf("\033[1;32m\033[40m*\033[0m"); //\033[1;32m green text,\033[40m black bg, display *,\033[0m set default
|
||||
}
|
||||
void SetCursorPosition(int XPos, int YPos) {
|
||||
printf("\033[%d;%dH",YPos+1,XPos+1);
|
||||
}
|
||||
|
||||
void moveSnake(int deltaX, int deltaY, int posX[100], int posY[100], int tempX[2], int tempY[2]){
|
||||
|
||||
tempX[0]=posX[0]+deltaX;
|
||||
tempY[0]=posY[0]+deltaY;
|
||||
bool sw=0;
|
||||
for(int i=0;(posX[i]!=-1)&&(posY[i]!=-1);i++){
|
||||
if((posX[i+1]==-1)&&(posY[i+1]==-1)){
|
||||
SetCursorPosition(posX[i],posY[i]);
|
||||
drawVoid();
|
||||
}
|
||||
if(sw==0){
|
||||
tempX[1]=posX[i];
|
||||
tempY[1]=posY[i];
|
||||
posX[i]=tempX[0];
|
||||
posY[i]=tempY[0];
|
||||
SetCursorPosition(posX[i],posY[i]);
|
||||
drawSnake();
|
||||
sw=1;
|
||||
}
|
||||
else if(sw==1){
|
||||
tempX[0]=posX[i];
|
||||
tempY[0]=posY[i];
|
||||
posX[i]=tempX[1];
|
||||
posY[i]=tempY[1];
|
||||
SetCursorPosition(posX[i],posY[i]);
|
||||
drawSnake();
|
||||
sw=0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
void drawFrame(){
|
||||
|
||||
for(int i=0;i<30;i++){
|
||||
for(int j=0;j<100;j++){
|
||||
if(i==0||i==29){
|
||||
SetCursorPosition(j,i);
|
||||
printf("\033[45m \033[0m");
|
||||
}
|
||||
else if(j==0||j==99){
|
||||
SetCursorPosition(j,i);
|
||||
printf("\033[45m \033[0m");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main () {
|
||||
clock_t start_t,this_t;
|
||||
start_t=clock();
|
||||
int posX[100],posY[100],fruitX,fruitY,ft=0;
|
||||
int tempX[2],tempY[2];
|
||||
double thistimeMillisec;
|
||||
srand(time(NULL));
|
||||
/*for(int j=0;j<20;j++){
|
||||
star[0][j]=rand()%4+2;//2-5
|
||||
}*/
|
||||
|
||||
char input,lastInput;
|
||||
initscr();
|
||||
cbreak();
|
||||
noecho();
|
||||
scrollok(stdscr, TRUE);
|
||||
nodelay(stdscr, TRUE);
|
||||
sleep(1);//รอโปรแกรมเปิด
|
||||
|
||||
start_color();
|
||||
init_pair(1,COLOR_WHITE, COLOR_BLACK);
|
||||
wbkgd(stdscr, COLOR_PAIR(1));
|
||||
printf("\e[?25l");//ปิดcursor
|
||||
for(int i=0;i<100;i++){
|
||||
|
||||
posX[i]=-1;
|
||||
posY[i]=-1;
|
||||
}
|
||||
posX[0]=95;
|
||||
posY[0]=25;
|
||||
posX[1]=95;
|
||||
posY[1]=25;
|
||||
srand(time(NULL));
|
||||
fruitX=rand()%98+1;
|
||||
fruitY=rand()%28+1;
|
||||
while(1){ //เริ่มเกม
|
||||
this_t=clock();
|
||||
SetCursorPosition(posX[0],posY[0]);
|
||||
|
||||
if(ft<5){//สร้างงูรอบแรกสุด
|
||||
drawSnake();
|
||||
drawFrame();
|
||||
ft++;
|
||||
}
|
||||
input=getch();//รับจากแป้นพิมพ์
|
||||
if(input=='x'){
|
||||
break;
|
||||
}
|
||||
if(input=='a'||input==leftArrow){
|
||||
if((posX[0]>1)&&(lastInput!='d')){
|
||||
moveSnake(-1,0,posX,posY,tempX,tempY);
|
||||
lastInput='a';
|
||||
}
|
||||
}
|
||||
else if(input=='d'||input==rightArrow){
|
||||
if((posX[0]<98)&&(lastInput!='a')){
|
||||
moveSnake(1,0,posX,posY,tempX,tempY);
|
||||
lastInput='d';
|
||||
}
|
||||
}
|
||||
else if(input=='w'||input==upArrow){
|
||||
if((posY[0]>1)&&(lastInput!='s')){
|
||||
moveSnake(0,-1,posX,posY,tempX,tempY);
|
||||
lastInput='w';
|
||||
}
|
||||
}
|
||||
else if(input=='s'||input==downArrow){
|
||||
if((posY[0]<28)&&(lastInput!='w')){
|
||||
moveSnake(0,1,posX,posY,tempX,tempY);
|
||||
lastInput='s';
|
||||
}
|
||||
}
|
||||
else if(input=='b'){
|
||||
lastInput='0';
|
||||
}
|
||||
|
||||
thistimeMillisec=(double)(this_t-start_t)/(double)CLOCKS_PER_SEC*15;
|
||||
if(((int)thistimeMillisec%10)==1){
|
||||
if(lastInput=='a'){
|
||||
if(posX[0]>1){
|
||||
moveSnake(-1,0,posX,posY,tempX,tempY);
|
||||
lastInput='a';
|
||||
}
|
||||
}
|
||||
else if(lastInput=='d'){
|
||||
if(posX[0]<98){
|
||||
moveSnake(1,0,posX,posY,tempX,tempY);
|
||||
lastInput='d';
|
||||
}
|
||||
}
|
||||
else if(lastInput=='w'){
|
||||
if(posY[0]>1){
|
||||
moveSnake(0,-1,posX,posY,tempX,tempY);
|
||||
lastInput='w';
|
||||
}
|
||||
}
|
||||
else if(lastInput=='s'){
|
||||
if(posY[0]<28){
|
||||
moveSnake(0,1,posX,posY,tempX,tempY);
|
||||
lastInput='s';
|
||||
}
|
||||
}
|
||||
start_t=this_t;
|
||||
}
|
||||
SetCursorPosition(fruitX,fruitY);
|
||||
printf("0");
|
||||
//system("((speaker-test -t sine -f 700)& pid=$!; sleep 0.1s; kill -9 $pid) > /dev/null");
|
||||
}
|
||||
return(0);
|
||||
}
|
Loading…
Reference in a new issue