As3: implementation of a small game "snake"

Source: Internet
Author: User

A few days ago, I saw someone using Silverlight as a "greedy snake" in the garden. on the rise, I also wanted to use as3.0. Although this game has been ruined by many developers, however, as a beginner, it is also a learning method to repeat it.

Analysis of technical difficulties:

1. Composition of the snake body
You can use arrays to store a bunch of small balls and arrange them into continuous straight lines.

 

2. Snake body movement
After the snake head moves, the ball following the snake head moves to the original position of the Snake Head, and then the ball following it moves to the position of the previous ball.

 

3. Collision Detection
When the snake head moves, if it exceeds the stage boundary, the game is over. If it encounters a snake, it is over.

 

4. Food Processing
Place a ball randomly on the stage. When the snake head passes through, the food disappears/the snake body is lengthened (I .e., a new ball is added to the array)/the food is randomly located.

 

Code:
The famous ball class in make things move is still used. However, to record the position where the next ball should be moved, add two variables nextx, nexty, and ball. The complete definition of the ball class is as follows:

Package {import flash. display. sprite; // ball class public class ball extends sprite {public var radius: uint; // radius public var color: uint; // color public var VX: Number = 0; // X axis speed public var Vy: Number = 0; // y axis speed public var count: uint = 0; // The auxiliary counting variable public var isdragged = false; // whether the image is being dragged public var Vr: Number = 0; // rotating speed public var mass: Number = 1; // quality public var nextx: Number = 0; public var nexty: Number = 0; Public Function ball (r: Number = 50, C: uint = 0xff0000) {This. radius = r; this. color = C; Init ();} private function Init (): void {graphics. beginfill (color); graphics. drawcircle (0, 0, radius); graphics. endfill ();}}}

Of course, for this game, there are a lot of variables that can be deleted. Here we keep them for the sake of completeness. The core code of the game:

Import flash. utils. timer; var bils: array; var ballorigincount: uint = 3; var radius: uint = 10; var SW: Number = stage. stagewidth; var sh: Number = stage. stageheight; var TMR: timer; var speed: uint = 200; // millisecond var food: ball; var rows: uint = Sw/(2 * radius); var Cols: uint = Sw/(2 * radius); var isdead: Boolean = false; var ispause: Boolean = true; // initialize function Init (): void {bils = new array (); For (var I: uint = 0; I <ballorigincount; I ++) {Var B: ball = new ball (radius, math. random () * 0 xffffff); bils. push (B); B. X = Sw/2; B. y = sh/2; B. nextx = B. x; B. nexty = B. y; B. VX = B. width; addchild (B) ;}// at the beginning, arrange the ball in a straight line for (I = 1; I <ballorigincount; I ++) {bils [I]. X = bils [I-1]. x-bils [I]. width;} drawgrid (); // initialize food = new ball (radius * 0.8, 0x000000); checkfoodposition (); addchild (food); stage. addeventlistener (keyboardevent. key_down, keydowmhandler); TMR = new timer (speed); TMR. ad Deventlistener (timerevent. timer, movesnake); TMR. start (); _ gameover. visible = isdead; _ notice. visible = ispause;} function restart (): void {// first remove the original snake for (var I: Int = bils. length-1; I> = 0; I --) {removechild (bils [I]);} // repeat all bils. length = 0; ballorigincount = 3; for (I = 0; I <ballorigincount; I ++) {var B: ball = new ball (radius, math. random () * 0 xffffff); bils. push (B); B. X = Sw/2; B. y = sh/2; B. nextx = B. x; B. nexty = B. y; B. VX = B. width; Ddchild (B) ;}for (I = 1; I <ballorigincount; I ++) {bils [I]. X = bils [I-1]. x-bils [I]. width;} isdead = false; ispause = false; _ gameover. visible = isdead; _ notice. visible = ispause;} // Direction Control Function keydowmhandler (E: keyboardevent): void {var B: ball = bils [0]; If (E. keycode = keyboard. space) {ispause =! Ispause; If (isdead) {restart () ;}_ notice. Visible = ispause;} else if (E. keycode = keyboard. Down & B. VX! = 0) {B. Vy = B. width; B. VX = 0;} else if (E. keycode = keyboard. Up & B. VX! = 0) {B. Vy =-B. width; B. VX = 0;} else if (E. keycode = keyboard. Left & B. Vy! = 0) {B. VX =-B. width; B. Vy = 0;} else if (E. keycode = keyboard. Right & B. Vy! = 0) {B. VX = B. width; B. vy = 0 ;}// draw the grid function drawgrid () {for (var I: uint = radius; I <SW; I ++ = 2 * radius) {graphics. linestyle (1, 0, 0.1); graphics. moveTo (I, 0); graphics. lineto (I, SH) ;}for (I = radius; I <sh; I ++ = 2 * radius) {graphics. linestyle (1, 0, 0.1); graphics. moveTo (0, I); graphics. lineto (SW, I) ;}/// mobile function movesnake (E: timerevent): void {// if it is paused or suspended, if (ispause | isdead) {return;} var B: ball = bils [0]; B. nextx = B. X + B. VX; B. nexty = B. Y + B. vy; For (var j: uint = 1; j <ballorigincount; j ++) {VaR _ B: ball = bils [J]; _ B. nextx = bils [J-1]. x; _ B. nexty = bils [J-1]. Y ;}for (j = 0; j <ballorigincount; j ++) {bils [J]. X = bils [J]. nextx; bils [J]. y = bils [J]. nexty;} // If (B. x <B. radius | B. x> sW-b.radius | B. Y <B. radius | B. y> sH-b.radius) {isdead = true; _ gameover. visible = isdead; _ notice. visible = isdead; return;} // checks whether the Snake Head has bit itself for (j = 1; j <ballorigincoun T; j ++) {var target: ball = bballs [J]; If (B. X = target. X & B. y = target. y) {isdead = true; _ gameover. visible = isdead; _ notice. visible = isdead; return; }}// if the snake header passes through the food, it indicates that if (B. X = food. X & B. y = food. y) {// trace ("eaten! "); VaR _ newball: ball = new ball (radius, math. random () * 0 xffffff); _ newball. X =-radius; _ newball. y =-radius; bils. push (_ newball); addchild (_ newball); food. visible = false; ballorigincount ++;} makefood () ;}// generates the new coordinate function makefood () {If (food. visible = false) {checkfoodposition (); food. visible = true;} If (food. alpha = 1) {food. alpha = 0.5; food. scalex = food. scale= 1.2;} else {food. alpha = 1; food. scalex = food. scaley = 1 ;}} Fu Nction checkfoodposition (): void {food. X = radius + int (math. random () * Rows) * 2 * radius; food. y = radius + int (math. random () * Cols) * 2 * radius; // check whether the new coordinates of the food are on the "snake". If yes, generate the new coordinates var iswrongpositon: Boolean = true; while (iswrongpositon) {var checkflag: Boolean = true; For (var I: uint = 0; I <ballorigincount; I ++) {var B: ball = bils [I]; if (B. X = food. Y & B. y = food. y) {checkflag = false; break;} If (! Checkflag) {food. X = radius + int (math. random () * Rows) * 2 * radius; food. y = radius + int (math. random () * Cols) * 2 * radius;} iswrongpositon =! Checkflag ;}} Init ();

Source File Download: http://cid-2959920b8267aaca.office.live.com/self.aspx/Flash/Snake.rar

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.