Reputation: 15
Here is the beginnings of my code for a snake game. There is an annoying delay between pressing an arrow key and the snake's actual turning.
It's hard to turn accurately and grab the 'apple'.
I'd like for this to be more responsive. How can I tweak my code to make the turning more responsive?
import random
import pygame
pygame.init()
#----------------------------
# CONSTANTS
#----------------------------
window_width = 800
window_height = 800
segment_size = 24
win = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('Snake Game')
#----------------------------
# CLASSES
#----------------------------
class Segment:
def __init__(self, x, y):
self.x = x
self.y = y
self.width = segment_size
self.height = segment_size
self.vel = segment_size
self.dir = 'right'
def move(self):
if self.dir == 'right':
self.x += self.vel
elif self.dir == 'left':
self.x -= self.vel
elif self.dir == 'up':
self.y -= self.vel
else:
self.y += self.vel
class Apple:
def __init__(self):
self.x = (random.randint(0, window_width//segment_size)) * segment_size
self.y = (random.randint(0, window_height//segment_size)) * segment_size
self.width = segment_size
self.height = segment_size
self.vel = segment_size
#----------------------------
# FUNCTIONS
#----------------------------
def draw_grid(win):
for x in range (0, window_width, segment_size):
pygame.draw.line(win, (75, 75, 75), (x, 0), (x, window_height), 1)
for y in range (0, window_height, segment_size):
pygame.draw.line(win, (75, 75, 75), (0, y), (window_width, y), 1)
#----------------------------
# REDRAW WINDOW
#----------------------------
def redraw_window(win):
win.fill((0, 0, 0)) # fill screen with black
draw_grid(win) # draw grid
pygame.draw.rect(win, (255, 255, 255), (s.x, s.y, s.width, s.height)) # draw snake
pygame.draw.rect(win, (255, 0, 0), (a.x, a.y, a.width, a.height)) # draw apple
s.move() # move snake
pygame.display.update()
#----------------------------
# MAIN GAME LOOP
#----------------------------
clock = pygame.time.Clock()
segments = []
s = Segment(48, 48)
a = Apple()
running = True
while running:
clock.tick(30)
# listen for window closure
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# listen for key presses
keys = pygame.key.get_pressed()
for key in keys:
if keys[pygame.K_LEFT]:
s.dir = 'left'
if keys[pygame.K_RIGHT]:
s.dir = 'right'
if keys[pygame.K_UP]:
s.dir = 'up'
if keys[pygame.K_DOWN]:
s.dir = 'down'
redraw_window(win)
pygame.quit()
Is there anything else you would do here to optimize the code at this point. Thank you!
Upvotes: 2
Views: 191
Reputation: 490
Increase your clock rate from 30 to 60 or 90 as it will update your game more often:
clock.tick(90)
instead of
clock.tick(30)
just make sure to decrease the speed at which your snake is moving because increasing clock rate will mean more iterations of the game loop per second and therefore the snake's movement will be increased more frequently
Upvotes: 1
Reputation: 224
This:
# listen for key presses
keys = pygame.key.get_pressed()
for key in keys:
if keys[pygame.K_LEFT]:
s.dir = 'left'
if keys[pygame.K_RIGHT]:
s.dir = 'right'
if keys[pygame.K_UP]:
s.dir = 'up'
if keys[pygame.K_DOWN]:
s.dir = 'down'
is unnecessary.
You aren't using the key
variable so there is no point of having that for loop. Do this instead:
# listen for key presses
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
s.dir = 'left'
if keys[pygame.K_RIGHT]:
s.dir = 'right'
if keys[pygame.K_UP]:
s.dir = 'up'
if keys[pygame.K_DOWN]:
s.dir = 'down'
Upvotes: 1