Giới thiệu sản phẩm
import pygame
import random
# Constants
WIDTH, HEIGHT = 400, 400
GRID_SIZE = 20
GRID_WIDTH = WIDTH // GRID_SIZE
GRID_HEIGHT = HEIGHT // GRID_SIZE
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
class App:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
self.clock = pygame.time.Clock()
self.snake = Snake()
self.food = Food()
def run(self):
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
self.snake.handle_keys()
self.snake.move()
self.check_collision()
self.screen.fill(BLACK)
self.snake.draw(self.screen)
self.food.draw(self.screen)
pygame.display.update()
self.clock.tick(10)
pygame.quit()
def check_collision(self):
if self.snake.head == self.food.position:
self.snake.length += 1
self.food.randomize_position()
if (self.snake.head[0] < 0 or self.snake.head[0] >= GRID_WIDTH
or self.snake.head[1] < 0 or self.snake.head[1] >= GRID_HEIGHT
or self.snake.head in self.snake.body[:-1]):
pygame.quit()
class Snake:
def __init__(self):
self.length = 1
self.body = [(random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))]
self.direction = (1, 0)
@property
def head(self):
return self.body[-1]
def move(self):
x, y = self.head
dx, dy = self.direction
new_head = ((x + dx) % GRID_WIDTH, (y + dy) % GRID_HEIGHT)
self.body.append(new_head)
if len(self.body) > self.length:
del self.body[0]
def handle_keys(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and self.direction != (0, 1):
self.direction = (0, -1)
elif keys[pygame.K_DOWN] and self.direction != (0, -1):
self.direction = (0, 1)
elif keys[pygame.K_LEFT] and self.direction != (1, 0):
self.direction = (-1, 0)
elif keys[pygame.K_RIGHT] and self.direction != (-1, 0):
self.direction = (1, 0)
def draw(self, surface):
for segment in self.body:
pygame.draw.rect(surface, GREEN,
(segment[0] * GRID_SIZE, segment[1] * GRID_SIZE,
GRID_SIZE, GRID_SIZE))
class Food:
def __init__(self):
self.position = (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
def randomize_position(self):
self.position = (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
def draw(self, surface):
pygame.draw.rect(surface, RED,
(self.position[0] * GRID_SIZE,
self.position[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
if __name__ == "__main__":
game = App()
game.run()
import random
# Constants
WIDTH, HEIGHT = 400, 400
GRID_SIZE = 20
GRID_WIDTH = WIDTH // GRID_SIZE
GRID_HEIGHT = HEIGHT // GRID_SIZE
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
class App:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
self.clock = pygame.time.Clock()
self.snake = Snake()
self.food = Food()
def run(self):
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
self.snake.handle_keys()
self.snake.move()
self.check_collision()
self.screen.fill(BLACK)
self.snake.draw(self.screen)
self.food.draw(self.screen)
pygame.display.update()
self.clock.tick(10)
pygame.quit()
def check_collision(self):
if self.snake.head == self.food.position:
self.snake.length += 1
self.food.randomize_position()
if (self.snake.head[0] < 0 or self.snake.head[0] >= GRID_WIDTH
or self.snake.head[1] < 0 or self.snake.head[1] >= GRID_HEIGHT
or self.snake.head in self.snake.body[:-1]):
pygame.quit()
class Snake:
def __init__(self):
self.length = 1
self.body = [(random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))]
self.direction = (1, 0)
@property
def head(self):
return self.body[-1]
def move(self):
x, y = self.head
dx, dy = self.direction
new_head = ((x + dx) % GRID_WIDTH, (y + dy) % GRID_HEIGHT)
self.body.append(new_head)
if len(self.body) > self.length:
del self.body[0]
def handle_keys(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and self.direction != (0, 1):
self.direction = (0, -1)
elif keys[pygame.K_DOWN] and self.direction != (0, -1):
self.direction = (0, 1)
elif keys[pygame.K_LEFT] and self.direction != (1, 0):
self.direction = (-1, 0)
elif keys[pygame.K_RIGHT] and self.direction != (-1, 0):
self.direction = (1, 0)
def draw(self, surface):
for segment in self.body:
pygame.draw.rect(surface, GREEN,
(segment[0] * GRID_SIZE, segment[1] * GRID_SIZE,
GRID_SIZE, GRID_SIZE))
class Food:
def __init__(self):
self.position = (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
def randomize_position(self):
self.position = (random.randint(0, GRID_WIDTH - 1),
random.randint(0, GRID_HEIGHT - 1))
def draw(self, surface):
pygame.draw.rect(surface, RED,
(self.position[0] * GRID_SIZE,
self.position[1] * GRID_SIZE, GRID_SIZE, GRID_SIZE))
if __name__ == "__main__":
game = App()
game.run()
Hình ảnh sản phẩm
Sản phẩm cùng tác giả
Sản phẩm liên quan
Không có sản phẩm nào
Đăng nhập để tham gia bình luận