<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf8">
<title>/usr/web/sources/contrib/john/snake.c - Plan 9 from Bell Labs</title>
<!-- THIS FILE IS AUTOMATICALLY GENERATED. -->
<!-- EDIT sources.tr INSTEAD. -->
</meta>
</head>
<body>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"><a href="/plan9/">Plan 9 from Bell Labs</a>&rsquo;s /usr/web/sources/contrib/john/snake.c</span></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
Copyright © 2009 Alcatel-Lucent.<br />
Distributed under the
<a href="/plan9/license.html">Lucent Public License version 1.02</a>.
<br />
<a href="/plan9/download.html">Download the Plan 9 distribution.</a>
</font>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<table width="100%" cellspacing=0 border=0><tr><td align="center">
<table cellspacing=0 cellpadding=5 bgcolor="#eeeeff"><tr><td align="left">
<pre>
<!-- END HEADER -->
#include &lt;u.h&gt;
#include &lt;libc.h&gt;
#include &lt;draw.h&gt;
#include &lt;event.h&gt;

#define SWIDTH 10 /* segment width */

typedef enum {
	UP,
	DOWN,
	LEFT,
	RIGHT
} Direction;

typedef 
struct Snake {
	Point p[50];
	int length;
	int head;
	int tail;
	Direction d;
} Snake;

Image *dots, *back, *snakecolor, *foodcolor;
struct Snake s; /* our hero */
Point food;

void
newfood(void) {
	Point newfood;
	Rectangle r = screen-&gt;r;

	newfood.x = r.min.x + ntruerand(29)*10;
	newfood.y = r.min.y + ntruerand(29)*10;

	food = newfood;
}	

void
redraw(Image *screen)
{
	static Rectangle r;
	Point pt;
	Rectangle foo;
	int i;

	r = screen-&gt;r;
	draw(screen, screen-&gt;r, back, nil, ZP);

	foo = Rect(food.x, food.y, food.x + SWIDTH, food.y + SWIDTH);
	draw(screen, foo, foodcolor, nil, ZP);

	for (i = 0; i &lt; s.length; i++) {
		pt = s.p[s.head - i];
		if (s.head - i &lt; 0)
			pt = s.p[50 + (s.head - i)];
		foo = Rect(pt.x, pt.y, pt.x + SWIDTH, pt.y + SWIDTH);
		draw(screen, foo, snakecolor, nil, ZP);
	}
	flushimage(display, 1);
}

void
movesnake(void) {
	Rectangle r = screen-&gt;r;
	Point head, pt;
	int i;

	if (s.length &lt; 50) {
		if (s.p[s.head].x == food.x &amp;&amp; s.p[s.head].y == food.y) {
			s.length++;
			newfood();
		} else {
			s.tail++;
			if (s.tail == 50)
				s.tail = 0;
		}
		s.head++;
		if (s.head == 50)
			s.head = 0;

		switch (s.d) {
			case UP:
				s.p[s.head] = subpt(s.p[s.head-1], Pt(0,SWIDTH));
				if (s.head == 0)
					s.p[0] = subpt(s.p[49], Pt(0,SWIDTH));
				break;
			case DOWN:
				s.p[s.head] = addpt(s.p[s.head-1], Pt(0,SWIDTH));
				if (s.head == 0)
					s.p[0] = addpt(s.p[49], Pt(0,SWIDTH));
				break;
			case LEFT:
				s.p[s.head] = subpt(s.p[s.head-1], Pt(SWIDTH,0));
				if (s.head == 0)
					s.p[0] = subpt(s.p[49], Pt(SWIDTH,0));
				break;
			case RIGHT:
				s.p[s.head] = addpt(s.p[s.head-1], Pt(SWIDTH,0));
				if (s.head == 0)
					s.p[0] = addpt(s.p[49], Pt(SWIDTH,0));
				break;
			default:
				print("wtf mate\n");
				break;
		}

		head = s.p[s.head];
		for (i = 1; i &lt; s.length; i++) {
			pt = s.p[s.head - i];
			if (s.head - i &lt; 0)
				pt = s.p[50 + (s.head - i)];
			if (head.x == pt.x &amp;&amp; head.y == pt.y) {
				print("You hit yourself! Score: %d\n", s.length);
				exits(nil);
			}
		}
		if ((head.x &lt; r.min.x) || (head.x &gt; r.max.x - 10) || (head.y &lt; r.min.y) || (head.y &gt; r.max.y - 10)) {
			print("You hit a wall! Score: %d\n", s.length);
			exits(nil);
		}
	}
	redraw(screen);
}

