Skip to content

Instantly share code, notes, and snippets.

@watura
Created May 3, 2012 02:06
Show Gist options
  • Save watura/2582551 to your computer and use it in GitHub Desktop.
Save watura/2582551 to your computer and use it in GitHub Desktop.
RitsPenでTronっぽいゲームっぽいものを作ってみた. awsdで操作. ソースコードとしては全然参考にならない汚い手抜きなものなので注意. さらに配列のはしっこの方とかちゃんと動くのかチェックしていないのでたぶおかしくなる
// シスプロ参照 7 12-
// Wataru Nishimoto
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
#include<ctype.h>
#include<termios.h>
#include<unistd.h>
#include "RitsPen.h"
#define BUFFSIZE 256
#define WAIT 20
int list[620][700]; //画面の配列
int angle = 0; //別スレッドで共有される変数
int willStop = 0;//別スレッドで共有される変数
int current_angle = 0;
void *drawLoop();
int StdinRaw(int flag)
{//キーボードからの入力をごにょごにょする処理
static struct termios tio_save;
struct termios tio;
if (flag == 1){
if (tcgetattr(0, &tio) == -1) {
perror("tcgetattr");
return(-1);
}
tio_save = tio; /* store data */
tio.c_iflag &= ~(BRKINT|ISTRIP|IXON);
tio.c_lflag &= ~(ICANON|IEXTEN|ECHO|ECHOE|ECHOK|ECHONL|ISIG);
tio.c_cc[VMIN] = 1; /* 受信するべき文字の最小数 */
tio.c_cc[VTIME] = 0; /* バーストで短期のデータ伝送をタイムアウトするために使用する0.10秒単位のタイマ */
if (tcsetattr(0, TCSANOW, &tio) == -1){
perror("tcsetattr");
return(-1);
}
return(0);
}
else{
if(tcsetattr(0, TCSANOW, &tio_save) == -1){
perror("tcsetattr");
return(-1);
}
return(0);
}
}
int main(){
char buf[BUFFSIZE];
int width = 5;
int r1 = 0;
pthread_t thread; // スレッド
unsigned char c;
if (pthread_create(&thread, NULL, drawLoop,NULL) != 0) //スレッドの生成
perror("pthread_create"), exit(1);
StdinRaw(1);
while(1){
c = getchar();
if (c == 0x04){ /* ^D */
break;
}
// 入力を受けてangleをどんどん変更していく.
// 効率のよい方法考えるのめんどくさくなったので適当にコピペ祭りした
if(c == 'a'){
switch(current_angle){
case 0:
angle = 180;
break;
case 90:
angle = 90;
break;
case 180:
angle = 0;
break;
case 270:
angle = 270;
break;
default:
break;
}
current_angle = 180;
}
if(c == 's'){
switch(current_angle){
case 0:
angle = 90;
break;
case 90:
angle = 0;
break;
case 180:
angle = 270;
break;
case 270:
angle = 180;
break;
default:
break;
}
current_angle = 90;
}
if(c == 'd'){
switch(current_angle){
case 0:
angle = 0;
break;
case 90:
angle = 270;
break;
case 180:
angle = 180;
break;
case 270:
angle = 90;
break;
default:
break;
}
current_angle = 0;
}
if(c == 'w'){
switch(current_angle){
case 0:
angle = 270;
break;
case 90:
angle =180;
break;
case 180:
angle = 90;
break;
case 270:
angle = 0;
break;
default:
break;
}
current_angle = 270;
}
if(willStop == 1)break;
}
willStop = 1;
StdinRaw(0);
}
void *drawLoop(){
struct timespec req = {0, WAIT * 1000000};
int speed = 1;
int x = 350;int y = 250; //初期位置
PEN pen = CreatePEN("tron");
ChangeSpeed(pen,WAIT);
list[x][y] = 1;
while(1){
// ペンの移動with wait waitしないと恐しい勢いでクエリを追加してとまらなくなる
nanosleep(&req,NULL);
UsePEN(pen,speed,angle);
angle = 0;
switch(current_angle){
case 0:
x++;
break;
case 90:
y++;
break;
case 180:
x--;
break;
case 270:
y--;
break;
default:
break;
}
if(x>=0&&y>=0&&x<620&&y<700){
if(list[x][y] == 1)willStop = 1;
list[x][y] = 1;
}
if(willStop == 1)break;
}
ClosePEN(pen);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment