Go language Greed pc egg set the difference between a dragon eating snake and C language

Source: Internet
Author: User

Use GOPC egg to build a dragon, need to search dsluntan.com, language write snake game then will use the object-oriented thinking to write, create a snake body object, and then write out,/go language writing when we need to call a C language to write a package/, The go language can call the C language function directly, very convenient and concise, we first look at my own written C language of a package

Package Clib

/*
#include <windows.h>
#include <conio.h>

Use the WINAPI to move the console cursor
void Gotoxy (int x,int y)
{
COORD C;
C.x=x,c.y=y;
SetConsoleCursorPosition (GetStdHandle (Std_output_handle), c);
}

Gets the key once from the keyboard, but does not display to the console
int Direct ()
{
return _getch ();
}
Remove the console cursor
void Hidecursor ()
{
Console_cursor_info CCI;
cci.bvisible = FALSE;
cci.dwsize = sizeof (CCI);
Setconsolecursorinfo (GetStdHandle (std_output_handle), &cci);
}
*/
Import "C"//Can embed C function in Go

//Set console cursor position
func gotopostion (X int, Y int) {
//Call C language function
C.gotoxy (C.int (X), C.int (Y))
}
// No explicit get keyboard input character
Func Direction () (key int) {
key = Int (C.direct ())
return
}
//Set console cursor Hide
func hidecursor () {
C.hidecursor ()
}
1
2
3
4
5
6
7
8
9
Ten
One
15


+


33


+
(
)
-
-

-
-

$
+
$
PNs
$
All-inclusive
-
-
-----this package writes some functions that need to be used in C, and calls C's functions to use the C language environment. Don't forget to install the C language environment on your computer. Let's have a look at this directory structure
write a description of the image here

First of all, our code is placed in the gocode below the SRC directory below, clib inside is the C language of their own package, bulimia Snake and Clib is the same level of directory, where we use the Goland compiler, the compiler can choose its own, we compile the time cannot be compiled by a single file , because you need to call your own package, so choose Multi-file compilation
Write a picture description here
I use the Goland compile time need to change to directory and then the directory is selected in the directory of the SRC directory, and then set up can be directly compiled to run, of course, can also be directly command-line compilation run
Write a picture description here
, we can go to build directly under the directory./This is the build executable. exe, or you can directly compile and run directly using the Go Run command.
Interested partners can try it out for themselves
The following is a look at the code of the Go language, (you can improve their own, such as adding the level, add obstacles, the speed of the snake is self-adjustable)

Package Main

Import (
"Clib"
"FMT"
"OS"
"Math/rand"
"Time"
)

const wide int = 20
const high int = 20

