Can code snake in python, first model at this size after deekseekcoder-instruct-6.7b (With caveat)
#7
by
rombodawg
- opened
It did take about 20 tries of failed attempts, either because of logical errors, or some other types of issues. But finally it got it right. A perfect coded game of snake. Here is the code. It works from the first try without error correcting.
Prompt:
User:
Write the classic game of "snake" in python using pygame
Ai:
Sure, here's an implementation of the classic Snake game in Python using the Pygame library:
import pygame
import random
# Initialize Pygame
pygame.init()
# Set up display window
screen_width = 600
screen_height = 400
win = pygame.display.set_mode((screen_width, screen_height))
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# Set up game variables
snake_speed = 50
direction = 'RIGHT'
score = 0
# Create snake and food
snake_body = [[100, 50], [90, 50], [80, 50]]
food_pos = [random.randint(1, (screen_width//10)) * 10, random.randint(1, (screen_height//10)) * 10]
# Game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT and direction != 'RIGHT':
direction = 'LEFT'
elif event.key == pygame.K_RIGHT and direction != 'LEFT':
direction = 'RIGHT'
elif event.key == pygame.K_UP and direction != 'DOWN':
direction = 'UP'
elif event.key == pygame.K_DOWN and direction != 'UP':
direction = 'DOWN'
# Move snake
new_head = [snake_body[0][0], snake_body[0][1]]
if direction == 'RIGHT':
new_head[0] += 10
elif direction == 'LEFT':
new_head[0] -= 10
elif direction == 'UP':
new_head[1] -= 10
elif direction == 'DOWN':
new_head[1] += 10
# Check for collision with food and wall
if new_head in snake_body or new_head[0] < 0 or new_head[0] >= screen_width or new_head[1] < 0 or new_head[1] >= screen_height:
running = False
else:
snake_body.insert(0, new_head)
# Check for collision with food and increase score
if snake_body[0] == food_pos:
score += 10
food_pos = [random.randint(1, (screen_width//10)) * 10, random.randint(1, (screen_height//10)) * 10]
else:
snake_body.pop()
# Draw everything on the screen
win.fill(BLACK)
for pos in snake_body:
pygame.draw.rect(win, WHITE, pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(win, RED, pygame.Rect(food_pos[0], food_pos[1], 10, 10))
# Update display
pygame.display.update()
# Set game speed
pygame.time.delay(snake_speed)
# Quit Pygame
pygame.quit()
rombodawg
changed discussion title from
Can code snake in python, first model after deekseekcoder-instruct-6.7b (With caveat)
to Can code snake in python, first model at this size after deekseekcoder-instruct-6.7b (With caveat)
It's easy to implement this old test into training. Test it for real case and come back with real feedback instead.
Thank you for testing. We welcome more examples about CodeQwen1.5.
huybery
changed discussion status to
closed