Repository navigation
Expand file tree
/
Copy pathcontroller.py
More file actions
107 lines (89 loc) · 4.06 KB
/
Copy pathcontroller.py
File metadata and controls
107 lines (89 loc) · 4.06 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
# the controller takes a keyboard or mouse event from the main game loop and performs
# the corresponding action
# the controller controls the player's ship and the main menu
import pygame
import ship
from math import degrees
from math import atan2
import gameState
class Controller:
def __init__(self, screenSize, mMenu, pMenu, gameOverScreen):
playerShipImage = pygame.image.load("images/myplane2_1.png")
playerAnimation = pygame.image.load("images/myplane2_1.png")
self.player = ship.AdvancedShip(playerShipImage, playerAnimation, screenSize[0] / 2, screenSize[1] / 2, 3)
self.mMenu = mMenu
self.pMenu = pMenu
self.gameOverScreen = gameOverScreen
# returns True or False, True if game is to exit, False if not
def handleInputs(self, state):
# to be sure the ship angle is constantly looking at the mouse cursor
if state == gameState.GameState.PLAYING:
self.setShipAngle()
# or to indicate the menu button that is being hovered over by the mouse
elif state != gameState.GameState.GAME_EXIT:
self.setActiveMenuBtn(state)
# handle for mouse buttons being held down
# ------------------------------------------
(m1,m2,m3) = pygame.mouse.get_pressed() # right mouse button = m3, left mouse button = m1, middle mouse = m2
if m3 == True and state == gameState.GameState.PLAYING: # if the right mouse button is held down
self.player.accelerate()
if m1 == True and state == gameState.GameState.PLAYING: # if the left mouse button is held down
self.player.fire()
# ------------------------------------------
# handle for key press or single mouse click
# ------------------------------------------
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: # access pause menu or resume game depending on state of the game
if state == gameState.GameState.PLAYING:
state = gameState.GameState.PAUSE_MENU
elif state == gameState.GameState.PAUSE_MENU:
state = gameState.GameState.PLAYING
# single mouse click is only relevent inside the menus
else:
if event.type == pygame.MOUSEBUTTONDOWN: # handle single mouse click
state = self.checkMenuBtnClick(state)
# ------------------------------------------
return state
# gets mouse position on the screen and
# determines angle of the mouse relative to the screen center
# then sets the angle of the ship equal to the computed angle
def setShipAngle(self):
mousePos = pygame.mouse.get_pos()
# atan2 is used when the signs of both x and y are known
self.player.angle = (-1 * degrees(atan2(mousePos[1] - self.player.y, mousePos[0] - self.player.x))) - 90
def setActiveMenuBtn(self, state):
mousePos = pygame.mouse.get_pos()
if state == gameState.GameState.PAUSE_MENU:
for key, btn in self.pMenu.items(): # loop to iterate over dictionary
if btn.sprite.collidepoint(mousePos):
btn.hover()
else:
btn.setInactive()
elif state == gameState.GameState.MAIN_MENU:
for key, btn in self.mMenu.items(): # loop to iterate over dictionary
if btn.sprite.collidepoint(mousePos):
btn.hover()
else:
btn.setInactive()
elif state == gameState.GameState.GAME_OVER:
for key, btn in self.gameOverScreen.items(): # loop to iterate over dictionary
if btn.sprite.collidepoint(mousePos):
btn.hover()
else:
btn.setInactive()
def checkMenuBtnClick(self, state):
mousePos = pygame.mouse.get_pos()
if state == gameState.GameState.PAUSE_MENU:
for key, btn in self.pMenu.items(): # loop to iterate over dictionary
if btn.sprite.collidepoint(mousePos):
state = btn.onClick(state)
elif state == gameState.GameState.MAIN_MENU:
for key, btn in self.mMenu.items(): # loop to iterate over dictionary
if btn.sprite.collidepoint(mousePos):
state = btn.onClick(state)
elif state == gameState.GameState.GAME_OVER:
for key, btn in self.gameOverScreen.items(): # loop to iterate over dictionary
if btn.sprite.collidepoint(mousePos):
state = btn.onClick(state)
return state