0% found this document useful (0 votes)
1 views3 pages

Snake Pro Code

The document contains the source code for a Python game called 'Snake Pro' built using the Pygame library. It includes functionalities for initializing the game, rendering graphics for the snake and food, handling user input, and managing game states such as playing and game over. The code also implements a scoring system and a menu interface for starting and exiting the game.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

Snake Pro Code

The document contains the source code for a Python game called 'Snake Pro' built using the Pygame library. It includes functionalities for initializing the game, rendering graphics for the snake and food, handling user input, and managing game states such as playing and game over. The code also implements a scoring system and a menu interface for starting and exiting the game.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SNAKE PRO - Python Source Code

import pygame, random, sys, math

# --- Init & Fullscreen ---


[Link]()
screen = [Link].set_mode((0,0), [Link])
W,H = screen.get_size()
[Link].set_caption("Snake Pro")
clock = [Link]()

# --- Grid ---


CELL=36; COLS=W//CELL; ROWS=H//CELL

# --- Colors: Dark theme with colored snake & apple ---
BG=(10,10,10); GRID=(22,22,22); WHITE=(240,240,240)
HEAD=(60,200,80); TAIL=(20,80,40); DARK=(10,10,10) # green snake
FOOD=(220,40,40); FOOD_IN=(255,100,80); GOLD=(255,215,0)
SHADOW=(5,5,5); STEM=(80,50,20); LEAF=(50,180,60)

# --- Fonts ---


big=[Link]("consolas",70,bold=True)
med=[Link]("consolas",38,bold=True)
sml=[Link]("consolas",26,bold=True)
tiny=[Link]("consolas",20)

# --- Pre-render grid texture once ---


grid_surf=[Link]((W,H))
grid_surf.fill(BG)
for x in range(0,W,CELL): [Link](grid_surf,GRID,(x,0),(x,H))
for y in range(0,H,CELL): [Link](grid_surf,GRID,(0,y),(W,y))
# Subtle noise texture on grid
for _ in range(2000):
px,py=[Link](0,W-1),[Link](0,H-1)
v=[Link](18,28)
grid_surf.set_at((px,py),(v,v,v))

# --- 3D Pixel Button (black/white style) ---