var key int64 = 1//Level
var food1 food//define a global diet structure
var size int = 2//Defines the length of a global snake
var score int = 0//define a global score
var dx int = 0
var dy int = 0//Snake offset
var barr1 barrier//Obstacle structure
var c cake//define a cake
var FLAG bool=true
Type postion struct {
x int
y Int//parent-class coordinates
}
Type Cake struct{
CA [5]postion
}//Define a cake
Type snake struct {
p [Widehigh]postion
Size int
Dir byte
}
Type barrier struct {
Barr [6]postion
}//Obstacle structure body
Func (c
Cake) Setcake () {
X:=rand. INTN (wide-6) +3
Y:=rand. INTN (high-6) +3
C.ca[0].x,c.ca[0].y=x,y
C.ca[1].x,c.ca[1].y=x-1,y
c.ca[2].x,c.ca[2].y= X-2,y
C.ca[3].x,c.ca[3].y=x-1,y-1
C.ca[4].x,c.ca[4].y=x-1,y+1
}
Func (bbarrier) Setbarrier () {//define some random obstructions
B.barr[0].x,b.barr[0].y=rand. INTN (wide-1) +1,rand. INTN (high-3) +1
B.barr[1].x,b.barr[1].y=rand. INTN (wide-1) +1,rand. INTN (high-3) +1
B.barr[2].x,b.barr[2].y=rand. INTN (wide-1) +1,rand. INTN (high-3) +1
B.barr[3].x,b.barr[3].y=rand. INTN (wide-1) +1,rand. INTN (high-3) +1
B.barr[4].x,b.barr[4].y=rand. INTN (wide-1) +1,rand. INTN (high-3) +1
B.barr[5].x,b.barr[5].y=rand. INTN (wide-1) +1,rand. INTN (high-3) +1
}
Type food struct {
Postion
}//Food
Func Drawui (P postion, ch byte) {
Clib.gotopostion (p.x
2+4, p.y+2+2)
Fmt. fprintf (OS. Stderr, "%c", ch)
}
Func (S *snake) Initsnake () {//Snake initialization
s.p[0].x = WIDE/2
S.P[0].Y = HIGH/2
s.p[1].x = WIDE/2-1
S.P[1].Y = HIGH/2//Snake head and first snake node initialization
S.dir = ' R '
s.size=2
Fmt. Fprintln (OS. Stderr,
`
#-----------------------------------------#

#-----------------------------------------#
')
Food1 = Food{postion{rand. INTN (wide), Rand. INTN (High)-2}}//Food initialization
Drawui (food1.postion, ' o ')//Draw food

Go func () {clib.gotopostion (46,19) fmt. Printf ("Doing%d off, beware of obstructions", key)//} () go func () {for{time. Sleep (time. Second) Num:=rand. INTN (Ten) if num==6{C.setcake () Break}} for I:=0;i<len (c.ca); i++{Dr Awui (C.ca[i], ' # ')}} ()//The role of eating cake go func () {for I:=0;i<len (Barr1.barr); i++{Clib.gotopostio    N (barr1.barr[i].x,barr1.barr[i].y) Drawui (barr1.barr[i], '! ') }} ()//Print out obstacle go func () {for {switch clib.direction () {case, 119:if, S.di                r = = ' D ' {break} S.dir = ' U ' case, 75:if, S.dir = = ' R ' {                Break} S.dir = ' l ' case, 77:if s.dir = = ' L ' {            Break} S.dir = ' R ' case, 80:if, S.dir = = ' U ' {break       } s.dir = ' D ' case 32:     S.dir = ' P '}} ()//Get the direction of the snake run 

}

Func (S *snake) PlayGame () {
Barr:=barrier{postion{rand. INTN (wide-5) +5,rand. INTN (high-5) +3}
Drawui (barr.postion, ' P ')
for {
Switch Key {
Case 1:time. Sleep (time. SECOND/3)
Case 2:time. Sleep (time. SECOND/5)
Case 3:time. Sleep (time. SECOND/6)
Case 4:time. Sleep (time. SECOND/7)
Case 5:time. Sleep (time. SECOND/8)
Case 6:time. Sleep (time. SECOND/9)//used to speed up each additional snake
}

    if S.dir = = ' P ' {continue} if s.p[0].x < 0 | | s.p[0].x >= Wide | | S.p[0].y+2 < 0 | | S.p[0].y >= high-2 {clib.gotopostion (wide*3, high-3) Flag=false return//If the snake head touches the wall it dies}//if s . p[0].x==barr.postion.x&&s.p[0].y==barr.postion.y{//Clib.gotopostion (wide*3, high-3)//return//If the snake head touches the barrier The object dies//} for I: = 1; I <s.size;            i++ {if s.p[0].x = = s.p[i].x && S.p[0].y = = s.p[i].y {clib.gotopostion (wide*3, high-3) Flag=false return}} for J:=0;j<len (Barr1.barr); j++{if S.P[0].X==BARR1.BARR[J].X&A        mp;&s.p[0].y==barr1.barr[j].y{clib.gotopostion (wide*3, high-3) Flag=false return }//Encounter obstruction Death} for M:=0;m<len (c.ca); m++{if s.p[0].x==c.ca[m].x&&s.p[0].y==c.ca[        m].y{s.size++ score++} if score >= int (6+key*2) {key++    return}} if s.p[0].x = = food1.x && S.p[0].y = = food1.y {s.size++ score++ If score >= int (6+key*2) {key++ return}//Draw snake//food1 = food{postion{rand.i NTN (wide), Rand. INTN (High)-2}} for {flag: = True Temp: = Food{postion{rand. INTN (wide), Rand. INTN (High)-2}} for I: = 1; i < s.size; i++ {if (temp.postion.x = = s.p[i].x && TEMP.POSTION.Y = = s.p[i].y) {flag = FA  LSE break} ' for I:=0;i<len (Barr1.barr); i++{if                    temp.postion.x==barr1.barr[i].x&&temp.postion.y==barr1.barr[i].y{Flag=false            Break}} If flag = = true {food1 = temp break    }} drawui (Food1.postion, ' o ')} switch s.dir {case ' U ':    DX = 0 dy = 1 case ' D ': dx = 0 dy = 1 case ' L ': dx =-1 dy = 0 case ' R ': DX = 1 dy = 0} LP: = S.p[s.size-1]//snake tail position for i: = s.size-1; i > 0;  i--{S.p[i] = S.p[i-1] Drawui (s.p[i], ' * ')} DRAWUI (LP, ')//snake tail draw space s.p[0].x + = dx S.p[0].y + = DY//Update snake head Drawui (s.p[0], ' O ')//Draw Snake head}

}
Func Main () {
Rand. Seed (time. Now (). Unixnano ())
var s snake

for k:=1;k<=6;k++{    //用来循环6次代表6个关卡,这里可以自己设置多少关卡    s.initsnake()        //初始化    barr1.setbarrier()  //障碍物    s.playgame()       //玩游戏开始    if FLAG==false{        //这个代表蛇死亡返回的,所以这样就退出了        Clib.GotoPostion(46,21)        fmt.Printf("你已死亡,第%d关总分:%d分",k, score)        break    }    Clib.GotoPostion(46,21)    fmt.Printf("第%d关总分:%d分,稍等进入下一关",k, score)    //key++    time.Sleep(time.Second * 5)  //延时5秒    Clib.Cls()                  //每一关清屏一下    //size=2    score=0                   //每一关分数置为0}time.Sleep(time.Second * 5)   //延时5秒

}

Go language Greed pc egg set the difference between a dragon eating snake and C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.