import pygame
from screeninfo import get_monitors
import numpy as np
pygame.init()
class Snake:
def __init__(self):
self.color_body = (140, 13, 17)
self.color_head = (59, 5, 7)
self.body_rect = []
self.head_move_x = 0
self.head_move_y = 0
self.head_x = 12 * width//25
self.head_y = 6 * height//13
self.body_x = np.array([11 * width//25, 10 * width//25, 9 * width//25])
self.body_y = np.array(
[6 * height//13, 6 * height//13, 6 * height//13])
self.body_move_x = self.body_x
self.body_move_y = self.body_y
self.start = False
def draw(self):
self.move()
self.check_fill()
self.body_rect = []
for index_x, index_y in zip(self.body_x, self.body_y):
self.body_rect.append(pygame.Rect(
index_x, index_y, width//24.8, height//13))
self.head_pos = np.array([self.head_x, self.head_y])
head_rect = pygame.Rect(
self.head_pos[0], self.head_pos[1], width//24.8, height//13)
pygame.draw.rect(screen, self.color_head, head_rect)
for index in self.body_rect[:-1]:
pygame.draw.rect(screen, self.color_body, index)
def move(self):
length = len(self.body_rect)
if self.start:
for index in range(1, length):
self.body_move_x[index] = self.body_move_x[index-1]
self.body_move_y[index] = self.body_move_y[index-1]
self.body_move_x[0] = self.head_x
self.body_move_y[0] = self.head_y
self.head_x += self.head_move_x
self.head_y += self.head_move_y
self.body_x = self.body_move_x
self.body_y = self.body_move_y
def reset_snake(self):
self.body_rect = []
self.head_move_x = 0
self.head_move_y = 0
self.head_x = 12 * width//25
self.head_y = 6 * height//13
self.body_x = np.array([11 * width//25, 10 * width//25, 9 * width//25])
self.body_y = np.array(
[6 * height//13, 6 * height//13, 6 * height//13])
self.body_move_x = self.body_x
self.body_move_y = self.body_y
self.start = False
def add_block(self):
self.body_move_x = np.append(self.body_move_x, [self.body_move_x[-1]+1 * width//25])
self.body_move_y = np.append(self.body_move_y, [self.body_move_y[-1]+1 * height//13])
def check_fill(self):
if (
not 0-6 < self.head_x < (width-width//25)+6
or not 0-3 < self.head_y < (height-height//13)+3
):
pygame.time.delay(160)
self.reset_snake()
class grass:
def __init__(self):
self.color = (167, 209, 61)
def draw(self):
for col in range(width//2):
if col % 2 == 0:
for row in range(height//2):
if row % 2 == 0:
grass = pygame.Rect(
col * width//25, row * height//13, width//25, height//13)
pygame.draw.rect(screen, self.color, grass)
else:
for row in range(height//2):
if row % 2 != 0:
grass = pygame.Rect(
col * width//25, row * height//13, width//25, height//13)
pygame.draw.rect(screen, self.color, grass)
class settings_button:
def __init__(self):
self.x = (width//56) * 2
self.y = (width//56) * 2
self.image = pygame.transform.scale(
pygame.image.load("settings.png"), (width//24, width//24))
def draw(self):
surface = self.image.get_rect(center=(self.x//2, self.y//2))
screen.blit(self.image, surface)
def pressed(self):
if (self.x >= (pygame.mouse.get_pos()[0]) >= 0 and self.y >= (pygame.mouse.get_pos()[1]) >= 0 and pygame.mouse.get_pressed()[0]):
return True
else:
return False
class settings:
def __init__(self):
self.font = pygame.font.Font("Hack-Regular.ttf", width//20)
self.text_q = 'Quit'
self.text_r = 'Replay'
self.text_b = 'Resumption'
self.text_s = 'Sound:'
self.text_p = 'Play'
self.text_m = 'Mute'
self.Quit_surface = self.font.render(self.text_q, True, 'red')
self.Replay_surface = self.font.render(self.text_r, True, 'cyan')
self.Resumption_surface = self.font.render(self.text_b, True, 'cyan')
self.Sound_surface = self.font.render(self.text_s, True, 'pink')
self.Mute_surface = self.font.render(self.text_m, True, 'pink')
self.Play_surface = self.font.render(self.text_p, True, 'pink')
self.Quit_rect = self.Quit_surface.get_rect(
center=((width//2), (height) - (self.Quit_surface.get_height()//2)))
self.Replay_rect = self.Replay_surface.get_rect(
center=((width//2), (height//2.5) - (self.Replay_surface.get_height()//2)))
self.Resumption_rect = self.Resumption_surface.get_rect(
center=((width//2), (height//1.5) - (self.Resumption_surface.get_height()//2)))
self.Sound_rect = self.Sound_surface.get_rect(
center=((width//2.4), (height//8) - (self.Sound_surface.get_height()//2)))
self.Mute_rect = self.Mute_surface.get_rect(
center=((width//1.7), (height//8) - (self.Mute_surface.get_height()//2)))
self.Play_rect = self.Play_surface.get_rect(
center=((width//1.7), (height//8) - (self.Play_surface.get_height()//2)))
def draw(self, sound_mode):
screen.blit(self.Quit_surface, self.Quit_rect)
screen.blit(self.Replay_surface, self.Replay_rect)
screen.blit(self.Resumption_surface, self.Resumption_rect)
screen.blit(self.Sound_surface, self.Sound_rect)
if sound_mode == 'Play':
screen.blit(self.Mute_surface, self.Mute_rect)
elif sound_mode == 'Mute':
screen.blit(self.Play_surface, self.Play_rect)
def pressed(self, sound_mode=None):
if (self.Quit_rect.x + self.Quit_surface.get_width() >= (pygame.mouse.get_pos()[0]) >= self.Quit_rect.x and self.Quit_rect.y + self.Quit_surface.get_height() >= (pygame.mouse.get_pos()[1]) >= self.Quit_rect.y and pygame.mouse.get_pressed()[0]):
return 'Quit'
elif (self.Replay_rect.x + self.Replay_surface.get_width() >= (pygame.mouse.get_pos()[0]) >= self.Replay_rect.x and self.Replay_rect.y + self.Replay_surface.get_height() >= (pygame.mouse.get_pos()[1]) >= self.Replay_rect.y and pygame.mouse.get_pressed()[0]):
return 'Replay'
elif (self.Resumption_rect.x + self.Resumption_surface.get_width() >= (pygame.mouse.get_pos()[0]) >= self.Resumption_rect.x and self.Resumption_rect.y + self.Resumption_surface.get_height() >= (pygame.mouse.get_pos()[1]) >= self.Resumption_rect.y and pygame.mouse.get_pressed()[0]):
return 'Resumption'
elif (self.Mute_rect.x + self.Mute_surface.get_width() >= (pygame.mouse.get_pos()[0]) >= self.Mute_rect.x and self.Mute_rect.y + self.Mute_surface.get_height() >= (pygame.mouse.get_pos()[1]) >= self.Mute_rect.y and pygame.mouse.get_pressed()[0] and sound_mode == 'Play'):
return 'Mute'
elif (self.Play_rect.x + self.Play_surface.get_width() >= (pygame.mouse.get_pos()[0]) >= self.Play_rect.x and self.Play_rect.y + self.Play_surface.get_height() >= (pygame.mouse.get_pos()[1]) >= self.Play_rect.y and pygame.mouse.get_pressed()[0] and sound_mode == 'Mute'):
return 'Play'
for index in get_monitors():
width = index.width
height = index.height
screen = pygame.display.set_mode((width, height))
click_sound = pygame.mixer.Sound('click1.wav')
settings_button_var = settings_button()
settings_var = settings()
grass_block = grass()
snake = Snake()
screen.fill((175, 220, 70))
grass_block.draw()
join_settings = False
sound_mode = 'Play'
deafult_numbers = np.array([height//13, -(height//13)])
w_test = width//24.8
snake.add_block()
colck = pygame.time.Clock()
try:
while True:
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Finish Work For pygame
break
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
if not join_settings:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake.head_move_y != deafult_numbers[0]:
snake.start = True
snake.head_move_y = deafult_numbers[1]
snake.head_move_x = 0
if not join_settings:
screen.fill((175, 220, 70))
grass_block.draw()
snake.draw()
settings_button_var.draw()
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
elif event.key == pygame.K_DOWN and snake.head_move_y != deafult_numbers[1]:
snake.start = True
snake.head_move_y = deafult_numbers[0]
snake.head_move_x = 0
if not join_settings:
screen.fill((175, 220, 70))
grass_block.draw()
snake.draw()
settings_button_var.draw()
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
elif event.key == pygame.K_LEFT and snake.head_move_x != w_test and snake.start:
snake.start = True
snake.head_move_x = -(w_test)
snake.head_move_y = 0
if not join_settings:
screen.fill((175, 220, 70))
grass_block.draw()
snake.draw()
settings_button_var.draw()
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
elif event.key == pygame.K_RIGHT and snake.head_move_x != -(w_test):
snake.start = True
snake.head_move_x = w_test
snake.head_move_y = 0
if not join_settings:
screen.fill((175, 220, 70))
grass_block.draw()
snake.draw()
settings_button_var.draw()
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
if not join_settings:
screen.fill((175, 220, 70))
grass_block.draw()
snake.draw()
settings_button_var.draw()
pygame.display.update()
if join_settings:
if settings_var.pressed() == 'Quit':
if join_settings and sound_mode == 'Play':
click_sound.play()
pygame.quit() # Finish Work For pygame
break
elif settings_var.pressed() == 'Resumption':
if join_settings and sound_mode == 'Play':
click_sound.play()
screen.fill((175, 220, 70))
join_settings = False
pygame.display.update()
elif settings_var.pressed() == 'Replay':
if join_settings and sound_mode == 'Play':
click_sound.play()
snake.reset_snake()
join_settings = False
pygame.display.update()
elif settings_var.pressed(sound_mode) == 'Mute' and sound_mode == 'Play':
screen.fill((149, 184, 68))
sound_mode = 'Mute'
if join_settings and sound_mode == 'Play':
click_sound.play()
settings_var.draw(sound_mode)
pygame.display.update()
pygame.time.delay(150)
elif settings_var.pressed(sound_mode) == 'Play' and sound_mode == 'Mute':
screen.fill((149, 184, 68))
sound_mode = 'Play'
if join_settings and sound_mode == 'Play':
click_sound.play()
settings_var.draw(sound_mode)
pygame.display.update()
pygame.time.delay(150)
if settings_button_var.pressed():
if not join_settings and sound_mode == 'Play':
click_sound.play()
if not join_settings:
join_settings = True
screen.fill((149, 184, 68))
settings_var.draw(sound_mode)
pygame.display.update()
pygame.display.update()
colck.tick(100)
except:
pass
i want add a function to add new block for a snake
======================================
======================================
if you see any problem in my code insert me
i don't why the block delete after start a moving