ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: game

game

๊ณ ์ „์ ์ธ ๋ฑ€ ๊ฒŒ์ž„์„ ์žฌํ˜„ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฑ€์„ ์›€์ง์ผ

๋ฌธ์ œ๋Š” ๊ฐ€๋Šฅํ•œ ์ ์€ ๋ฐ”์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํด๋ž˜์‹ ์Šค๋„ค์ดํฌ ๊ฒŒ์ž„์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์š”๊ตฌ ์‚ฌํ•ญ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๊ฒŒ์ž„์€ ์ผ๋ฐ˜์ ์ธ 2 ์ฐจ์› ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๊ตฌํ˜„ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋ฑ€์€์ง€๋„์˜ ๊ฒฝ๊ณ„ ๋‚ด์—์„œ ํฌ๊ฒŒ ์ž๋ž„ ์ˆ˜ ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค (์ด๊ฒƒ์€ ์‹ค์ œ๋กœ์ง€๋„๋ฅผ ๋„ˆ๋ฌด ์ž‘๊ฒŒ ๋งŒ๋“ค์ง€ ๋ง๊ณ  ์—ฌ๊ธฐ์— ์žฌ๋Ÿ‰์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค).
  • ์‚ฌ์šฉ์ž๋Š” ์›ํ•˜๋Š” ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฑ€์„ ์›€์ง์ผ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฑ€์€ ๋‹ค์‹œ ๋‘ ๋ฐฐ๋กœ ๋Œ์•„์˜ฌ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค (์˜ˆ : ์„œ์ชฝ์œผ๋กœ ๊ฐ€๋ฉด ๋จผ์ € ๋ถ์ชฝ์ด๋‚˜ ๋‚จ์ชฝ์œผ๋กœ ๊ฐ€์ง€ ์•Š๊ณ  ๋™์ชฝ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†์Œ). ๋ฑ€์€ ์œ„, ์•„๋ž˜, ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ (๋ถ์ชฝ, ๋‚จ์ชฝ, ์„œ์ชฝ, ๋™์ชฝ)์˜ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ๋ชจ๋‘ ์ด๋™ํ•  ์ˆ˜ ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฑ€์€ ๊ธธ์ด 1๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. โ€œ์Œ์‹โ€๋ฌผ์ฒด๋ฅผ ๋จน์„ ๋•Œ๋งˆ๋‹ค ๊ธธ์ด๊ฐ€ +1๋กœ ์ž๋ž๋‹ˆ๋‹ค.
  • ์Œ์‹๋ฌผ์€ ๋ฑ€์ด ์ฐจ์ง€ํ•˜๋Š” ๊ณณ ์ด์™ธ์˜ ์žฅ์†Œ์— ๋ฌด์ž‘์œ„๋กœ ๋ฐฐ์น˜๋ฉ๋‹ˆ๋‹ค.
  • ๋ฑ€์ด ๋ฒฝ์— ๋ถ€๋”ช ํžˆ๊ฑฐ๋‚˜ ๋ฒฝ์— ๋ถ€๋”ช ์น˜๋ฉด ๊ฒŒ์ž„์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค
  • ๊ฒŒ์ž„์ด ๋๋‚˜๋ฉด ๋ฌธ์ž โ€œScore : [score]โ€๊ฐ€ ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ [score]๋Š” ๊ฒŒ์ž„ ์ค‘์— ๋จน๋Š” ์Œ์‹ ์ˆ˜์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๊ฒŒ์ž„์ด ๋๋‚ฌ์„ ๋•Œ ๋ฑ€์ด 4 ๊ฐœ์˜ โ€œ์Œ์‹โ€์„ ๋จน์—ˆ๊ณ  (๋”ฐ๋ผ์„œ ๊ธธ์ด๊ฐ€ 5 ์ธ ๊ฒฝ์šฐ) โ€œ์ ์ˆ˜ : 4โ€ณ๊ฐ€ ์ธ์‡„๋ฉ๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ์— ๋ช…์‹œ ์ ์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์—†์Šต๋‹ˆ๋‹ค.

๋‚ด ํ•ด๊ฒฐ์ฑ…์€ 908 ๋ฐ”์ดํŠธ, Python 2.7์ž…๋‹ˆ๋‹ค.

import random as r
import curses as c
def g(s,w,l):
 while 1:
  p=[r.randrange(0,w),r.randrange(0,l)]
  for l in s:
   if l==p:continue
  return p