def draw_btn(rect,label,fnt=med):
x,y,w,h=rect.x,rect.y,[Link],[Link]
[Link](screen,SHADOW,(x+7,y+7,w,h)) # 3D shadow
[Link](screen,WHITE,(x,y,w,h)) # white face
[Link](screen,DARK,(x,y,w,h),3) # black border
t=[Link](label,True,DARK)
[Link](t,([Link]-t.get_width()//2,[Link]-t.get_height()//2))

# --- Dark overlay for menu/gameover ---


def draw_overlay():
s=[Link]((W,H),[Link])
[Link]((0,0,0,180)); [Link](s,(0,0))

# --- Snake: green gradient + scales texture + eyes ---


def draw_snake(snake):
n=max(len(snake)-1,1)
for i,(x,y) in enumerate(snake):
ratio=i/n
r=int(HEAD[0]+(TAIL[0]-HEAD[0])*ratio)
g=int(HEAD[1]+(TAIL[1]-HEAD[1])*ratio)
b=int(HEAD[2]+(TAIL[2]-HEAD[2])*ratio)
rect=[Link](x*CELL+3,y*CELL+3,CELL-6,CELL-6)
[Link](screen,(r,g,b),rect) # body
[Link](screen,(r-20,g-30,b-10),rect,2) # dark outline
# Scale texture: small darker ellipse on each segment
[Link](screen,(r-15,g-25,b-10),(x*CELL+7,y*CELL+7,CELL-16,CELL-16))
# Highlight shine top-left
[Link](screen,(min(r+60,255),min(g+60,255),min(b+40,255)),
(x*CELL+5,y*CELL+5,8,6))
if i==0: # eyes on head
[Link](screen,WHITE,(x*CELL+9,y*CELL+9),5)
[Link](screen,WHITE,(x*CELL+CELL-9,y*CELL+9),5)
[Link](screen,DARK,(x*CELL+9,y*CELL+9),3)
[Link](screen,DARK,(x*CELL+CELL-9,y*CELL+9),3)
[Link](screen,WHITE,(x*CELL+10,y*CELL+8),1) # eye shine
[Link](screen,WHITE,(x*CELL+CELL-8,y*CELL+8),1)

# --- Apple: red body + stem + leaf + shine ---


def draw_food(food,t):
fx,fy=food
pulse=int(abs([Link](t*0.06))*3)
cx,cy=fx*CELL+CELL//2,fy*CELL+CELL//2+3
r=CELL//2-2+pulse
# Apple shadow
[Link](screen,(30,10,10),(cx-r+3,cy+r-4,r*2-4,6))
# Apple body (red)
[Link](screen,FOOD,(cx,cy),r)
# Apple darker base shading
[Link](screen,(160,20,20),(cx-r+4,cy,r*2-8,r))
# Apple bright highlight
[Link](screen,(255,120,120),(cx-r//2,cy-r//2,r//2+2,r//3+2))
# Specular dot
[Link](screen,(255,200,200),(cx-r//3,cy-r//3),3)
# Stem (brown line)
[Link](screen,STEM,(cx,cy-r),(cx+3,cy-r-8),3)
# Leaf (small green ellipse)
[Link](screen,LEAF,(cx+2,cy-r-9,10,6))
[Link](screen,(80,220,80),(cx+3,cy-r-8,6,3)) # leaf shine

# --- Score panel top-left box ---


def draw_score(score,hi):
box=[Link](10,10,220,75)
[Link](screen,SHADOW,(17,17,220,75))
[Link](screen,WHITE,box)
[Link](screen,DARK,box,2)
[Link]([Link](f"SCORE {score:04d}",True,DARK),(20,18))
[Link]([Link](f"BEST {hi:04d}",True,(80,80,80)),(20,50))

# --- Game logic ---


def new_game():
s=[(COLS//2,ROWS//2),(COLS//2-1,ROWS//2),(COLS//2-2,ROWS//2)]
return s,(1,0),place_food(s),0

def place_food(snake):
while True:
f=([Link](1,COLS-2),[Link](1,ROWS-2))
if f not in snake: return f

snake,direction,food,score=new_game()
hi=0; state="MENU"; t=0

# Button rects
start_btn=[Link](W//2-130,H//2+20,260,65)
exit_btn1=[Link](W//2-130,H//2+100,260,65) # menu exit
retry_btn=[Link](W//2-130,H//2+60,260,65)
exit_btn2=[Link](W//2-130,H//2+140,260,65) # gameover exit
last_move=0

# --- Main Loop ---


while True:
t+=1
mx,my=[Link].get_pos()
for e in [Link]():
if [Link]==[Link]: [Link](); [Link]()
if [Link]==[Link]:
if [Link]==pygame.K_ESCAPE: [Link](); [Link]()
if state=="PLAYING":
if [Link] in (pygame.K_UP,pygame.K_w) and direction!=(0,1): direction=(0,-1)
if [Link] in (pygame.K_DOWN,pygame.K_s) and direction!=(0,-1): direction=(0,1)
if [Link] in (pygame.K_LEFT,pygame.K_a) and direction!=(1,0): direction=(-1,0)
if [Link] in (pygame.K_RIGHT,pygame.K_d) and direction!=(-1,0): direction=(1,0)
if [Link]==[Link]:
if state=="MENU" and start_btn.collidepoint(mx,my): state="PLAYING"
if state=="MENU" and exit_btn1.collidepoint(mx,my): [Link](); [Link]()
if state=="DEAD" and retry_btn.collidepoint(mx,my):
snake,direction,food,score=new_game(); state="PLAYING"
if state=="DEAD" and exit_btn2.collidepoint(mx,my): [Link](); [Link]()

# --- Move snake ---


if state=="PLAYING":
spd=max(60,130-score*2) # speed up with score
now=[Link].get_ticks()
if now-last_move>spd:
last_move=now
hx,hy=snake[0]; dx,dy=direction
nh=(hx+dx,hy+dy)
if nh in snake or not(0<=nh[0]<COLS) or not(0<=nh[1]<ROWS):
if score>hi: hi=score
state="DEAD"
else:
[Link](0,nh)
if nh==food: score+=10; food=place_food(snake)
else: [Link]()

# --- Draw ---


[Link](grid_surf,(0,0)) # textured grid bg
draw_snake(snake)
draw_food(food,t)
draw_score(score,hi)

if state=="MENU":
draw_overlay()
ti=[Link]("SNAKE PRO",True,WHITE)
sh=[Link]("SNAKE PRO",True,(50,50,50))
[Link](sh,(W//2-sh.get_width()//2+4,H//3+4)) # title shadow
[Link](ti,(W//2-ti.get_width()//2,H//3)) # title
draw_btn(start_btn,"▶ START")
draw_btn(exit_btn1,"✕ EXIT")

if state=="DEAD":
draw_overlay()
ti=[Link]("GAME OVER",True,WHITE)
sh=[Link]("GAME OVER",True,(50,50,50))
[Link](sh,(W//2-sh.get_width()//2+4,H//3+4))
[Link](ti,(W//2-ti.get_width()//2,H//3))
sc=[Link](f"SCORE: {score}",True,GOLD)
[Link](sc,(W//2-sc.get_width()//2,H//3+90))
draw_btn(retry_btn,"↺ RETRY")
draw_btn(exit_btn2,"✕ EXIT")

[Link]()
[Link](60)

You might also like