-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.py
55 lines (50 loc) · 1.82 KB
/
controls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import pygame
from config import SNAKE_BLOCK_SIZE
class Controls():
"""
This class handles the inputs translating them into gameplay related
output
"""
def __init__(self):
self.x1_change = 0
self.y1_change = 0
self.mx = 0
self.my = 0
self.pause = False
def stop(self):
self.x1_change = 0
self.y1_change = 0
def handle_input(self):
self.mx, self.my = pygame.mouse.get_pos()
keys = pygame.key.get_pressed()
x1_change_old = self.x1_change
y1_change_old = self.y1_change
if keys[pygame.K_LEFT] and keys[pygame.K_UP]:
self.x1_change = -SNAKE_BLOCK_SIZE
self.y1_change = -SNAKE_BLOCK_SIZE
elif keys[pygame.K_LEFT] and keys[pygame.K_DOWN]:
self.x1_change = -SNAKE_BLOCK_SIZE
self.y1_change = SNAKE_BLOCK_SIZE
elif keys[pygame.K_RIGHT] and keys[pygame.K_UP]:
self.x1_change = SNAKE_BLOCK_SIZE
self.y1_change = -SNAKE_BLOCK_SIZE
elif keys[pygame.K_RIGHT] and keys[pygame.K_DOWN]:
self.x1_change = SNAKE_BLOCK_SIZE
self.y1_change = SNAKE_BLOCK_SIZE
elif keys[pygame.K_LEFT]:
self.x1_change = -SNAKE_BLOCK_SIZE
self.y1_change = 0
elif keys[pygame.K_RIGHT]:
self.x1_change = SNAKE_BLOCK_SIZE
self.y1_change = 0
elif keys[pygame.K_UP]:
self.x1_change = 0
self.y1_change = -SNAKE_BLOCK_SIZE
elif keys[pygame.K_DOWN]:
self.x1_change = 0
self.y1_change = SNAKE_BLOCK_SIZE
elif keys[pygame.K_ESCAPE]:
self.pause = True
if y1_change_old == -self.y1_change and x1_change_old == -self.x1_change:
self.y1_change = y1_change_old
self.x1_change = x1_change_old