s=[]
d=[0,1]
p=k=n=0
e=100
v={65:[-1,0],66:[1,0],68:[0,-1],67:[0,1]}
z=c.initscr()
w,l=z.getmaxyx()[0],z.getmaxyx()[1]
c.noecho()
z.clear()
x=g(s,w,l)
s.append([w/2,l/2])
z.nodelay(1)
q=lambda h,i:range(h,len(i))
while k!=101:
 k=z.getch()
 if k in v and not (d[0]==(v[k][0]*-1) and d[1]==(v[k][1]*-1)):d=v[k]
 f=[0,0]
 for i in q(0,s):
  if i == 0:
   f=[s[i][0],s[i][1]]
   s[i][0]+=d[0]
   s[i][1]+=d[1]
  else:s[i],f=f,s[i]
 if s[0]==x:
  n+=1
  s.append(f)
  x=g(s,w,l)
 z.clear()
 if s[0][0]>=w or s[0][1]>=l or s[0][0]<0 or s[0][1]<0:break
 for i in q(1,s):
  if s[0] == s[i]: k = 101
 for i in q(0,s):z.addch(s[i][0],s[i][1],"X")
 z.addch(x[0],x[1],"O")
 z.move(0,0)
 z.refresh()
 if d[1]!=0:c.napms(e/2)
 else:c.napms(e)
c.endwin()
print 'Score: %s'%n


๋‹ต๋ณ€

๋ฃจ๋น„ 1.9 + SDL (341 324 316)

๋‹ค์Œ์€ SDL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•œ Ruby ๋ฒ„์ „์˜ ์ฒซ ๋ฒˆ์งธ ์‹œ๋„์ž…๋‹ˆ๋‹ค. -rsdlrequire ๋ฌธ ๋Œ€์‹  ๋ช…๋ น ์ค„์„ ์‚ฌ์šฉํ•˜์—ฌ SDL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉด 6์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

require'sdl'
f=o=d=3
s=SDL::Screen.open l=32,l,0,0
r=*0..l*l
loop{f==o ?f=(r-$*).sample: $*.shift
/yU/=~"#{e=SDL::Event.poll}"&&(v=e.sym%4)&&d+v!=3&&d=v
$><<"Score #{$*.size}"&&exit if$*.index(n=o+[-1,-l,l,1][d])||n<0||n>=l*l||d%3<1&&n/l!=o/l
$*<<o=n
r.map{|i|s[i%l,i/l]=[[f,*$*].index(i)?0:255]*3}
s.flip
sleep 0.1}

๋ฑ€ ์„ธ๊ทธ๋จผํŠธ์™€ ์Œ์‹ ์กฐ๊ฐ์€ ๊ฒ€์€ ํ”ฝ์…€์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‘œ์‹œ๋˜๋ฉฐ ๊ฒฉ์ž ํฌ๊ธฐ๋Š” ํ˜„์žฌ 32 * 32์ž…๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ œ์–ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค (๋˜๋Š” ๋ชจ๋“  ํ‚ค๋Š” ์‹ค์ œ๋กœ ํ‚ค ์ฝ”๋“œ mod 4๊ฐ€ ๋ฐฉํ–ฅ ๋ฐฐ์—ด [LEFT, UP, DOWN, RIGHT]์„ ์ธ๋ฑ์‹ฑํ•ฉ๋‹ˆ๋‹ค). ํŠนํžˆ ์‚ฌ๋ง ํ™•์ธ IF ๋ฌธ์—์„œ ๊ฐœ์„ ์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

๋‚˜๋Š” ์ด์ „ ๋ฒ„์ „์— ๋น„ํ•ด ์ด๊ฒƒ์„ ํฌ๊ฒŒ ๊ฐœ์„ ํ–ˆ์œผ๋ฉฐ, ํ˜„์žฌ ์งˆ๋ฌธ์˜ ์ •์‹ ๊ณผ ๋” ๋ฐ€์ ‘ํ•˜๊ฒŒ ์ผ์น˜ํ•˜๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์‚ฌ์–‘์„ ์ค€์ˆ˜ํ•˜๊ธฐ ์œ„ํ•ด ์ˆ˜์ •ํ•ด์•ผ ํ•  ํ•œ ๊ฐ€์ง€ ์‚ฌํ•ญ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์Œ์‹์ด ํ˜„์žฌ ๊ผฌ๋ฆฌ ์•ˆ์— ์ƒ์„ฑ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฒฐ์ •๋œ!

๊ฒŒ์ž„์ด ์™„๋ฃŒ๋œ ํ›„ ์ ์ˆ˜๋ฅผ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ธ์‡„ํ•ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

์ž๋ฐ”, 2343 2239

