C 언어 로 뱀 먹 기 게임 을 하 겠 습 니 다.
기능 을 실현 하 다
뱀 은 처음에는 세 마디 를 오른쪽으로 이동한다.사용 자 는 상하 좌우 로 뱀의 이동 을 통제 할 수 있 으 며,음식 은 무 작위 로 생 겨 뱀 이 음식 을 먹 으 면 뱀의 몸 이 길어진다.뱀 이 벽 에 부 딪 히 거나 자신의 몸 에 부 딪 히 면 게임 이 끝난다.
어떻게
뱀 을 게 걸 스 럽 게 먹 는 작은 게임 을 실현 하려 면 먼저 게임 에 무엇이 있 고 어떻게 기능 을 실현 하 는 지 잘 생각해 야 한다.
게임 에는 뱀 과 음식 이 두 가지 밖 에 없 음 이 분명 하 다.
그래서 뱀 과 음식 정 보 를 만들어 뱀 과 음식 을 초기 화하 고 뱀 과 음식 을 그 려 야 한다.
실 현 된 기능 은 다음 과 같다.
1.뱀의 이동
2.버튼 을 눌 러 뱀의 이동 제어
3.음식의 생 성
4.뱀 이 먹 이 를 먹 으 면 몸 이 길어진다
5.게임 의 끝
구조 체 로 뱀 과 음식 을 만 드 는 정보
struct COOR{// ,x,y
int x;
int y;
};
struct SNAKE{//
int size;//
int speed;//
char dir;//
struct COOR xy[MAX];//
}snakes;
struct FOOD{//
struct COOR fooddir;//
int flag;// ,1 ,0
}food;
기능 구현 함수:뱀:
void snakeInit(){//
void drawSnake(){//
void moveSnake(){//
void coorSnake(){//
음식:
void initFood(){//
void drawFood(){//
기타:
int gameOver(){//
void gameInit(){//
코드
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<graphics.h>
#include<conio.h>
#include<Windows.h>
#define MAX 200
HWND hwnd = NULL;
enum DIR{//
UP,
DOWN,
LEFT,
RIGHT,
};
struct COOR{// ,x,y
int x;
int y;
};
struct SNAKE{//
int size;//
int speed;//
char dir;//
struct COOR xy[MAX];//
}snakes;
struct FOOD{//
struct COOR fooddir;//
int flag;// ,1 ,0
}food;
void snakeInit(){//
snakes.size = 3;//
snakes.dir = RIGHT;//
snakes.speed = 10;
int i = 0;
for (; i < snakes.size; i++){// ,
snakes.xy[i].x = 40 - 10 * i;
snakes.xy[i].y = 10;
}
}
void drawSnake(){//
int i = 0;
for (; i < snakes.size; i++){
setlinecolor(BLACK);//
setfillcolor(RED);//
//fillrectangle(snakes.xy[i].x, snakes.xy[i].y, snakes.xy[i].x + 10, snakes.xy[i].y+10);//
fillcircle(snakes.xy[i].x, snakes.xy[i].y, 5);//
}
}
void moveSnake(){//
//snakes.xy[0].x++;
int i = 0;
for (i = snakes.size-1; i >0; i--){//
snakes.xy[i].x = snakes.xy[i-1].x;
snakes.xy[i].y = snakes.xy[i-1].y;
}
switch (snakes.dir){
case UP:
snakes.xy[0].y-=snakes.speed;
break;
case DOWN:
snakes.xy[0].y+=snakes.speed;
break;
case LEFT:
snakes.xy[0].x-=snakes.speed;
break;
case RIGHT:
snakes.xy[0].x+=snakes.speed;
break;
default:
break;
}
}
void coorSnake(){//
if (_kbhit()){ //
char c = _getch();//
switch (c){
case 72:
case'w':
if (snakes.dir != DOWN){
snakes.dir = UP;
}
break;
case 80:
case's':
if (snakes.dir != UP){
snakes.dir = DOWN;
}
break;
case 75:
case'a':
if (snakes.dir != RIGHT){
snakes.dir = LEFT;
}
break;
case 77:
case'd':
if (snakes.dir != LEFT){
snakes.dir = RIGHT;
}
break;
default:
break;
}
}
}
void initFood(){//
food.flag = 1;
while (1){
START:
food.fooddir.x = rand() % 63 * 10;//
food.fooddir.y = rand() % 47 * 10;
for (int i = 0; i < snakes.size; i++){// 。
if (food.fooddir.x == snakes.xy[i].x&&food.fooddir.y == snakes.xy[i].y){
goto START;
}
else{
break;
}
}
break;
}
}
void drawFood(){//
//food.fooddir.x = 100;
//food.fooddir.y = 200;
setlinecolor(BLACK);
setfillcolor(RED);
fillcircle(food.fooddir.x, food.fooddir.y, 5);
}
void eatFood(){//
if (snakes.xy[0].x - food.fooddir.x <= 5 && snakes.xy[0].y - food.fooddir.y <= 5 \
&& food.fooddir.x - snakes.xy[0].x <= 5 && food.fooddir.y - snakes.xy[0].y <= 5 && food.flag == 1){
food.flag = 0;
snakes.size++;
}
}
int gameOver(){//
if (snakes.xy[0].x < 5 || snakes.xy[0].y <= 0 || snakes.xy[0].x > 635 || snakes.xy[0].y > 478){
MessageBox(hwnd, "GAME OVER!"," !", MB_OK);
return 1;
}
for (int i = 1; i < snakes.size; i++){
if (snakes.xy[0].x == snakes.xy[i].x&&snakes.xy[0].y == snakes.xy[i].y){
MessageBox(hwnd, "GAME OVER!", " ",MB_OK);
return 1;
}
}
return 0;
}
void gameInit(){
hwnd=initgraph(640, 480);//
setbkcolor(GREEN);//
}
int main(){
srand((unsigned long)time(NULL));//
gameInit();
cleardevice();//
snakeInit();
initFood();
while (1){
cleardevice();
if (food.flag == 0){
initFood();
}
drawFood();
drawSnake();
coorSnake();
eatFood();
moveSnake();
//eatFood();
if (gameOver()){
break;
}
//stopGame();
Sleep(100);
}
getchar();//
closegraph();
system("pause");
return 0;
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C 언어 구현 천둥 제거 게임 상세 정보먼저 작은 메뉴를 표시하고 게임을 할지 여부를 선택하십시오.사용자가 종료를 선택하면 프로그램 실행이 끝나고, 사용자가 게임을 선택하면 지뢰 제거 위치 좌표를 입력하라는 메시지가 표시됩니다.사용자가 입력한 좌표가 바둑...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.