void
eresized(int new)
{
	if(new &amp;&amp; getwindow(display, Refnone) &lt; 0)
		fprint(2,"can't reattach to window");
	redraw(screen);
}

void
resize(int x, int y)
{
	int fd;

	fd = open("/dev/wctl", OWRITE);
	if(fd &gt;= 0){
		fprint(fd, "resize -dx %d -dy %d", x, y);
		close(fd);
	}
}

void
main(int, char**)
{
	Event e;
	Mouse m;
	Menu menu;
	char *mstr[] = {"exit", 0};
	int key, timer, inputk;
	int t;

	resize(300,300);

	initdraw(0,0,"snake");

	back = allocimagemix(display, DPalebluegreen, DWhite);
	snakecolor = allocimage(display, Rect(0, 0, 1, 1), RGB24, 1, DGreen);
	foodcolor = allocimage(display, Rect(0,0,1,1), RGB24, 1, DBlack);

	food = addpt(screen-&gt;r.min, Pt(150, 60));

	s.p[0] = addpt(screen-&gt;r.min, Pt(150,150));
	s.length = 1;
	s.d = UP;
	s.head = 0;
	s.tail = 0;

	redraw(screen);

	einit(Emouse | Ekeyboard);
	t = (150);
	timer = etimer(0, t);

	menu.item = mstr;
	menu.lasthit = 0;
	for(;;) {
		key = event(&amp;e);
		if(key == Emouse) {
			m = e.mouse;
			if(m.buttons &amp; 4) {
				if(emenuhit(3, &amp;m, &amp;menu) == 0) {
					print("mouse exit\n");
					exits(0);
				}
			}
		} else if (key == Ekeyboard) {
			inputk = e.kbdc;
			switch (inputk) {
				case 'h':
					if (s.d != RIGHT)
						s.d = LEFT;
					break;
				case 'j':
					if (s.d != UP)
						s.d = DOWN;
					break;
				case 'k':
					if (s.d != DOWN)
						s.d = UP;
					break;
				case 'l':
					if (s.d != LEFT)
						s.d = RIGHT;
					break;
				case 'q':
					print("Quitter... your score: %d\n", s.length);
					exits(nil);
					break;
				default:
					break;
			}		
		} else if(key == timer) {
			movesnake();
		}
	}	
}
<!-- BEGIN TAIL -->
</pre>
</td></tr></table>
</td></tr></table>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<p style="line-height: 1.2em; margin-left: 1.00in; text-indent: 0.00in; margin-right: 1.00in; margin-top: 0; margin-bottom: 0; text-align: center;">
<span style="font-size: 10pt"></span></p>
<p style="margin-top: 0; margin-bottom: 0.50in"></p>
<p style="margin-top: 0; margin-bottom: 0.33in"></p>
<center><table border="0"><tr>
<td valign="middle"><a href="http://www.alcatel-lucent.com/"><img border="0" src="/plan9/img/logo_ft.gif" alt="Bell Labs" />
</a></td>
<td valign="middle"><a href="http://www.opensource.org"><img border="0" alt="OSI certified" src="/plan9/img/osi-certified-60x50.gif" />
</a></td>
<td><img style="padding-right: 45px;" alt="Powered by Plan 9" src="/plan9/img/power36.gif" />
</td>
</tr></table></center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center>
<span style="font-size: 10pt">(<a href="/plan9/">Return to Plan 9 Home Page</a>)</span>
</center>
<p style="margin-top: 0; margin-bottom: 0.17in"></p>
<center><font size=-1>
<span style="font-size: 10pt"><a href="http://www.lucent.com/copyright.html">Copyright</a></span>
<span style="font-size: 10pt">© 2009 Alcatel-Lucent.</span>
<span style="font-size: 10pt">All Rights Reserved.</span>
<br />
<span style="font-size: 10pt">Comments to</span>
<span style="font-size: 10pt"><a href="mailto:webmaster@plan9.bell-labs.com">webmaster@plan9.bell-labs.com</a>.</span>
</font></center>
</body>
</html>