C語言所寫的簡單的貪吃蛇程式碼
- 才剛開始學習不久,學到連結串列之後老師佈置了這個任務,用了很多不太熟悉的函式,例如WINDOWS.H中的定位游標,暫停、清空面板、改變面板大小等。
- 之後我會慢慢加上註釋。
- #include<stdio.h>
- #include<stdlib.h>
#include<time.h> #include<windows.h> #define U 1 #define D 2 #define L 3 #define R 4 void Pos(); void creatmap(); void creatsnake(); int biteself(); void creatfood(); void cantcrosswall(); void snakemove(); void welcometogame(); void gamestart(); void caozuo(); typedef struct snake { int x; int y; struct snake *next; }snake; int score=0,add=10,maxscore=0; int status,sleeptime=200; snake *head,*food; snake *q; int endgamestatus=0; void Pos(int x,int y) { COORD pos; HANDLE hOutput; pos.X=x; pos.Y=y; hOutput=GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOutput,pos); } void creatmap() { int i; for(i=0;i<58;i+=2) { Pos(i,0); printf("■"); Pos(i,26); printf("■"); } for(i=1;i<26;i++) { Pos(0,i); printf("■"); Pos(56,i); printf("■"); } } void creatsnake() { snake *tail; int i; tail=(snake*)malloc(sizeof(snake)); tail->x=24; tail->y=5; tail->next=NULL; for(i=1;i<=4;i++) { head=(snake*)malloc(sizeof(snake)); head->next=tail; head->x=24+2*i; head->y=5; tail=head; } Pos(tail->x,tail->y); printf("⊙"); tail=tail->next; while(tail!=NULL) { Pos(tail->x,tail->y); printf("●"); tail=tail->next; } } int biteself() { snake *self; self=head->next; while(self!=NULL) { if(self->x==head->x && self->y==head->y) { return 1; } self=self->next; } return 0; } void creatfood() { snake *food_1; srand((unsigned)time(NULL)); food_1=(snake*)malloc(sizeof(snake)); while(food_1->x%2!=0) { food_1->x=rand()%52+2; } food_1->y=rand()%24+1; q=head; while(q->next==NULL) { if(q->x==food_1->x && q->y==food_1->y) { free(food_1); creatfood(); } q=q->next; } Pos(food_1->x,food_1->y); food=food_1; printf("◎"); } void cantcrosswall() { if(head->x==0 || head->x==56 ||head->y==0 || head->y==26) { endgamestatus=1; } } void snakemove() { snake * nexthead; cantcrosswall(); nexthead=(snake*)malloc(sizeof(snake)); if(status==U) { nexthead->x=head->x; nexthead->y=head->y-1; if(nexthead->x==food->x && nexthead->y==food->y) { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } score=score+add; creatfood(); } else { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q->next->next!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } Pos(q->next->x,q->next->y); printf(" "); free(q->next); q->next=NULL; } } if(status==D) { nexthead->x=head->x; nexthead->y=head->y+1; if(nexthead->x==food->x && nexthead->y==food->y) { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } score=score+add; creatfood(); } else { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q->next->next!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } Pos(q->next->x,q->next->y); printf(" "); free(q->next); q->next=NULL; } } if(status==L) { nexthead->x=head->x-2; nexthead->y=head->y; if(nexthead->x==food->x && nexthead->y==food->y) { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } score=score+add; creatfood(); } else { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q->next->next!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } Pos(q->next->x,q->next->y); printf(" "); free(q->next); q->next=NULL; } } if(status==R) { nexthead->x=head->x+2; nexthead->y=head->y; if(nexthead->x==food->x && nexthead->y==food->y) { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } score=score+add; creatfood(); } else { nexthead->next=head; head=nexthead; q=head; Pos(q->x,q->y); printf("⊙"); q=q->next; while(q->next->next!=NULL) { Pos(q->x,q->y); printf("●"); q=q->next; } Pos(q->next->x,q->next->y); printf(" "); free(q->next); q->next=NULL; } } if(biteself()==1) { endgamestatus=2; } } void pause() { while(1) { Sleep(300); if(GetAsyncKeyState(VK_SPACE)) { break; } } } void welcometogame() { Pos(40,12); printf("歡迎來到貪食蛇遊戲!"); Pos(40,25); system("pause"); system("cls"); } void caozuo() { Pos(64,16); printf("你可以用↑.↓.←.→分別控制蛇的移動."); Pos(64,18); printf("ESC :退出遊戲.space:暫停遊戲."); Pos(64,20); printf("F1:減慢蛇的運動速度 F2:加快它的速度"); status=R; while(1) { Pos(64,10); printf("得分:%d ",score); Pos(64,11); printf("每個食物得分:%d分",add); Pos(64,12); printf("最高分:%d分",maxscore); if(GetAsyncKeyState(VK_UP) && status!=D) { status=U; } else if(GetAsyncKeyState(VK_DOWN) && status!=U) { status=D; } else if(GetAsyncKeyState(VK_LEFT)&& status!=R) { status=L; } else if(GetAsyncKeyState(VK_RIGHT)&& status!=L) { status=R; } else if(GetAsyncKeyState(VK_SPACE)) { pause(); } else if(GetAsyncKeyState(VK_ESCAPE)) { endgamestatus=3; break; } else if(GetAsyncKeyState(VK_F1)) { if(sleeptime>=50) { sleeptime=sleeptime-30; add=add+2; if(sleeptime==320) { add=2; } } } else if(GetAsyncKeyState(VK_F2)) { if(sleeptime<350) { sleeptime=sleeptime+30; add=add-2; if(sleeptime==350) { add=1; } } } Sleep(sleeptime); snakemove(); if(endgamestatus!=0) { system("cls"); Pos(24,12); if(endgamestatus==1) { printf("對不起,您撞到牆了,遊戲結束."); } else if(endgamestatus==2) { printf("對不起,您咬到自己了,遊戲結束."); } else if(endgamestatus==3) { printf("您結束了遊戲。"); } Pos(24,13); if(maxscore<score) { printf("恭喜你打破了記錄併成為了最高分!"); maxscore= score; } Pos(24,14); printf("您的得分是%d\n",score); endgamestatus=0; system("pause"); break; } } } void gamestart() { system("mode con cols=100 lines=30"); welcometogame(); creatmap(); creatsnake(); creatfood(); } int main() { while(1) { gamestart(); caozuo(); } return 0; }
相關推薦
C語言寫的貪吃蛇(國慶任務專案)
分享寫貪吃蛇的緣起 博主大一新生,因為有任務C語言寫貪吃蛇,去網上找別人的原始碼試一試效果,可是沒有一個可以通過編譯執行,現在博主好不容易寫完了貪吃蛇,卻深感單初不易,現在分享一下原始碼,應該不會像網上大多數的C語言貪吃蛇程式碼無法編譯正確,通過執行。(我用的D
C語言所寫的簡單的貪吃蛇程式碼
才剛開始學習不久,學到連結串列之後老師佈置了這個任務,用了很多不太熟悉的函式,例如WINDOWS.H中的定位游標,暫停、清空面板、改變面板大小等。之後我會慢慢加上註釋。#include<stdio.h>#include<stdlib.h>#incl
C++控制檯貪吃蛇程式碼
1 #include"Snake_Class.h" 2 #include<iostream> 3 #include<fstream> 4 #include<ctime> 5 #include<cstdlib> 6 7 //獲取緩衝區控制代碼
C++貪吃蛇程式碼
C++貪吃蛇 初嘗,略亂。 #include <bits/stdc++.h> #include <windows.h> #include <stdio.h> #include <conio.h> usin
C語言貪吃蛇程式碼
#include <stdio.h> #include <Windows.h> #include <time.h> #include <stdlib.h> #include <conio.h> typedef s
簡單貪吃蛇遊戲js
貪吃蛇 col doc var math oat turn rip onkeyup 新手,仿照別人的寫的。只能實現簡單的功能,代碼如下: 1 <!DOCTYPE html> 2 <html> 3 <head> 4
C語言控制臺貪吃蛇2
隨機數函數 edi mms ‘\0’ sign rand() 拷貝 printf rand 顯示遊戲邊框及蛇的位置初始化 一、顯示遊戲邊框 1、定義二位數組,例如20*48,將並邊框按行打印(運行顯示邊框會卡,暫時不會別的寫法QAQ) 註:一個方塊兩個字節。 /
C語言控制臺貪吃蛇4
eba bsp console 食物 blog pan 是否 lec ons 蛇死亡判斷、吃食物、計分 一、蛇死亡判斷:裝邊界,撞自己死亡 定義:IsSnakeDie()//蛇死亡判斷 函數,返回類型為bool型 思路: //蛇頭的下一個,是方塊 就是死亡
二十行貪吃蛇程式碼詳解
文章目錄 完整原始碼 詳解 完整原始碼 原文:https://blog.csdn.net/hj7jay/article/details/51011269 <!doctype html> <html> <bod
JavaScrip寫的貪吃蛇
原生JavaScript寫的 貪吃蛇這個遊戲好像就如同hello world一樣,簡單又有代表性,以前我學習c語言的時候,第一個做的趣味小遊戲也是貪吃蛇--------------------------------- 1 //貪吃蛇的食物模組 2 (function(){ 3
一步一步用Canvas寫一個貪吃蛇
之前在慕課網看了幾集Canvas的視訊,一直想著寫點東西練練手。感覺貪吃蛇算是比較簡單的了,當年大學的時候還寫過C語言字元版的,沒想到還是遇到了很多問題。 最終效果如下(圖太大的話 時間太長 錄製gif的軟體有時限…) 首先定義遊戲區域。貪吃蛇的螢幕上只有蛇身和蘋果兩種元素,而這兩個都可以用正方形格子
c++經典專案控制檯貪吃蛇小遊戲詳細教程
貪吃蛇GreedySnake 本文將講解如何使用c++面向物件方法編寫控制檯版貪吃蛇小遊戲,專案github地址:silence1772/GreedySnake 遊戲下載:GreedySnake 本人屬初學者,水平所限,難免有所錯誤及不妥之處,勞請指出或發
c++簡易100行貪吃蛇
大一小白的清明節作業。這個貪吃蛇的核心思路是用vector建立物件陣列,將蛇體移動轉化為蛇頭移動並將尾部物件刪除,吃到食物則不刪除尾部。遊戲開始後點擊wasd鍵進行上下左右移動。#include <iostream> #include<vector>
HTML5貪吃蛇程式碼
首先宣告,這是網上摘錄的貪吃蛇完整程式碼,放在我部落格上僅作學習用。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <tit
C/C++之寫出高質量程式碼
自從看了林銳博士的《高質量C++程式設計指南》以後,感覺自己以前寫的程式碼都是一些垃圾,真的low,根本不知道一些基本的程式設計規範,然後看了一些大神寫的程式碼,清晰易懂,而且還很漂亮,給人一種賞心悅目的感覺,寫一手高質量的程式碼,是一個程式設計師最基本的素養,
C語言遊戲之貪吃蛇--連結串列實現
早自習時突然想到怎麼寫貪吃蛇,話不多說,程式碼如下: 開發環境:vs2015 雖然開始還是出了很多指標問題。。。(很煩C語言指標)除錯了很久也大概是可以暢快的玩了。 C語言新手,有很多寫得不好的地方望大神提出 當然,我也不介意把我程式碼拿去學習(這樣就說明我的程式碼有
Python寫的貪吃蛇遊戲例子
from Tkinter import * import tkMessageBox,sys from random import randint class Grid(object): def __init__(self,master=None,window_width=800,window_hei
C++ Windows程式設計實現貪吃蛇(可用Dev C++實現)
最近學習Windows程式設計,試著自己做了個簡單的貪吃蛇遊戲。不到300行程式碼,把Windows訊息機制,繪製方法,多執行緒等知識都用上了,適合初學者入門。效果圖如下:完整程式碼附註釋:#include <windows.h> #include <std
C# RichTextBox 做簡單的HTML程式碼編輯器 ---------左側顯示行號
說明:此顯示行號為實際行號,不論是空行還是自動換行,都計算在內,跟實際IDE的行號不同,同步滾動會有半行高度以內的誤差。 實現原理,在RichTextBox 編輯器左側放置另一RichTextBox (或其它控制元件也可),行號為編輯器實際文字行數,滾動時計算文字滾動高
純JS寫的貪吃蛇遊戲(大二暑假實習)
此遊戲是大二暑假實習無聊,然後弄的,剛翻檔案看到了,所以想和你們分享一下。。。<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title&