summaryrefslogtreecommitdiff
path: root/main.py
blob: 485df6f545d1d945751f1247e369db512bcb5bb9 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
import pygame
import sys

from lib.Environment import EnvironmentController

class Game(object):
    def __init__(self, w=1280, h=720, cap=60, fullscreen=False):
        self.width, self.height = [w, h]
        os.environ['SDL_VIDEO_CENTERED'] = '1'

        pygame.display.set_caption('Controller Keyboard')
        if fullscreen:
            self.screen = pygame.display.set_mode((self.width, self.height), pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF)
        else:
            self.screen = pygame.display.set_mode((self.width, self.height))

        print('[info] pre pygame init')
        pygame.init()
        pygame.mixer.quit()
        pygame.joystick.init()
        print('[info] post pygame init')

        self.tps = cap
        self.clock = pygame.time.Clock()

        self.controller = EnvironmentController(self.clock)

    def main(self):
        running = True

        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                    break
                elif event.type == pygame.KEYDOWN:
                    if event.key is pygame.K_ESCAPE:
                        running = False
                        break
                    else:
                        self.controller.do_keydown(event)
                elif event.type == pygame.KEYUP:
                    self.controller.do_keyup(event)
                elif event.type == pygame.JOYAXISMOTION:
                    ## All values are received and processed in the update()
                    ## call.  Handling this event wouldn't change anything,
                    ## it would only complicate things.
                    pass

                ## TODO: pass these off to the controller to be passed off to
                ##       the controller keyboard which gets passed off to the
                ##       JoyHandler.
                elif event.type == pygame.JOYBUTTONDOWN:
                    self.controller.do_joydown(event)
                elif event.type == pygame.JOYBUTTONUP:
                    self.controller.do_joyup(event)

                ## Send the focus state to the controller.
                elif event.type == pygame.ACTIVEEVENT:
                    if event.state == 2:
                        self.controller.set_focus(False)
                    elif event.state == 6:
                        self.controller.set_focus(True)

            if running == False:
                continue

            self.controller.update()
            self.controller.draw(self.screen)
            pygame.display.flip()
            if self.tps > 0:
                self.clock.tick(self.tps)

        else:
            pygame.joystick.quit()
            pygame.quit()

if __name__ == '__main__':
    g = Game(cap=5000, w=720, h=720)
    g.main()