0% found this document useful (0 votes)
7 views11 pages

Anaconda Half

Uploaded by

vincent raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

Anaconda Half

Uploaded by

vincent raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

import pygame as pg

from pygame.math import Vector2


import sys
import pickle
import time
import random as r

class SNAKE:
def __init__(self):
self.body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
self.direction = Vector2(0,0)
self.new_block = False

self.head_up = pg.image.load('Graphics/head_up.png').convert_alpha()
self.head_down = pg.image.load('Graphics/head_down.png').convert_alpha()
self.head_right = pg.image.load('Graphics/head_right.png').convert_alpha()
self.head_left = pg.image.load('Graphics/head_left.png').convert_alpha()

self.tail_up = pg.image.load('Graphics/tail_up.png').convert_alpha()
self.tail_down = pg.image.load('Graphics/tail_down.png').convert_alpha()
self.tail_right = pg.image.load('Graphics/tail_right.png').convert_alpha()
self.tail_left = pg.image.load('Graphics/tail_left.png').convert_alpha()

self.body_vertical = pg.image.load('Graphics/body_vertical.png').convert_alpha()
self.body_horizontal = pg.image.load('Graphics/body_horizontal.png').convert_alpha()

self.body_tr = pg.image.load('Graphics/body_tr.png').convert_alpha()
self.body_tl = pg.image.load('Graphics/body_tl.png').convert_alpha()
self.body_br = pg.image.load('Graphics/body_br.png').convert_alpha()
self.body_bl = pg.image.load('Graphics/body_bl.png').convert_alpha()
self.sound = pg.mixer.Sound('Sound/crunch.wav')
self.fruit = FRUIT()

def draw_snake(self):
self.update_head()
self.update_tail()

for index,block in enumerate(self.body):


x_pos = int(block.x*cell_size)
y_pos = int(block.y*cell_size)
block_rect = pg.Rect(x_pos,y_pos,cell_size,cell_size)
#pg.draw.rect(screen,(0,0,0),block_rect)
if index == 0:
screen.blit(self.head,block_rect)
elif index == len(self.body)-1:
screen.blit(self.tail,block_rect)
else:
previous_block = self.body[index + 1] - block
next_block = self.body[index - 1] - block
if previous_block.y == next_block.y:
screen.blit(self.body_horizontal,block_rect)
elif previous_block.x == next_block.x:
screen.blit(self.body_vertical,block_rect)
else:
if previous_block.x == -1 and next_block.y == -1 or previous_block.y == -1 and
next_block.x == -1:
screen.blit(self.body_tl,block_rect)
elif previous_block.x == -1 and next_block.y == 1 or previous_block.y == 1 and
next_block.x == -1:
screen.blit(self.body_bl,block_rect)
elif previous_block.x == 1 and next_block.y == -1 or previous_block.y == -1 and
next_block.x == 1:
screen.blit(self.body_tr,block_rect)
elif previous_block.x == 1 and next_block.y == 1 or previous_block.y == 1 and
next_block.x == 1:
screen.blit(self.body_br,block_rect)

def update_head(self):
head_relation = self.body[1] - self.body[0]
if head_relation == Vector2(-1,0): self.head = self.head_right
elif head_relation == Vector2(1,0): self.head = self.head_left
elif head_relation == Vector2(0,-1): self.head = self.head_down
elif head_relation == Vector2(0,1): self.head = self.head_up

def update_tail(self):
tail_relation = self.body[-2] - self.body[-1]
if tail_relation == Vector2(1,0): self.tail = self.tail_left
elif tail_relation == Vector2(-1,0): self.tail = self.tail_right
elif tail_relation == Vector2(0,1): self.tail = self.tail_up
elif tail_relation == Vector2(0,-1): self.tail = self.tail_down

def move_snake(self):
if self.new_block == True:
body_copy = self.body[:]
body_copy.insert(0,body_copy[0] + self.direction)
self.body = body_copy[:]
self.new_block = False
else:
body_copy = self.body[:-1]
body_copy.insert(0,body_copy[0] + self.direction)
self.body = body_copy[:]

