簡介:
因為我準備要在NIOS上面跑貪食蛇程式,所以就要先用C寫一隻程式,不過這是DOS版。
原始碼:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#include "utilwin32.h"
/*
up 00 48 shift-up
down 00 50 shift-down
left 00 4b shift-left
right 00 4d shift-right
*/
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define MAX_X 80
#define MAX_Y 25
#define bool int
#define true 1
#define false 0
struct node {
int x;
int y;
struct node *next;
};
int foodx,foody;
typedef struct node queue;
queue *front, *rear;
bool outFrame(int x,int y){
bool b = false;
if(x<1 || x>=MAX_X || y<1 || y>=MAX_Y )
b=true;
return b;
}
void print(int x,int y,char c){
gotoxy(x,y);
printf("%c",c);
}
void createq() {
front = (queue*) malloc(sizeof(queue));
rear = (queue*) malloc(sizeof(queue));
front->next = rear;
rear->next = NULL;
rear->x = 1;
rear->y = 1;
}
void removeq(){
if(!front->next->next==NULL){
queue *temp;
temp=front->next;
front->next=temp->next;
free(temp);
}
}
void addq(int x,int y){
queue *newnode = (queue*) malloc(sizeof(queue));
if(front->next->next==NULL){
front->next->next = newnode;
}
newnode->x = x;
newnode->y = y;
newnode->next = NULL;
rear->next = newnode;
rear = newnode;
}
void showq() {
queue *temp;
temp = front->next;
while(temp != NULL) {
print(temp->x,temp->y,'#');
temp = temp->next;
}
}
bool isSame(){
bool b = false;
queue *temp;
temp = front->next;
while(temp != NULL) {
if(temp->x==foodx && temp->y==foody){
return true;
}
temp = temp->next;
}
return false;
}
void food() {
while(true){
foodx=rand()%78+1;
foody=rand()%23+1;
if(!isSame())
break;
}
//printf("foodx=%d,\tfoody=%d\n",foodx,foody);
}
int main()
{
srand(time(NULL));
createq();
food();
int c1, c2;
int x=1,y=1;
char control=RIGHT;
//RUN game
while (true){
if(kbhit()){
//Enter KEY
c1 = getch();
if ((c1 == 0xe0)||(c1==00))
{
c2 = getch();
switch(c2){
case UP:
if(control!=DOWN)
control=UP;
break;
case LEFT:
if(control!=RIGHT)
control=LEFT;
break;
case DOWN:
if(control!=UP)
control=DOWN;
break;
case RIGHT:
if(control!=LEFT)
control=RIGHT;
break;
}
}
}
switch(control){
case UP:
if(!outFrame(x,y-1)){
clrscr();
y=y-1;
if(! (x==foodx && foody==y )){
removeq();
}else{
food();
}
addq(x,y);
showq();
//print(x,y,'#');
}
break;
case LEFT:
if(!outFrame(x-1,y)){
clrscr();
x=x-1;
if(! (x==foodx && foody==y )){
removeq();
}else{
food();
}
addq(x,y);
showq();
//print(x,y,'#');
}
break;
case DOWN:
if(!outFrame(x,y+1)){
clrscr();
y=y+1;
if(! (x==foodx && foody==y )){
removeq();
}else{
food();
}
addq(x,y);
showq();
//print(x,y,'#');
}
break;
case RIGHT:
if(!outFrame(x+1,y)){
clrscr();
x=x+1;
if(! (x==foodx && foody==y )){
removeq();
}else{
food();
}
addq(x,y);
showq();
//print(x,y,'#');
}
break;
}
print(foodx,foody,'*');
Sleep(50);
}
system("pause");
return 0;
}
解釋:
#include "utilwin32.h"
因為DEV-C++沒有gotoxy() and clrscr() 所以要加入這2個檔
下載處utilwin32.c, utilwin32.h
gotoxy() 將游標移到那裡
clrscr() 清除螢幕
/*
up 00 48 shift-up
down 00 50 shift-down
left 00 4b shift-left
right 00 4d shift-right
*/
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define MAX_X 80
#define MAX_Y 25
定義一些按鍵控制碼(左、上、右、下)
struct node {
int x;
int y;
struct node *next;
};
typedef struct node queue;
queue *front, *rear;
用到Queue資料結構 這裡就不多講了
待續...
程式下載:
參考:
[1].簡單遊戲程式--貪食蛇
浙公网安备 33010602011771号