Snake Py

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

import pygame,random,time,sys

pygame.init()

m = 20 # kích thước chiều cao và chiều rộng


# tạo cửa sổ
gameSurface = pygame.display.set_mode((735,475))
pygame.display.set_caption('Snake Game!')
# tạo các màu sắc
red = pygame.Color(255,0,0)
blue = pygame.Color(65,105,255)
black = pygame.Color(0,0,0)
white = pygame.Color(255,255,255)
gray = pygame.Color(128,128,128)
# hàm gameover
def game_over():
gameSurface.fill(white)
pygame.draw.rect(gameSurface, gray, (10, 10, 715, 455), 2)
gfont = pygame.font.SysFont('consolas',40)
gsurf = gfont.render('Game over!', True, red)
grect = gsurf.get_rect()
grect.midtop = (360,150)
gameSurface.blit(gsurf,grect)
gsurf1 = gfont.render('Press C-Play Again or Q-Quit!', True, red)
grect1 = gsurf.get_rect()
grect1.midtop = (180, 300)
gameSurface.blit(gsurf1, grect1)
# hàm Game Snake
def Snake():
# khai báo biến
snakepos = [100, 60]
snakebody = [[100, 60], [80, 60], [60, 60]] # m = 20
foodx = random.randrange(1, 71)
foody = random.randrange(1, 45)
if foodx % 2 != 0: foodx += 1
if foody % 2 != 0: foody += 1
foodpos = [foodx * 10, foody * 10]
foodflat = True
direction = 'RIGHT'
changeto = direction
score = 0
over = False
close = False

while over == False:


while close == True:
game_over()
# show score
sfont1 = pygame.font.SysFont('consolas', 20)
ssurf1 = sfont1.render('Score: {0}'.format(score),True, black)
srect1 = ssurf1.get_rect()
srect1.midtop = (360, 230) # điểm khi game over
gameSurface.blit(ssurf1, srect1)
pygame.display.flip() # cập nhật màn hình
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
over = True
close = False
if event.key == pygame.K_c:
Snake()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.time.delay(200) # tốc độ chơi
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# xử lý phím
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
changeto = 'RIGHT'
if event.key == pygame.K_LEFT:
changeto = 'LEFT'
if event.key == pygame.K_UP:
changeto = 'UP'
if event.key == pygame.K_DOWN:
changeto = 'DOWN'
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.evet.Event(pygame.QUIT))
# xử lý hướng đi
if changeto == 'RIGHT' and not direction == 'LEFT':
direction = 'RIGHT'
if changeto == 'LEFT' and not direction == 'RIGHT':
direction = 'LEFT'
if changeto == 'UP' and not direction == 'DOWN':
direction = 'UP'
if changeto == 'DOWN' and not direction == 'UP':
direction = 'DOWN'
# cập nhật vị trí mới
if direction == 'RIGHT':
snakepos[0] += m
if direction == 'LEFT':
snakepos[0] -= m
if direction == 'UP':
snakepos[1] -= m
if direction == 'DOWN':
snakepos[1] += m
# ăn mồi body dài ra
snakebody.insert(0,list(snakepos))
if snakepos[0] == foodpos[0] and snakepos[1] == foodpos[1]:
score += 1
foodflat = False
else:
snakebody.pop()
# sản sinh mồi
if foodflat == False:
foodx = random.randrange(1,71)
foody = random.randrange(1,45)
if foodx %2 != 0: foodx += 1
if foody %2 != 0: foody += 1
foodpos = [foodx * 10, foody * 10]
foodflat = True
# cập nhật lên cửa sổ
gameSurface.fill(white)
for pos in snakebody:
pygame.draw.rect(gameSurface,black,pygame.Rect(pos[0],pos[1],m,m))
pygame.draw.rect(gameSurface,red,pygame.Rect(foodpos[0],foodpos[1],m,m))
# xử lý di chuyển đụng 4 cạnh biên
if snakepos[0] > 710 or snakepos[0] < 10:
close = True
if snakepos[1] > 450 or snakepos[1] < 10:
close = True
# xử lý tự ăn chính mình
for b in snakebody[1:]:
if snakepos[0] == b[0] and snakepos[1] == b[1]:
close = True
# đường viền
pygame.draw.rect(gameSurface,gray,(10,10,715,455),2)
# điểm khi bắt đầu và chơi
sfont = pygame.font.SysFont('consolas', 20)
ssurf = sfont.render('Score: {0}'.format(score), True, black)
srect = ssurf.get_rect()
srect.midtop = (70, 20)
gameSurface.blit(ssurf, srect)
pygame.display.flip()
pygame.quit()
sys.exit()
Snake()

You might also like