aboutsummaryrefslogtreecommitdiff
path: root/src/Game.py
blob: aedfa0c84854abb1f8719382cc49f9be75e2045d (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
84
85
86
87
88
89
90
91
92
93
94
95
import Player
from Direction import DIRECTION
import settings as Settings
import pygame

class Game():
    def __init__(self):
        self.settings = Settings.settings

    def init(self):
        # Initialize Pygame
        pygame.init()

        # Set the dimensions of the window
        screen = pygame.display.set_mode((Settings.settings.width, Settings.settings.height))

        # Sprite sheet for pacman
        sprite_sheet = pygame.image.load('../assets/pacman_left_sprite.png').convert_alpha();

        player = Player.Player(sprite_sheet)

        # Set the circle's velocity
        dx = 0
        dy = 0

        # counter used to cycle through pacman sprite animation
        counter = 0

        clock = pygame.time.Clock()

        # Sprite sheet for pacman
        sprite_sheet = pygame.image.load('../assets/pacman_left_sprite.png').convert_alpha();

        sprite_width, sprite_height = 32, 32



        # Checks collision with walls
        def check_collision(dx, dy):
            return player.y + sprite_height + dy > Settings.settings.height or player.y + dy < 0 or player.x + sprite_width + dx > Settings.settings.width or player.x + dx < 0 

        # Main game loop
        running = True


        while running:
            # setting game fps
            clock.tick(Settings.settings.fps)

            # counter logic for cycling between pacman different sprites
            if counter < 19:
                counter += 1
            else:
                counter = 0

            # Handling events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                elif event.type == pygame.KEYDOWN:
                    # Move the circle based on the pressed key
                    if event.key == pygame.K_w:
                        player.direction = DIRECTION.UP
                        dy = -player.speed
                        dx = 0 # Necssarry to move only horizontal or vertical
                    elif event.key == pygame.K_s:
                        player.direction = DIRECTION.DOWN
                        dy = player.speed
                        dx = 0 # Necssarry to move only horizontal or vertical
                    elif event.key == pygame.K_a:
                        player.direction = DIRECTION.LEFT
                        dx = -player.speed
                        dy = 0 # Necssarry to move only horizontal or vertical
                    elif event.key == pygame.K_d:
                        player.direction = DIRECTION.RIGHT
                        dx = player.speed
                        dy = 0 # Necssarry to move only horizontal or vertical


            # print(player.direction)
            # Update the circle's position and checking for collisions
            if not check_collision(dx, dy):
                player.x += dx
                player.y += dy

            screen.fill((0, 0, 0)) # Clear the screen

            player.draw(screen, counter)

            # Update the screen
            pygame.display.flip()


        # Quit Pygame
        pygame.quit()