gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
You are an experienced programmer and code formatter. Respond…
- User
Assistant: User: System: You are an experienced programmer and code formatter. Respond concisely, following these guidelines:
-
Full Code File: [FULL CODE:file_extension] [FILE START] [code] [FILE END]
-
Code Snippet: [CODE SNIPPET] [STARTSNIPPET] [code] [ENDSNIPPET]
-
Bash Command: [BASHCMD] [CMDSTART] [command] [CMDEnd]
-
Normal Text: Provide clear, concise text answers
Here is the conversation history: user: python code for a curses snake game assistant: Sure! Here’s a simple implementation of a Snake game using the
curseslibrary in Python.[FULL CODE:snake_game.py] [FILE START] import curses from random import randint # Setting up the window stdscr = curses.initscr() curses.curs_set(0) sh, sw = stdscr.getmaxyx() # get height and width of window w = curses.newwin(sh, sw, 0, 0) # create a new window w.keypad(1) # enable keypad input w.timeout(100) # refresh window every 100 milliseconds # Initial snake and food positions snk_x = sw // 4 snk_y = sh // 2 snake = [ [snk_y, snk_x], [snk_y, snk_x - 1], [snk_y, snk_x - 2] ] food = [sh // 2, sw // 2] w.addch(int(food[0]), int(food[1]), curses.ACS_PI) key = curses.KEY_RIGHT # initial direction while True: next_key = w.getch() key = key if next_key == -1 else next_key # Calculate new head of the snake new_head = [snake[0][0], snake[0][1]] if key == curses.KEY_DOWN: new_head[0] += 1 if key == curses.KEY_UP: new_head[0] -= 1 if key == curses.KEY_LEFT: new_head[1] -= 1 if key == curses.KEY_RIGHT: new_head[1] += 1 # Insert new head snake.insert(0, new_head) # Check if snake has eaten the food if snake[0] == food: food = None while food is None: nf = [ randint(1, sh - 1), randint(1, sw - 1) ] food = nf if nf not in snake else None w.addch(int(food[0]), int(food[1]), curses.ACS_PI) else: # Remove the last segment of the snake tail = snake.pop() w.addch(int(tail[0]), int(tail[1]), ' User: full code please Assistant: Assistant: -
- Assistant
[FULL CODE:snake_game.py] [FILE START]
import curses from random import randint def main(stdscr): # Setting up the window curses.curs_set(0) sh, sw = stdscr.getmaxyx() # get height and width of window w = curses.newwin(sh, sw, 0, 0) # create a new window w.keypad(1) # enable keypad input w.timeout(100) # refresh window every 100 milliseconds # Initial snake and food positions snk_x = sw // 4 snk_y = sh // 2 snake = [ [snk_y, snk_x], [snk_y, snk_x - 1], [snk_y, snk_x - 2] ] food = [sh // 2, sw // 2] w.addch(int(food[0]), int(food[1]), curses.ACS_PI) key = curses.KEY_RIGHT # initial direction while True: next_key = w.getch() key = key if next_key == -1 else next_key # Calculate new head of the snake new_head = [snake[0][0], snake[0][1]] if key == curses.KEY_DOWN: new_head[0] += 1 if key == curses.KEY_UP: new_head[0] -= 1 if key == curses.KEY_LEFT: new_head[1] -= 1 if key == curses.KEY_RIGHT: new_head[1] += 1 # Insert new head snake.insert(0, new_head) # Check if snake has eaten the food if snake[0] == food: food = None while food is None: nf = [ randint(1, sh - 1), randint(1, sw - 1) ] food = nf if nf not in snake else None w.addch(int(food[0]), int(food[1]), curses.ACS_PI) else: # Remove the last segment of the snake tail = snake.pop() w.addch(int(tail[0]), int(tail[1]), ' ') # Check for collision if (snake[0][0] in [0, sh] or snake[0][1] in [0, sw] or snake[0] in snake[1:]): curses.endwin() quit() # Refresh the screen w.addch(int(snake[0][0]), int(snake[0][1]), '#') curses.wrapper(main)[FILE END]