0% found this document useful (0 votes)
380 views

Snake Game On Python

This document contains code for a snake game created using the Pygame library in Python. It defines variables for the screen dimensions, block and speed sizes, and colors. Functions are created to display the score, draw the snake as a list of rectangles, and show messages. The main game loop handles events, key presses to move the snake, collision detection, drawing elements, updating the score, growing the snake for eating food, and resetting on collision. Random food is generated and the game restarts if the snake hits the edge or itself.

Uploaded by

camelia imaan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
380 views

Snake Game On Python

This document contains code for a snake game created using the Pygame library in Python. It defines variables for the screen dimensions, block and speed sizes, and colors. Functions are created to display the score, draw the snake as a list of rectangles, and show messages. The main game loop handles events, key presses to move the snake, collision detection, drawing elements, updating the score, growing the snake for eating food, and resetting on collision. Random food is generated and the game restarts if the snake hits the edge or itself.

Uploaded by

camelia imaan
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame #import pygame supaya boleh guna

import time
import random

pygame.init()

white = (255, 255, 255) #ikut rgb, ni untuk nk input colour supaya tu colour yg
kite nak
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (255, 105, 97)
green = (190, 229, 176)
blue = (174, 198, 207)
pink = (255, 192, 203) #pink cantik

dis_width = 600 #ni display window pny lebar


dis_height = 400 #ni display window pny ketinggian

dis = pygame.display.set_mode((dis_width, dis_height)) #display window


pygame.display.set_caption('Snake Game by Amal, Anis & Camelia') #display caption

clock = pygame.time.Clock()

snake_block = 10 #kebesaran snake


snake_speed = 15 #kelajuan snake

font_style = pygame.font.SysFont("bahnschrift", 25) #font


score_font = pygame.font.SysFont("comicsansms", 35) #font

def Your_score(score):
value = score_font.render("Your Score: " + str(score), True, yellow) #yellow tu
colour display score
dis.blit(value, [0, 0])

def our_snake(snake_block, snake_list):


for x in snake_list:
pygame.draw.rect(dis, pink, [x[0], x[1], snake_block, snake_block]) #build
snake

def message(msg, color):


mesg = font_style.render(msg, True, color)
dis.blit(mesg, [dis_width / 6, dis_height / 3])

def gameLoop():
game_over = False
game_close = False

x1 = dis_width / 2 #badan snake


y1 = dis_height / 2 #badan snake

x1_change = 0 #tmpat snake paksi x


y1_change = 0 #tmpat snake paksi y

snake_List = []
Length_of_snake = 1 #pnjng snake ialah 1 block

foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0


#random tempat mknn untuk paksi x
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
#random tmpat mknn untuk paksi y

while not game_over:

while game_close == True: #klw game over, display blue and ckp haha kalah
dis.fill(blue)
message("Haha kalah! Press C-Play Again or Q-Quit", red) #gelakkan org
yg kalah
Your_score(Length_of_snake - 1) #score = panjang snake tolak 1
pygame.display.update()

for event in pygame.event.get():


if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q: #klw tkn 'q', game akan quit
game_over = True
game_close = False
if event.key == pygame.K_c:
gameLoop() #klw tkn 'c', game akan loop

for event in pygame.event.get():


if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.KEYDOWN: #untuk klw tekan key tu dia akan
bergerak
if event.key == pygame.K_a: #key utk gerak ke kiri
x1_change = -snake_block #klw tekan 'a', snake tu akan minus ke
x sebnyk 10 block
y1_change = 0 #y tak gerak sbb yg tukar x je
elif event.key == pygame.K_d: #gerak ke kanan
x1_change = snake_block #tekn 'd', snake tu akan positive ke x
sbnyk 10 block
y1_change = 0 #y tak gerak sbb dia guna paksi x je
elif event.key == pygame.K_w: #gerak ke atas
y1_change = -snake_block #tekn 'w', snake akan tolak block
paksi y sbnyk 10 block, so dia ke atas
x1_change = 0
elif event.key == pygame.K_s: #gerak ke bawah
y1_change = snake_block #yg tukar paksi y je
x1_change = 0

if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:


game_close = True #ni klw snake kena border, game akan stop
x1 += x1_change
y1 += y1_change
dis.fill(blue)
pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block]) #nk
build makanan
snake_Head = []
snake_Head.append(x1)
snake_Head.append(y1)
snake_List.append(snake_Head)
if len(snake_List) > Length_of_snake:
del snake_List[0]
for x in snake_List[:-1]:
if x == snake_Head: #klw mana bhagian snake kena snake head
game_close = False #i buat false supaya klw snake trkene badan
dia tak mati (sbb i sllu kalah, originally 'True')

our_snake(snake_block, snake_List) #ni bahagian snake kita


Your_score(Length_of_snake - 1) #awal awl score kita 0 sbb panjang snake
kita 1 block. klw pnjng snake 4 block, score 3 la

pygame.display.update() #dia akn update screen setiap kali kita mkn mknn,
so snake pnjng 1, makan 1 mknn, pnjng snake akn 2, jadi score ialah 1

if x1 == foodx and y1 == foody:


foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) *
10.0
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) *
10.0
Length_of_snake += 1 #klw snake kena makanan, panjang snake akan
bertambah

clock.tick(snake_speed)

pygame.quit()
quit() #utk stop semua

gameLoop() #game loop

You might also like