-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
53 lines (36 loc) · 1.52 KB
/
bot.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
import pygame
from joystick_handler import JoystickHandler
class Bot(pygame.sprite.Sprite):
def __init__(self, game, x, y, joystick_handler, id):
self.game = game
self.groups = self.game.robots
pygame.sprite.Sprite.__init__(self, self.groups)
self.original_image = pygame.image.load("bot.png").convert()
self.original_image.set_colorkey("white")
self.image = self.original_image.copy()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.angle = 0
self.x_change = 0
self.y_change = 0
self.joystick_handler = joystick_handler
self.joystick = self.joystick_handler.get_joystick(id)
def update(self):
self.movement()
self.rotate()
self.rect.x += self.x_change
self.rect.y += self.y_change
self.x_change = 0
self.y_change = 0
def movement(self):
self.x_change += self.joystick_handler.get_ajusted_axis(self.joystick, 0)
self.y_change += self.joystick_handler.get_ajusted_axis(self.joystick, 1)
def rotate(self):
# Get the current joystick angle
self.angle += -self.joystick_handler.get_ajusted_axis(self.joystick, 2) * .35
# Rotate the image
self.image = pygame.transform.rotate(self.original_image, self.angle)
# Get the rect of the rotated image and update its position
self.rect = self.image.get_rect(center=self.rect.center)
self.angle %= 360