def auto_move(self):
head_x, head_y = self.body[0]

# Check current direction


current_direction = self.body[0] - self.body[1]

if current_direction != Vector2(-1, 0) and head_x < self.fruit.position.x: #if apple's head x


is more than snake then move right
self.direction = Vector2(1, 0) # Right
elif current_direction != Vector2(1, 0) and head_x > self.fruit.position.x: #if apple's head
x is less than snake then move left
self.direction = Vector2(-1, 0) # Left
elif current_direction != Vector2(0, -1) and head_y < self.fruit.position.y: #if apple's head
x is more than snake then move right
self.direction = Vector2(0, 1) # Down
elif current_direction != Vector2(0, 1) and head_y > self.fruit.position.y : #if apple's
head x is more than snake then move right
self.direction = Vector2(0, -1) # Up

new_head = self.body[0] + self.direction


self.body.insert(0, new_head)
self.body.pop(-1)

def add_block(self):
self.new_block = True

def reset(self):
self.body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
self.direction = Vector2(0,0)

def play_crunch(self):
self.sound.play()

class FRUIT:
def __init__(self):
self.randomize()

def draw_apple(self):
fruit_rect =
pg.Rect(int(self.position.x*cell_size),int(self.position.y*cell_size),cell_size,cell_size)
Orchid_rect = Orchid.get_rect(center = (fruit_rect.centerx,fruit_rect.centery))
screen.blit(Orchid,Orchid_rect)

def randomize(self):
self.x = r.randint(0,cell_number - 1)
self.y = r.randint(0,cell_number - 1)
self.position = Vector2(self.x,self.y)

class GAME1:
def __init__(self):
self.snake = SNAKE()
self.fruit = FRUIT()
self.high_score()

def draw(self):
self.draw_grass()
self.snake.draw_snake()
if self.fruit.position not in self.snake.body:
self.fruit.draw_apple()
else: self.fruit.randomize()
self.draw_score()

def draw_grass(self):
grass_color = (0,0,0)
for row in range(cell_number):
if row%2 == 0:
for col in range(cell_number):
if col%2 == 0:
grass_rect = pg.Rect(col*cell_size,row*cell_size,cell_size,cell_size)
pg.draw.rect(screen,grass_color,grass_rect)
else:
for col in range(cell_number):
if col%2 != 0:
grass_rect = pg.Rect(col*cell_size,row*cell_size,cell_size,cell_size)
pg.draw.rect(screen,grass_color,grass_rect)

def update(self):
self.snake.move_snake()
self.check_collisions()
self.check_wall()
self.high_score()

def check_collisions(self):
global score
if self.snake.body[0] == self.fruit.position:
self.fruit.randomize()
self.snake.new_block = True
self.snake.play_crunch()
score += 1
if self.snake.body[0] in self.snake.body[1:]:
self.fruit.randomize()
self.game_over()

def check_wall(self):
if not 0 <= self.snake.body[0].x < cell_number or not 0 <= self.snake.body[0].y <
cell_number:
self.fruit.randomize()
self.game_over()

def game_over(self):
global game_over
global score
self.snake.reset()
score = 0
game_over = False

def Opening(self):
Opening_text = str("Press 'Spacebar' to start")
logo_rect = logo.get_rect(center = (400,300))
screen.blit(logo,logo_rect)
Opening_surface = Opening_font.render(Opening_text,True,(255,255,255))
Opening_rect = Opening_surface.get_rect(midtop = (logo_rect.midbottom))
screen.blit(Opening_surface,Opening_rect)

def reset_game(self):
Reset_text = str("Press arrow keys to Start")
Reset_surface = Reset_font.render(Reset_text,True,(255,255,255))
Reset_rect = Reset_surface.get_rect(midtop = (400,600))
screen.blit(Reset_surface,Reset_rect)
Escape_text = str("Press Escape to go Home")
Escape_surface = Reset_font.render(Escape_text,True,(255,255,255))
Escape_rect = Escape_surface.get_rect(midtop = (400,670))
screen.blit(Escape_surface,Escape_rect)

