Untitled

 avatar
unknown
plain_text
2 months ago
3.5 kB
10
Indexable
import pygame,sys,random
from pygame.math import Vector2

pygame.mixer.pre_init(44100,-16,2,512)
pygame.init()
cell_size = 40
cell_number = 20
screen = pygame.display.set_mode((cell_number * cell_size,cell_number * cell_size))
clock = pygame.time.Clock()
apple = pygame.image.load('Graphics/apple.png').convert_alpha()
game_font = pygame.font.Font('Font/PoetsenOne-Regular.ttf', 25)

SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE,150)

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 #boolean value
		
		self.head_up = pygame.image.load('Graphics/head_up.png').convert_alpha()
		self.head_down = pygame.image.load('Graphics/head_down.png').convert_alpha()
		self.head_right = pygame.image.load('Graphics/head_right.png').convert_alpha()
		self.head_left = pygame.image.load('Graphics/head_left.png').convert_alpha()
		
		self.tail_up = pygame.image.load('Graphics/tail_up.png').convert_alpha()
		self.tail_down = pygame.image.load('Graphics/tail_down.png').convert_alpha()
		self.tail_right = pygame.image.load('Graphics/tail_right.png').convert_alpha()
		self.tail_left = pygame.image.load('Graphics/tail_left.png').convert_alpha()

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

		self.body_tr = pygame.image.load('Graphics/body_tr.png').convert_alpha()
		self.body_tl = pygame.image.load('Graphics/body_tl.png').convert_alpha()
		self.body_br = pygame.image.load('Graphics/body_br.png').convert_alpha()
		self.body_bl = pygame.image.load('Graphics/body_bl.png').convert_alpha()
		self.crunch_sound = pygame.mixer.Sound('Sound/crunch.wav')


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

	def update_tail_graphics(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):  # positive y 
			self.tail = self.tail_up
		elif tail_relation == Vector2(0,-1):  #negative y 
			self.tail = self.tail_down


	def draw_snake(self):
		self.update_head_graphics()
		self.update_tail_graphics()

		#for letter/word in ...:
		#* multiply
		for index, block in enumerate(self.body):
			x_pos = int(block.x * cell_size)
			y_pos = int(block.y * cell_size)
			block_rect = pygame.Rect(x_pos, y_pos, cell_size, cell_size)


			if index == 0:
				screen.blit(self.head, block_rect) #image, location 
			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 
				







		

#class - blueprint(plan)  
#object - something created from the class

class MAIN:
	def __init__(self):
		self.snake = SNAKE()
main_game = MAIN()	


while True:
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
        
	screen.fill((175,215,70))
	
	pygame.display.update()
	clock.tick(60)

	
Editor is loading...
Leave a Comment