Prompt Title: Pygame Snake Game

Created 1 year ago
Votes 0
Views 320 times
0

Prompt Detail:

Sure! Here's an example Python code using Pygame to create the classic Snake game: ``` import pygame import random # Initialize Pygame pygame.init() # Set up the screen screen_width = 600 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Snake Game") # Set up the colors background_color = (0, 0, 0) snake_color = (0, 255, 0) food_color = (255, 0, 0) # Set up the snake snake_block_size = 10 snake_speed = 15 def draw_snake(snake_block_size, snake_list): for x in snake_list: pygame.draw.rect(screen, snake_color, [x[0], x[1], snake_block_size, snake_block_size]) # Set up the food food_block_size = 10 def draw_food(): food_x = round(random.randrange(0, screen_width - food_block_size) / 10.0) * 10.0 food_y = round(random.randrange(0, screen_height - food_block_size) / 10.0) * 10.0 return food_x, food_y # Set up the game loop def game_loop(): game_over = False game_close = False # Set up the starting position of the snake snake_start_x = round(screen_width / 2 / 10.0) * 10.0 snake_start_y = round(screen_height / 2 / 10.0) * 10.0 snake_list = [] length_of_snake = 1 # Add the starting position of the snake to the snake list snake_list.append([snake_start_x, snake_start_y]) # Set up the food food_x, food_y = draw_food() # Set up the initial direction of the snake direction = "right" while not game_over: # Check for events for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: direction = "left" elif event.key == pygame.K_RIGHT: direction = "right" elif event.key == pygame.K_UP: direction = "up" elif event.key == pygame.K_DOWN: direction = "down" # Move the snake if direction == "right": snake_head = [] snake_head.append(snake_list[-1][0] + snake_block_size) snake_head.append(snake_list[-1][1]) elif direction == "left": snake_head = [] snake_head.append(snake_list[-1][0] - snake_block_size) snake_head.append(snake_list[-1][1]) elif direction == "up": snake_head = [] snake_head.append(snake_list[-1][0]) snake_head.append(snake_list[-1][1] - snake_block_size) elif direction == "down": snake_head = [] snake_head.append(snake_list[-1][0]) snake_head.append(snake_list[-1][1] + snake_block_size) # Add the new head of the snake to the snake list snake_list.append(snake_head) # Check if the snake hits the wall if snake_head[0] >= screen_width or snake_head[0] < 0 or snake_head[1] >= screen_height or snake_head[1] < 0: game_close = True
avatar
Waseem
Shared 2 prompts
Created 1 year ago

Leave a Comment

Related Tag Prompts

271
0
Python Pro
1 year ago 2023-01-19 05:16:28 AIPRM
0
0
MySQL Database Creation.
1 year ago 2023-03-08 12:23:48 Pub
0
0
Cool stuff
1 year ago 2023-03-08 15:49:26 Sivayogeith
0
0
Bajar version de python
1 year ago 2023-03-09 10:48:33 Juanma
0
0
Frappe
1 year ago 2023-03-11 01:40:59 KuuKuu
0
0
0
0
python remote control server
1 year ago 2023-03-11 23:05:25 V0rt
0
0
output
1 year ago 2023-03-14 07:09:17 yuvaraj
0
0
s-parameter processing in python
1 year ago 2023-03-14 14:46:13 jhdo
0
0
Python Landsat data handling.
1 year ago 2023-03-15 13:10:40 nathan
0
0
Python para Ciencia Datos
1 year ago 2023-03-16 08:24:10 pedro
0
0
Following Rotated Logfiles.
1 year ago 2023-03-17 09:05:24 hamid
0
0
Modelo de Negócio.
1 year ago 2023-03-18 01:35:57 reiu
0
0
Django API for Todo
1 year ago 2023-03-18 17:45:33 AbdAlmjed
0
0
python爬虫
1 year ago 2023-03-21 00:23:14 DDL