def draw_score(self):
global score
score_text = str(score)
score_surface = score_font.render(score_text,True,(56,74,12))
score_x = int(cell_size * cell_number - 60)
score_y = int(cell_size * cell_number - 40)
score_rect = score_surface.get_rect(center = ((score_x+10),score_y))
highscore_text = str(f'Highscore: {self.highscore}')
highscore_surface = highscore_font.render(highscore_text,True,(155,155,155))
highscore_rect = highscore_surface.get_rect(topright = (790,0))
Orchid_rect = Orchid.get_rect(midright = (score_rect.left-10,score_rect.centery))
bg_rect = pg.Rect(Orchid_rect.left-6,Orchid_rect.top-4,Orchid_rect.width +
score_rect.width + 25,Orchid_rect.height+7)
pg.draw.rect(screen,(0,155,0),bg_rect)
screen.blit(score_surface,score_rect)
screen.blit(Orchid,Orchid_rect)
screen.blit(highscore_surface,highscore_rect)
pg.draw.rect(screen,(7,21,36),bg_rect,2)

def high_score(self):
global score
highfile = open('Highscore.bin','rb')
highfile2 = pickle.load(highfile)
self.highscore = highfile2['HighScore']
if score > self.highscore:
self.highscore = score
highdict = {'HighScore':self.highscore}
highfilewrite = open('Highscore.bin','wb')
pickle.dump(highdict,highfilewrite)

pg.mixer.pre_init(44100,-16,2,512)
pg.init()
pg.display.init()
Opening_font = pg.font.Font('Font/SerpensRegular-7MGE.ttf',90)
Reset_font = pg.font.Font('Font/SerpensRegular-7MGE.ttf',90)
score_font = pg.font.Font('Font/PoetsenOne-Regular.ttf',25)
highscore_font = pg.font.Font('Font/PoetsenOne-Regular.ttf',25)
game_over = False
score = 0
cell_size = 40
cell_number = 20
screen = pg.display.set_mode((cell_number*cell_size,cell_size*cell_number))
clock = pg.time.Clock()
Blood_Orchid = pg.image.load('Graphics/Blood Orchid.png').convert_alpha()
Orchid = pg.transform.scale_by(Blood_Orchid,0.2)
logo = pg.image.load('Graphics/icon.jpg').convert_alpha()
main_game = GAME1()
SCREEN_UPDATE = pg.USEREVENT
pg.time.set_timer(SCREEN_UPDATE,150)
pg.display.set_icon(logo)
pg.display.set_caption('Anaconda - The Legacy Returns')
game_opening = True
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if game_over:
if event.type == SCREEN_UPDATE:
main_game.update()
if event.type == pg.KEYDOWN:
if not game_opening:
game_over = True
if event.key == pg.K_UP:
if main_game.snake.direction != Vector2(0,1):
main_game.snake.direction = Vector2(0,-1)
if event.key == pg.K_DOWN:
if main_game.snake.direction != Vector2(0,-1):
main_game.snake.direction = Vector2(0,1)
if event.key == pg.K_RIGHT:
if main_game.snake.direction != Vector2(-1,0):
main_game.snake.direction = Vector2(1,0)
if event.key == pg.K_LEFT:
if main_game.snake.direction != Vector2(1,0):
main_game.snake.direction = Vector2(-1,0)
if event.key == pg.K_ESCAPE:
game_opening = True
game_over = False
if not game_over:
if event.key == pg.K_SPACE:
game_opening = False

pg.display.update()
screen.fill((7,21,36))
main_game.draw()
if game_opening:
screen.fill((0,0,0))
main_game.Opening()
elif not game_over:
main_game.reset_game()
clock.tick(60)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy