improve speed

This commit is contained in:
Michael Pilosov 2024-05-09 18:02:30 -06:00
parent eb617e6d59
commit 069f98910e

View File

@ -1,67 +1,76 @@
import curses
import random
import numpy as np import numpy as np
import curses
import time
import random
def main(stdscr): def main(stdscr):
curses.curs_set(0) # Hide cursor curses.curs_set(0) # Hide the cursor for a cleaner look
stdscr.nodelay(True) # Don't block I/O calls stdscr.nodelay(True) # Non-blocking input to keep the game flowing
stdscr.timeout(100) # Refresh rate h, w = 20, 40 # Dimensions of the game board
board = np.zeros((h, w), dtype=int) # Game board initialization
# Initialize the board snake = [(h // 2, w // 2)] # Starting position of the snake
h, w = 40, 80 # Board dimensions board[h // 2, w // 2] = 1 # Mark the snake's start on the board
board = np.zeros((h, w), dtype=int)
snake = [(h // 2, w // 2)]
board[h // 2, w // 2] = 1
food = (random.randint(0, h - 1), random.randint(0, w - 1)) food = (random.randint(0, h - 1), random.randint(0, w - 1))
board[food] = 2 # Food position board[food] = 2 # Place initial food
direction = (0, 1) # Initial direction (right) direction = (0, 1) # Initial movement direction
last_update_time = time.time()
move_interval = 0.1 # Base move interval in seconds
vertical_adjustment = 2 # Vertical moves occur every this number of base intervals
while True: while True:
# Input handling
key = stdscr.getch() key = stdscr.getch()
if key in [ord("q"), ord("Q")]: if key in [ord("Q"), ord("q")]:
break break
elif key in [ord("a"), ord("A")] and direction != (0, 1): if key in [ord("a"), ord("A")] and direction != (0, 1):
direction = (0, -1) direction = (0, -1)
elif key in [ord("d"), ord("D")] and direction != (0, -1): if key in [ord("d"), ord("D")] and direction != (0, -1):
direction = (0, 1) direction = (0, 1)
elif key in [ord("w"), ord("W")] and direction != (1, 0): if key in [ord("w"), ord("W")] and direction != (1, 0):
direction = (-1, 0) direction = (-1, 0)
elif key in [ord("s"), ord("S")] and direction != (-1, 0): if key in [ord("s"), ord("S")] and direction != (-1, 0):
direction = (1, 0) direction = (1, 0)
elif key == ord(" "): # Pause
stdscr.nodelay(False)
stdscr.getch()
stdscr.nodelay(True)
# Update snake position current_time = time.time()
head = (snake[0][0] + direction[0], snake[0][1] + direction[1]) time_elapsed = current_time - last_update_time
head = (head[0] % h, head[1] % w) # Torus wrapping
if head in snake: # Check self-collision # Adjust move interval based on direction
break current_interval = move_interval * (
vertical_adjustment if direction in [(-1, 0), (1, 0)] else 1
)
snake.insert(0, head) # Move head if time_elapsed >= current_interval:
if head == food: # Check food collision new_head = (snake[0][0] + direction[0], snake[0][1] + direction[1])
food = (random.randint(0, h - 1), random.randint(0, w - 1)) new_head = (new_head[0] % h, new_head[1] % w) # Toroidal field logic
while food in snake:
if new_head in snake:
break # Collision detection
snake.insert(0, new_head)
if new_head == food:
food = (random.randint(0, h - 1), random.randint(0, w - 1)) food = (random.randint(0, h - 1), random.randint(0, w - 1))
else: while food in snake:
tail = snake.pop() food = (random.randint(0, h - 1), random.randint(0, w - 1))
board[tail] = 0 else:
tail = snake.pop()
board[tail] = 0
board[head] = 1 board[new_head] = 1
board[food] = 2 board[food] = 2
last_update_time = current_time # Reset the timer
# Draw the board # Clear and redraw the board
stdscr.clear() stdscr.clear()
for i in range(h): for i in range(h):
for j in range(w): for j in range(w):
char = " " if board[i, j] == 0 else "*" if board[i, j] == 1 else "O" if board[i, j] == 1:
stdscr.addch(i, j, char) stdscr.addch(i, j, "X")
stdscr.refresh() elif board[i, j] == 2:
stdscr.addch(i, j, "O")
else:
stdscr.addch(i, j, " ")
stdscr.refresh()
curses.wrapper(main) curses.wrapper(main)