์ •ํ™•ํžˆ ๊ฐ„๊ฒฐํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ๋ชจ๋“  ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ค€์ˆ˜ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

์Šค๋„ค์ดํฌ ํด๋ž˜์Šค

import javax.swing.*;
public class S extends JFrame{
S(){add(new B());setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(320,340);setVisible(true);}
public static void main(String[]a){new S();}}

๋ณด๋“œ ํด๋ž˜์Šค

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class B extends JPanel implements ActionListener{
int W=300;int H=300;int DS=10;int AD=900;int RP=29;int D=140;int x[]=new int[AD];int y[]=new int[AD];int d;int ax;int ay;boolean l=false;boolean r=true;boolean u=false;boolean dn=false;boolean ig=true;Timer t;Image b;Image a;Image h;
B(){addKeyListener(new T());setBackground(Color.black);ImageIcon id=new ImageIcon(this.getClass().getResource("d.png"));b=id.getImage();ImageIcon ia=new ImageIcon(this.getClass().getResource("a.png"));a=ia.getImage();ImageIcon ih=new ImageIcon(this.getClass().getResource("h.png"));h=ih.getImage();setFocusable(true);i();}
void i(){d=3;for(int z=0;z<d;z++){x[z]=50-z*10;y[z]=50;}l();t=new Timer(D,this);t.start();}
public void p(Graphics g){super.paint(g);if(i){g.drawImage(a,ax,ay,this);for(int z=0;z<d;z++){if(z==0)g.drawImage(h,x[z],y[z],this);else g.drawImage(b,x[z],y[z],this);}Toolkit.getDefaultToolkit().sync();g.dispose();}else{g(g);}}
void g(Graphics g){String ms="Score:";Font sm=new Font("Courier",Font.PLAIN,12);FontMetrics me=this.getFontMetrics(sm);g.setColor(Color.white);g.setFont(sm);g.drawString(ms+d,(W-me.stringWidth(ms)),H);}
void c(){if((x[0]==ax)&&(y[0]==ay)){d++;l();}}
void m(){for(int z=d;z>0;z--){x[z]=x[(z-1)]; y[z]=y[(z-1)];}if(l){x[0]-=DS;}if (r){x[0]+=DS;}if(u){y[0]-=DS;}if(dn){y[0]+=DS;}}
void cc(){for(int z=d;z>0;z--){if((z>4)&&(x[0]==x[z])&&(y[0]==y[z])){ig=false;}}if(y[0]>H){ig=false;}if(y[0]<0){ig=false;}if(x[0]> W){ig=false;}if(x[0]<0){ig=false;}}
void l(){int r=(int)(Math.random()*RP);ax=((r*DS));r=(int)(Math.random()*RP);ay=((r*DS));}
public void actionPerformed(ActionEvent e){if(ig){c();cc();m();}repaint();}
class T extends KeyAdapter{public void keyPressed(KeyEvent e){int k=e.getKeyCode();if((k==KeyEvent.VK_LEFT)&&(!r)){l=true;u=false;dn=false;}if((k==KeyEvent.VK_RIGHT)&&(!l)){r=true;u=false;dn=false;}if((k==KeyEvent.VK_UP)&&(!dn)){u=true;r=false;l=false;}if((k==KeyEvent.VK_DOWN)&&(!u)){dn=true;r=false;l=false;}}}}

์Šคํฌ๋ฆฐ ์ƒท


ํ•ด์„ค

์–ผ๋งˆ ์ „ ๋‚˜๋Š” ์ž๋ฐ”๋กœ ํด๋ž˜์‹ 2D ๊ฒŒ์ž„์„ ๋งŒ๋“œ๋Š” ํŠœํ† ๋ฆฌ์–ผ์„ ์ œ๊ณตํ•˜๋Š” zetcode ๋ผ๋Š” ์›น ์‚ฌ์ดํŠธ๋ฅผ ๋ฐฉ๋ฌธํ–ˆ๋‹ค . ์ œ๊ณต๋œ ์ฝ”๋“œ ๋Š” ์Šค๋„ค์ดํฌ ๊ฒŒ์ž„์„ ์œ„ํ•ด ์ œ๊ณต๋œ ํŠœํ† ๋ฆฌ์–ผ์˜ ์˜ํ–ฅ์„ ๋งŽ์ด๋ฐ›์Šต๋‹ˆ๋‹ค โ€ฆ์ด ์‹œ์ ์—์„œ ๋ฐฉ๊ธˆ ํด๋ž˜์‹ ๊ฒŒ์ž„์„ ์ฝ”๋”ฉํ•˜๊ธฐ ์‹œ์ž‘ํ–ˆ๊ณ  ํŠœํ† ๋ฆฌ์–ผ์„ โ€˜Tโ€™๋กœ ๋”ฐ๋ž์Šต๋‹ˆ๋‹ค.

