|
|
import pygame |
|
|
import sys |
|
|
import random |
|
|
import time |
|
|
|
|
|
|
|
|
pygame.init() |
|
|
|
|
|
|
|
|
WIDTH, HEIGHT = 640, 480 |
|
|
|
|
|
BLOCK_SIZE = 20 |
|
|
|
|
|
SPEED = 10 |
|
|
|
|
|
DIRECTIONS = {'UP': (0, -1), 'DOWN': (0, 1), 'LEFT': (-1, 0), 'RIGHT': (1, 0)} |
|
|
|
|
|
|
|
|
WHITE = (255, 255, 255) |
|
|
BLACK = (0, 0, 0) |
|
|
GREEN = (0, 255, 0) |
|
|
|
|
|
RED = (255, 0, 0) |
|
|
|
|
|
class SnakeGame: |
|
|
def __init__(self): |
|
|
self.display = pygame.display.set_mode((WIDTH, HEIGHT)) |
|
|
pygame.display.set_caption("Snake Game 2000 Edition") |
|
|
self.clock = pygame.time.Clock() |
|
|
self.reset() |
|
|
|
|
|
def reset(self): |
|
|
|
|
|
self.snake = [(200, 200), (220, 200), (240, 200)] |
|
|
|
|
|
self.direction = 'RIGHT' |
|
|
|
|
|
self.apple = self.generate_apple() |
|
|
|
|
|
self.score = 0 |
|
|
|
|
|
self.game_over = False |
|
|
|
|
|
def generate_apple(self): |
|
|
|
|
|
while True: |
|
|
x = random.randint(0, WIDTH - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE |
|
|
y = random.randint(0, HEIGHT - BLOCK_SIZE) // BLOCK_SIZE * BLOCK_SIZE |
|
|
apple = (x, y) |
|
|
if apple not in self.snake: |
|
|
return apple |
|
|
|
|
|
def update(self): |
|
|
if self.game_over: |
|
|
|
|
|
print(f"\nGame over anon! Final score: {self.score}") |
|
|
print("You died like a virgin. Press R to try again.") |
|
|
return |
|
|
|
|
|
|
|
|
head = self.snake[-1] |
|
|
|
|
|
new_head = (head[0] + DIRECTIONS[self.direction][0] * BLOCK_SIZE, |
|
|
head[1] + DIRECTIONS[self.direction][1] * BLOCK_SIZE) |
|
|
|
|
|
|
|
|
if ((new_head[0] < 0 or new_head[0] >= WIDTH or |
|
|
new_head[1] < 0 or new_head[1] >= HEIGHT) or |
|
|
new_head in self.snake): |
|
|
|
|
|
self.game_over = True |
|
|
self.draw() |
|
|
pygame.time.delay(2000) |
|
|
return |
|
|
|
|
|
|
|
|
self.snake.append(new_head) |
|
|
|
|
|
|
|
|
if self.apple == new_head: |
|
|
|
|
|
self.score += 1 |
|
|
print(f"🍎 Munch! Score: {self.score}") |
|
|
self.apple = self.generate_apple() |
|
|
else: |
|
|
|
|
|
self.snake.pop(0) |
|
|
|
|
|
def draw(self): |
|
|
|
|
|
self.display.fill(BLACK) |
|
|
|
|
|
|
|
|
for pos in self.snake: |
|
|
pygame.draw.rect(self.display, GREEN, pygame.Rect(pos[0], pos[1], BLOCK_SIZE, BLOCK_SIZE)) |
|
|
|
|
|
|
|
|
pygame.draw.rect(self.display, RED, pygame.Rect(self.apple[0], self.apple[1], BLOCK_SIZE, BLOCK_SIZE)) |
|
|
|
|
|
|
|
|
font = pygame.font.Font(None, 36) |
|
|
text = font.render(f"Score: {self.score}", True, WHITE) |
|
|
self.display.blit(text, (10, 10)) |
|
|
|
|
|
|
|
|
if self.game_over: |
|
|
text = font.render("Game Over. Press R to restart.", True, RED) |
|
|
self.display.blit(text, (10, 50)) |
|
|
|
|
|
|
|
|
pygame.display.flip() |
|
|
|
|
|
def handle_input(self): |
|
|
for event in pygame.event.get(): |
|
|
if event.type == pygame.QUIT: |
|
|
|
|
|
pygame.quit() |
|
|
sys.exit() |
|
|
elif event.type == pygame.KEYDOWN: |
|
|
|
|
|
if event.key == pygame.K_UP and self.direction != 'DOWN': |
|
|
self.direction = 'UP' |
|
|
elif event.key == pygame.K_DOWN and self.direction != 'UP': |
|
|
self.direction = 'DOWN' |
|
|
elif event.key == pygame.K_LEFT and self.direction != 'RIGHT': |
|
|
self.direction = 'LEFT' |
|
|
elif event.key == pygame.K_RIGHT and self.direction != 'LEFT': |
|
|
self.direction = 'RIGHT' |
|
|
elif event.key == pygame.K_r: |
|
|
|
|
|
self.reset() |
|
|
self.game_over = False |
|
|
elif event.key == pygame.K_s: |
|
|
|
|
|
try: |
|
|
speed = int(input("Enter new speed: ")) |
|
|
if speed < 1: |
|
|
print("Speed must be 1 or higher, you absolute mongoloid") |
|
|
else: |
|
|
SPEED = speed |
|
|
print(f"Speed set to {speed}. Your grandma can handle this.") |
|
|
except ValueError: |
|
|
print("Invalid input. Default speed remains.") |
|
|
|
|
|
|
|
|
elif event.key == pygame.K_c: |
|
|
print(f"Current speed: {SPEED}") |
|
|
|
|
|
def run(self): |
|
|
|
|
|
while True: |
|
|
self.handle_input() |
|
|
|
|
|
if self.game_over: |
|
|
self.draw() |
|
|
|
|
|
pygame.time.delay(2000) |
|
|
break |
|
|
|
|
|
self.update() |
|
|
self.draw() |
|
|
|
|
|
self.clock.tick(SPEED) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
game = SnakeGame() |
|
|
print("Welcome to Snake Game 2000 Edition!") |
|
|
print("Use arrow keys to move, R to restart, S to change speed") |
|
|
print(f"Current speed: {SPEED}") |
|
|
game.run() |
|
|
|