|  | 
| 2 | 2 | import random | 
| 3 | 3 | import sys | 
| 4 | 4 | from pygame.locals import * | 
| 5 |  | -def collide(x1,x2,y1,y2,wh): | 
| 6 |  | -	w1=20; | 
| 7 |  | -	w2=wh; | 
| 8 |  | -	h2=wh; | 
| 9 |  | -	h1=20; | 
| 10 |  | -	if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2: | 
| 11 |  | -		return True | 
| 12 |  | -	else: | 
| 13 |  | -		return False | 
|  | 5 | + | 
|  | 6 | + | 
|  | 7 | +def collide(x1, x2, y1, y2, wh): | 
|  | 8 | +    w1 = 20 | 
|  | 9 | +    w2 = wh | 
|  | 10 | +    h2 = wh | 
|  | 11 | +    h1 = 20 | 
|  | 12 | +    if x1+w1 > x2 and x1 < x2+w2 and y1+h1 > y2 and y1 < y2+h2: | 
|  | 13 | +        return True | 
|  | 14 | +    else: | 
|  | 15 | +        return False | 
|  | 16 | + | 
|  | 17 | + | 
| 14 | 18 | def die(screen, score): | 
| 15 |  | -	f=pygame.font.SysFont('Monospace',30); | 
| 16 |  | -	t=f.render('YOUR SCORE IS : '+str(score),True,(0,0,0)); | 
| 17 |  | -	screen.blit(t,(10,270)); | 
| 18 |  | -	pygame.display.update(); | 
| 19 |  | -	pygame.time.wait(2000); | 
| 20 |  | -	sys.exit(0) | 
| 21 |  | -xs=[290,290,290,290,290]; | 
| 22 |  | -ys=[290,270,250,230,210]; | 
| 23 |  | -dirs=0; | 
| 24 |  | -score=0; | 
| 25 |  | -applepos=(random.randint(0,590),random.randint(0,590)); | 
| 26 |  | -pygame.init(); | 
| 27 |  | -s=pygame.display.set_mode((600,600)); | 
| 28 |  | -pygame.display.set_caption('SNAKE'); | 
| 29 |  | -appleimage=pygame.Surface((10,10)); | 
| 30 |  | -appleimage.fill((0,255,0)); | 
| 31 |  | -img=pygame.Surface((20,20)); | 
| 32 |  | -img.fill((255,0,0)); | 
| 33 |  | -f=pygame.font.SysFont('Monospace',20); | 
| 34 |  | -clock=pygame.time.Clock() | 
|  | 19 | +    f = pygame.font.SysFont('Monospace', 30) | 
|  | 20 | +    t = f.render('YOUR SCORE IS : '+str(score), True, (0, 0, 0)) | 
|  | 21 | +    screen.blit(t, (10, 270)) | 
|  | 22 | +    pygame.display.update() | 
|  | 23 | +    pygame.time.wait(2000) | 
|  | 24 | +    sys.exit(0) | 
|  | 25 | + | 
|  | 26 | + | 
|  | 27 | +xs = [290, 290, 290, 290, 290] | 
|  | 28 | +ys = [290, 270, 250, 230, 210] | 
|  | 29 | +dirs = 0 | 
|  | 30 | +score = 0 | 
|  | 31 | +applepos = (random.randint(0, 590), random.randint(0, 590)) | 
|  | 32 | +pygame.init() | 
|  | 33 | +s = pygame.display.set_mode((600, 600)) | 
|  | 34 | +pygame.display.set_caption('SNAKE') | 
|  | 35 | +appleimage = pygame.Surface((10, 10)) | 
|  | 36 | +appleimage.fill((0, 255, 0)) | 
|  | 37 | +img = pygame.Surface((20, 20)) | 
|  | 38 | +img.fill((255, 0, 0)) | 
|  | 39 | +f = pygame.font.SysFont('Monospace', 20) | 
|  | 40 | +clock = pygame.time.Clock() | 
| 35 | 41 | while True: | 
| 36 |  | -	clock.tick(10) | 
| 37 |  | -	for e in pygame.event.get(): | 
| 38 |  | -		if e.type==QUIT: | 
| 39 |  | -			sys.exit(0) | 
| 40 |  | -		elif e.type==KEYDOWN: | 
| 41 |  | -			if e.key==K_UP and dirs!=0: | 
| 42 |  | -				dirs=2 | 
| 43 |  | -			elif e.key==K_DOWN and dirs!=2: | 
| 44 |  | -				dirs=0 | 
| 45 |  | -			elif e.key==K_LEFT and dirs!=1: | 
| 46 |  | -				dirs=3 | 
| 47 |  | -			elif e.key==K_RIGHT and dirs!=3: | 
| 48 |  | -				dirs=1 | 
| 49 |  | -	i=len(xs)-1 | 
| 50 |  | -	while i>=2: | 
| 51 |  | -		if collide(xs[0],xs[i],ys[0],ys[i],20): | 
| 52 |  | -			die(s,score) | 
| 53 |  | -		i-=1 | 
| 54 |  | -	if collide(xs[0],applepos[0],ys[0],applepos[1],10): | 
| 55 |  | -		score+=1; | 
| 56 |  | -		xs.append(700); | 
| 57 |  | -		ys.append(700); | 
| 58 |  | -		applepos=(random.randint(0,590),random.randint(0,590)) | 
| 59 |  | -	if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: | 
| 60 |  | -		die(s,score) | 
| 61 |  | -	i=len(xs)-1 | 
| 62 |  | -	while i >= 1: | 
| 63 |  | -		xs[i]=xs[i-1]; | 
| 64 |  | -		ys[i]=ys[i-1]; | 
| 65 |  | -		i-=1 | 
| 66 |  | -	if dirs==0: | 
| 67 |  | -		ys[0]+=20 | 
| 68 |  | -	elif dirs==1: | 
| 69 |  | -		xs[0]+=20 | 
| 70 |  | -	elif dirs==2: | 
| 71 |  | -		ys[0]-=20 | 
| 72 |  | -	elif dirs==3: | 
| 73 |  | -		xs[0]-=20	 | 
| 74 |  | -	s.fill((255,255,255))	 | 
| 75 |  | -	for i in range(0,len(xs)): | 
| 76 |  | -		s.blit(img,(xs[i],ys[i])) | 
| 77 |  | -	s.blit(appleimage,applepos); | 
| 78 |  | -	t=f.render(str(score),True,(0,0,0)); | 
| 79 |  | -	s.blit(t,(10,10)); | 
| 80 |  | -	pygame.display.update() | 
|  | 42 | +    clock.tick(10) | 
|  | 43 | +    for e in pygame.event.get(): | 
|  | 44 | +        if e.type == QUIT: | 
|  | 45 | +            sys.exit(0) | 
|  | 46 | +        elif e.type == KEYDOWN: | 
|  | 47 | +            if e.key == K_UP and dirs != 0: | 
|  | 48 | +                dirs = 2 | 
|  | 49 | +            elif e.key == K_DOWN and dirs != 2: | 
|  | 50 | +                dirs = 0 | 
|  | 51 | +            elif e.key == K_LEFT and dirs != 1: | 
|  | 52 | +                dirs = 3 | 
|  | 53 | +            elif e.key == K_RIGHT and dirs != 3: | 
|  | 54 | +                dirs = 1 | 
|  | 55 | +    i = len(xs)-1 | 
|  | 56 | +    while i >= 2: | 
|  | 57 | +        if collide(xs[0], xs[i], ys[0], ys[i], 20): | 
|  | 58 | +            die(s, score) | 
|  | 59 | +        i -= 1 | 
|  | 60 | +    if collide(xs[0], applepos[0], ys[0], applepos[1], 10): | 
|  | 61 | +        score += 1 | 
|  | 62 | +        xs.append(700) | 
|  | 63 | +        ys.append(700) | 
|  | 64 | +        applepos = (random.randint(0, 590), random.randint(0, 590)) | 
|  | 65 | +    if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: | 
|  | 66 | +        die(s, score) | 
|  | 67 | +    i = len(xs)-1 | 
|  | 68 | +    while i >= 1: | 
|  | 69 | +        xs[i] = xs[i-1] | 
|  | 70 | +        ys[i] = ys[i-1] | 
|  | 71 | +        i -= 1 | 
|  | 72 | +    if dirs == 0: | 
|  | 73 | +        ys[0] += 20 | 
|  | 74 | +    elif dirs == 1: | 
|  | 75 | +        xs[0] += 20 | 
|  | 76 | +    elif dirs == 2: | 
|  | 77 | +        ys[0] -= 20 | 
|  | 78 | +    elif dirs == 3: | 
|  | 79 | +        xs[0] -= 20 | 
|  | 80 | +    s.fill((255, 255, 255)) | 
|  | 81 | +    for i in range(0, len(xs)): | 
|  | 82 | +        s.blit(img, (xs[i], ys[i])) | 
|  | 83 | +    s.blit(appleimage, applepos) | 
|  | 84 | +    t = f.render(str(score), True, (0, 0, 0)) | 
|  | 85 | +    s.blit(t, (10, 10)) | 
|  | 86 | +    pygame.display.update() | 
0 commit comments