Reputation: 38
I'm writing a snake game code in pygame as a new programmer . I chose to work with a list of tuples and move them by pressing keys on keyboard, but it has some problems in moving. I don't know how to make it continue moving when I press a key. I tried using event.get() instead key.pressed() and working with while loop, but it still doesn't work...
import pygame
pygame.init()
w, h = 500, 500
screen = pygame.display.set_mode((w, h))
c = pygame.time.Clock()
running = True
screen.fill((255,255,255))
x1 , x2 , x3 = 80 , 100 , 120
y1 , y2 , y3 = 80 , 80 , 80
rc = 10
speed = 20
climax = [ (x1 , y1 ) , (x2 , y2), (x3 , y3)]
snake_color = (0,255,0)
while running:
c.tick(20)
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
running = False
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[-1]) , rc , 0)
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] :
del climax[2]
climax.insert(2 , climax[1])
climax.insert(1 , climax[0])
climax.insert(0 , (climax[1][0]-speed , climax[1][1]))
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)
if key[pygame.K_UP]:
del climax[2]
climax.insert(2 , climax[1])
climax.insert(1 , climax[0])
climax.insert(0 , (climax[0][0] , climax[0][1]-speed))
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)
if key[pygame.K_DOWN]:
del climax[2]
climax.insert(2 , climax[1])
climax.insert(1 , climax[0])
climax.insert(0 , (climax[0][0] , climax[0][1]+speed))
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)
if key[pygame.K_RIGHT]:
del climax[0]
climax.insert(0 , climax[1])
climax.insert(1 , climax[2])
climax.insert(2 , (climax[1][0]+speed , climax[1][1]))
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)
pygame.display.update()
pygame.quit()
quit()
Upvotes: 1
Views: 116
Reputation: 100
it seems that your move algorithm is wrong and for pygame.event.get(), it will
empty the event queue after your call it. below code should work
import pygame
pygame.init()
w, h = 500, 500
screen = pygame.display.set_mode((w, h))
c = pygame.time.Clock()
pygame.key.set_repeat(100)
running = True
screen.fill((255, 255, 255))
x1 , x2 , x3 = 70 , 90 , 110
y1 , y2 , y3 = 90 , 90 , 90
rc = 10
speed = 20
climax = [ (x1 , y1 ) , (x2 , y2), (x3 , y3)]
snake_color = (0,255,0)
head = len(climax)-1
def move_forward(climax, head, movedir):
if head == 0:
for i in range(len(climax)-1, 0, -1):
print('{} --> {}'.format(climax[i], climax[i-1]))
climax[i] = climax[i-1]
elif head == len(climax)-1:
for i in range(len(climax)-1):
print('{} --> {}'.format(climax[i+1], climax[i]))
climax[i] = climax[i+1]
if movedir == 'left':
climax[head] = (climax[head][0]-speed, climax[head][1])
elif movedir == 'right':
climax[head] = (climax[head][0]+speed, climax[head][1])
elif movedir == 'up':
climax[head] = (climax[head][0], climax[head][1]-speed)
elif movedir == 'down':
climax[head] = (climax[head][0], climax[head][1]+speed)
print(climax)
def draw_circle(screen, snake_color, climax):
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[2]) , rc , 0)
while running:
c.tick(20)
screen.fill((0, 0, 0))
pygame.draw.circle(screen , snake_color , (climax[0]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[1]) , rc , 0)
pygame.draw.circle(screen , snake_color , (climax[-1]) , rc , 0)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_LEFT:
if climax[0][0] < climax[-1][0]:
head = 0
elif climax[0][0] > climax[-1][0]:
head = len(climax)-1
print('head is {}'.format(head))
if climax[head][0] > rc:
move_forward(climax, head, 'left')
draw_circle(screen, snake_color, climax)
break
elif event.key == pygame.K_UP:
if climax[0][1] < climax[-1][1]:
head = 0
elif climax[0][1] > climax[-1][1]:
head = len(climax)-1
print('head is {}'.format(head))
if climax[head][1] > rc:
move_forward(climax, head, 'up')
draw_circle(screen, snake_color, climax)
break
elif event.key == pygame.K_DOWN:
if climax[0][1] > climax[-1][1]:
head = 0
elif climax[0][1] < climax[-1][1]:
head = len(climax)-1
print('head is {}'.format(head))
if climax[head][1] < h-rc:
move_forward(climax, head, 'down')
draw_circle(screen, snake_color, climax)
break
elif event.key == pygame.K_RIGHT:
if climax[0][0] > climax[-1][0]:
head = 0
elif climax[0][0] < climax[-1][0]:
head = len(climax)-1
print('head is {}'.format(head))
if climax[head][0] < w-rc:
move_forward(climax, head, 'right')
draw_circle(screen, snake_color, climax)
break
pygame.display.update()
pygame.quit()
quit() `enter code here`
Upvotes: 2