|
From: Zbigniew <ze...@in...> - 2004-05-13 20:09:51
|
Below is code for 2d snake I wrote at the beginning.
This code was ok, but might be useful to find out
how the snake games are done.
In the 3d version I'm looking for somekind of bug
with collision detection.
---
# snake 2d
# wersja 3.5.2004
from visual import *
from string import *
from random import *
skrin=display(width=640, height=480, range=30)
pozycjaX=0
pozycjaY=0
bok=15
kierunek=0
snake=[]
snake.append(box(pos=[pozycjaX,pozycjaY,0], width=0.8, height=0.8,
length=0.8))
klocX=0
klocY=0
while klocX==pozycjaX:
klocX=randrange(-bok,bok)
while klocY==pozycjaY:
klocY=randrange(-bok,bok)
kloc=box(pos=[klocX,klocY,0], width=0.8, height=0.8, length=0.8)
box(pos=[-bok-1,0,0],width=0.8, height=2*(bok+2), length=0.8)
box(pos=[bok+1,0,0], width=0.8, height=2*(bok+2), length=0.8)
box(pos=[0,bok+1,0], width=0.8, height=0.8, length=2*(bok+2))
box(pos=[0,-bok-1,0], width=0.8, height=0.8, length=2*(bok+2))
while 1:
for i in range(0,10):
rate(50)
snake[len(snake)-1].color=[0.5+random(),0.5+random(),0.5+random()]
if skrin.kb.keys:
s = skrin.kb.getkey()
if s=="right":
kierunek=0
if s=="left":
kierunek=1
if s=="up":
kierunek=2
if s=="down":
kierunek=3
break
if kierunek==0:
pozycjaX=pozycjaX+1
if pozycjaX>bok:
break
if kierunek==1:
pozycjaX=pozycjaX-1
if pozycjaX<-bok:
break
if kierunek==2:
pozycjaY=pozycjaY+1
if pozycjaY>bok:
break
if kierunek==3:
pozycjaY=pozycjaY-1
if pozycjaY<-bok:
break
crash=0
for i in range(0,len(snake)):
if snake[i].pos[0]==pozycjaX and snake[i].pos[1]==pozycjaY:
crash=1
if crash==1:
break
snake.append(box(pos=[pozycjaX,pozycjaY,0], width=0.8, height=0.8,
length=0.8))
temp=snake.pop(0)
temp.visible=0
if pozycjaX==klocX and pozycjaY==klocY:
while klocX==pozycjaX:
klocX=randrange(-bok,bok)
while klocY==pozycjaY:
klocY=randrange(-bok,bok)
kloc.pos=[klocX,klocY,0]
snake.append(box(pos=[pozycjaX,pozycjaY,0], width=0.8, height=0.8,
length=0.8))
label(text="game over")
Zbigniew Trzcionkowski <ze...@in...>
|