row=0
col=0
def? __init__(self, row,? col):
self.row=row
self.col=col
def? Copy (self):
Return? Point(row=self.row,? col=self.col)
# Initial frame
Import? pygame
Import? at will
# Initialize
pygame.init()
W=800
H=600
ROW=30
COL=40
Size = (width, height)
window = py game . display . set _ mode(size)
py game . display . set _ caption(' snake ')
bg_color=(255,255,255)
snake _ color =(200200200)
head=Point(row=int(ROW/2),? col=int(COL/2))
head_color=(0, 128, 128)
Snake = [
Point(row=head.row,? col=head.col+ 1),
Point(row=head.row,? col=head.col+2),
Point(row=head.row,? col=head.col+3)
]
# Produce food
def? gen_food():
What time? 1:
pos=Point(row=random.randint(0,ROW- 1),? col=random.randint(0,COL- 1))
#
is_coll=False
# Did you meet a snake?
What if? head.row==pos.row? And then what? head.col==pos.col:
is_coll=True
# Snake body
For what? Snakes? Are you online? Snake:
What if? snake.row==pos.row? And then what? snake.col==pos.col:
is_coll=True
break
What if? Isn't it? is_coll:
break
Return? card reader
# Define coordinates
food=gen_food()
food_color=(255,255,0)
Direct =' left'? # Left, right, up, down
#
def? Rect (point,? Color):
Cell width = width/column
cell_height=H/ROW
Left = point.col * cell width
top=point.row*cell_height
pygame.draw.rect(
Window? Color,
(left,? Top,? Cell _ width,? Cell _ Height)
)
get through
# Game loop
Exit = true
clock=pygame.time.Clock()
What time? Exit:
# Handling events
For what? Events? Are you online? pygame.event.get():
What if? event.type==pygame。 Exit:
Exit = false
Elif? event.type==pygame。 Keys:
What if? event.key==273? Or? event.key== 1 19:
What if? Direct = =' left'? Or? direct=='right ':
Direct =' Up'
Elif? event.key==274? Or? event.key== 1 15:
What if? Direct? ==? Left? Or? Direct? ==? "right":
direct='down '
Elif? event.key==276? Or? event.key==97:
What if? Direct? ==? Get up Or? Direct? ==? Down':
direct='left '
Elif? event.key==275? Or? event.key== 100:
What if? Direct? ==? Get up Or? Direct? ==? Down':
Direct =' Right'
# Eat it
Eat =(head.row==food.row? And then what? head.col==food.col)
# Reproduce food
What if? Eat:
Food? =? gen_food()
# Dispose of the body
# 1. Insert the original head into the snake's head.
snakes.insert(0,head.copy())
#2. Delete the last snake.
What if? Isn't it? Eat:
snakes.pop()
# Move
What if? direct=='left ':
head.col-= 1
Elif? direct=='right ':
head.col+= 1
Elif? direct=='up ':
head.row-= 1
Elif? direct=='down ':
head.row+= 1
# Detection
Death = false
# 1. Suddenly I can't run.
What if? head.col & lt0? Or? head.row & lt0? Or? head.col & gt=COL? Or? Head.row & gt= line:
Death = truth
#2. Hit yourself
For what? Snakes? Are you online? Snake:
What if? head.col==snake.col? And then what? head.row==snake.row:
Death = truth
break
What if? Death:
Print ("dead")
Exit = false
# Render-Draw it
# Background
pygame.draw.rect(window,? bg_color,? (0,0,W,H))
# Snakehead
For what? Snakes? Are you online? Snake:
Rect (snake,? Snake _ color)
Rect (head, head _ color)
Rect (food,? Food _ color)
#
pygame.display.flip()
# Set the frame rate (speed)
clock.tick(8)
# Finishing Work This is a simple version of the snake code. Although the structure is simple, it has complete functions and good playability.