์‚ฌ๋žŒ๋“ค์ด ๊ฒŒ์ž„์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‚˜์ค‘์— ํŽธ์ง‘ํ•˜๊ณ  ์‹คํ–‰ ํŒŒ์ผ์— ๋Œ€ํ•œ ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


EDITS

  • 9/9/12 : ๋ฆฌ์†Œ์Šค ํด๋”์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ œ๋Œ€๋กœ๋กœ๋“œ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‚ด ์ฝ”๋“œ๊ฐ€ ์ž‘๋™ํ•˜๊ณ  ์งˆ๋ฌธ์˜ ๋ชจ๋“  ๊ธฐ์ค€์„ ์ถฉ์กฑํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ฆ๋ช…ํ•˜๊ธฐ ์œ„ํ•ด์ด ๋ฌธ์ œ๋ฅผ ๊ณ„์† ์ง„ํ–‰ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • 9/11/12 : ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์—์„œ ์‚ฌ์ง„์„๋กœ๋“œํ•˜๋Š” ์ž‘์—…์„ ๊ณ„์†ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ZetCode ํŠœํ† ๋ฆฌ์–ผ์—์„œ ์ œ๊ณต ํ•œ ๊ทธ๋ฆผ์„ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋‹ต๋ณ€

๊ฐ•ํƒ€ : 537 533 507 ์ž

C=$COLUMNS;L=$LINES;D=-1;c=9;r=9;z=(9\ 9);l=;h=1;v=;s=1;d=1
t(){ echo -en "\e[$2;$1H$3";}
b(){ ((f=RANDOM%C+1));((g=RANDOM%L+1));for i in "${z[@]}";do [[ $f\ $g = $i ]]&&b;done;t $f $g F;}
echo $'\e[2J';b
while :;do
read -sn1 -t.1 k
case $k in
w|s)((h))&&h=&&v=${D:$k};;
a|d)((v))&&v=&&h=${D:$k};;
esac
((c+=h));((r+=v))
((c==f&&r==g&&++l))&&b
((c<1||r<1||c>C||r>L))&&break
for i in "${z[@]}";do [[ $c\ $r = $i ]]&&break 2;done
t ${z[-1]} \ ;t $c $r X
z=($c\ $r "${z[@]::l}")
done
echo $'\e[2J\e[H'Score: $l

$COLUMNS๋ฐ $LINES์…ธ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ sourced๋ฅผ ์‹คํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค . snake.sh. ๋ฑ€์€ w/ a/ s/ dํ‚ค๋กœ ์ œ์–ด ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค .

clearํ™”๋ฉด์„ ์ง€์šฐ๋ ค๋ฉด ์„ ์‚ฌ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ 493 ์ž๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์ง€๋งŒ bash์™ธ๋ถ€ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์ˆœ์ˆ˜ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธํ•ฉ๋‹ˆ๋‹ค .


๋‹ต๋ณ€

ํŒŒ์ด์ฌ 2.7 : 869 ๊ฐœ 816 818 817 816 ๋ฌธ์ž

์ง€๋‚œ ๋ช‡ ์‹œ๊ฐ„ ๋™์•ˆ ํ•จ๊ป˜ ํ•ดํ‚นํ–ˆ์Šต๋‹ˆ๋‹ค. ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ถฉ์กฑํ•ด์•ผํ•˜๋ฉฐ mjgpy3์˜ ์†”๋ฃจ์…˜๋ณด๋‹ค ๋ช‡ ์ž ๋” ์งง์Šต๋‹ˆ๋‹ค (์—ด์‹ฌํžˆ ์‹œ๋„ํ–ˆ์ง€๋งŒ ๋” ์งง๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์—†์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ํ”ผ๊ณคํ•ฉ๋‹ˆ๋‹ค). ๋†€๋ž๊ฒŒ๋„, pygame๊ณผ ๊ฐ™์€ ๊ฒŒ์ž„ ๊ฐœ๋ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŒŒ์ด์ฌ ๋ฑ€์ด ํ›จ์”ฌ ์งง์•„์ง€์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ์งง๊ฒŒ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•œ ์ œ์•ˆ๊ณผ ํŒ์€ ๋†’์ด ํ‰๊ฐ€๋ฉ๋‹ˆ๋‹ค. ๋‚˜๋Š” ๊ทธ๊ฒƒ์ด ๋„ˆ๋ฌด ๋น„๋ฐ€์Šค๋Ÿฝ์ง€ ์•Š๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.

๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

import pygame as p
from random import randint as r
p.init();l=20
c=p.time.Clock()
dp=p.display;w=p.display.set_mode((500,)*2)
C=p.Color;b=C(0,0,0);g=C(0,99,0)
D=(0,1);U=(0,-1);L=(-1,0);R=(1,0)
S=[R];d=R;n=[]
O=lambda t:{U:D,R:L,D:U,L:R}[t]
def Q(e):print "Score: %i"%(len(S)-1);p.quit()
def K(e):global d;_={276:L,273:U,274:D,275:R}.get(e.key,(0,0));d=not _==O(d) and _ or d
def N(S):[p.draw.rect(w,g,[x[0]*l,x[1]*l,l,l]) for x in S+n]
def M():n=(r(0,24),r(0,24));return n not in S and n or M()
A=lambda s,o:tuple(x+y for x,y in zip(s,o))
n=[M()]
while True:
 w.fill(b);[{12:Q,2:K}.get(e.type,lambda e:e)(e) for e in p.event.get()]
 if not (0<=S[-1][0]<25 and 0<=S[-1][1]<25) or A(S[-1],d) in S: Q(e)
 if A(S[-1],d) in n: S.append(A(S[-1],d));n=[M()]
 else: S.append(A(S[-1],d));S.pop(0)
 N(S);dp.update();c.tick(6)

ํŽธ์ง‘ : ๋‚˜๋Š” ๊ทธ๊ฒƒ์„ 816 ๋ฐ”์ดํŠธ๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ™‚ ์ ์ˆ˜๋ฅผ ์ˆ˜์ •

EDIT2 : ์‹ค์ˆ˜๋กœ ์ž˜๋ชป๋œ ๋ฒ„์ „์„ ๋ถ™์—ฌ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์€ ์ฃผ์„ ์ฒ˜๋ฆฌ ๋œ ๋ฒ„์ „์ž…๋‹ˆ๋‹ค.

import pygame as p
from random import randint as r

# initialize pygame
p.init()

# the game consists of 25*25 blocks,with each block 20*20 pixels
l=20

# initialize the main loop clock
c=p.time.Clock()

# open the window
dp=p.display;w=p.display.set_mode((500,)*2)

# define black and green colors
C=p.Color;b=C(0,0,0);g=C(0,99,0)

# Directions of the snake: down, up, left, right
D=(0,1);U=(0,-1);L=(-1,0);R=(1,0)

# S is the snake, d is the current direction and n is the array of foods
S=[R];d=R;n=[]

# get the opposite direction of a direction to forbid double backing
O=lambda t:{U:D,R:L,D:U,L:R}[t]

# print the score and quit
def Q(e):print "Score: %i"%(len(S)-1);p.quit()

# update the direction (this is a key press handler)
def K(e):global d;_={276:L,273:U,274:D,275:R}.get(e.key,(0,0));d=not _==O(d) and _ or d

# draw the snake and food boxes
def N(S):[p.draw.rect(w,g,[x[0]*l,x[1]*l,l,l]) for x in S+n]

# place new food on the map not colliding with the snake
def M():n=(r(0,24),r(0,24));return n not in S and n or M()

# A((1,1), (-2, 1)) -> (-1,2)
A=lambda s,o:tuple(x+y for x,y in zip(s,o))

# initialize food array
n=[M()]

while True:
 # fill the screen black
 w.fill(b)
 # get quit or key press events and execute the event handlers
 [{12:Q,2:K}.get(e.type,lambda e:e)(e) for e in p.event.get()]

 # check if snake hits map boundaries or itself
 if not (0<=S[-1][0]<25 and 0<=S[-1][1]<25) or A(S[-1],d) in S: Q(e)

 # check if snake is eating food at the moment and append one to the snake's length
 if A(S[-1],d) in n: S.append(A(S[-1],d));n=[M()]

 # move the snake in the current direction
 else: S.append(A(S[-1],d));S.pop(0)

 # draw the map and limit the main loop to 6 frames per second
 N(S);dp.update();c.tick(6)

๋‹ต๋ณ€


์ด ๊ธ€์€ ์ฝ”๋”ฉ ์นดํ…Œ๊ณ ๋ฆฌ์— ๋ถ„๋ฅ˜๋˜์—ˆ๊ณ  , ํƒœ๊ทธ๊ฐ€ ์žˆ์œผ๋ฉฐ ๋‹˜์— ์˜ํ•ด ์— ์ž‘์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.