-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·203 lines (144 loc) · 5.74 KB
/
main.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import pygame
pygame.init()
#*GLOBALS_____________________________________________________
global screen
#*PRE-SETUP____________________________________________________
#small variables for screen size
original_resolution = (800, 600)
aspect_ratio = original_resolution[0] / original_resolution[1]
#initialize the screen
screen = pygame.display.set_mode(original_resolution, pygame.RESIZABLE)
clock = pygame.time.Clock()
pygame.mixer.init()
#*VARIABLES_____________________________________________________
sound_played1 = False
sound_played2 = False
screencount = 0
P1 = {
'name': 'Amie',
'x': 133,
'y': 270,
'speed': 5,
'image': pygame.image.load('characters/amisita.png'),
'image2': pygame.image.load('characters/amisitalewd.png'),
'alt': False,
'last_direction': 'right',
'directions': {
'up': pygame.K_w,
'down': pygame.K_s,
'left': pygame.K_a,
'right': pygame.K_d,
},
'can_move': True
}
P2 = {
'name': 'Felix',
'x': 600,
'y': 270,
'speed': 5,
'image': pygame.image.load('characters/Felixito.png'),
'image2': pygame.image.load('characters/Felixitoblush.png'),
'alt': False,
'last_direction': 'left',
'directions': {
'up': pygame.K_UP,
'down': pygame.K_DOWN,
'left': pygame.K_LEFT,
'right': pygame.K_RIGHT,
},
'can_move': True
}
circleX1, circleY1 = original_resolution[0] / 2, original_resolution[1] / 2
circleX2, circleY2 = original_resolution[0] / 2, original_resolution[1] / 2
speed = 3
#*FUNCTIONS_____________________________________________________
# @params: directions: dictionary of directions and their respective keys
# @params: keys: the current state of all keyboard keys
# @params: circleX: the x position of the circle
# @params: circleY: the y position of the circle
# @params: speed: the speed at which the circle moves
# @returns: the new x and y position of the circle
def handle_movement(player, keys):
if not player['can_move']:
return
for direction, key in player['directions'].items():
if keys[key]:
print(f"{direction} arrow key is pressed")
if direction == 'right':
player['x'] += player['speed']
if player['last_direction'] != 'right':
player['image'] = pygame.transform.flip(player['image'], True, False)
player['image2'] = pygame.transform.flip(player['image2'], True, False)
player['last_direction'] = 'right'
elif direction == 'left':
player['x'] -= player['speed']
if player['last_direction'] != 'left':
player['image'] = pygame.transform.flip(player['image'], True, False)
player['image2'] = pygame.transform.flip(player['image2'], True, False)
player['last_direction'] = 'left'
elif direction == 'up':
player['y'] -= player['speed']
elif direction == 'down':
player['y'] += player['speed']
#render handler
def render_handler():
screen.fill((255, 255, 255))
if P1['alt']:
screen.blit(P1['image2'], (P1['x'], P1['y']))
else:
screen.blit(P1['image'], (P1['x'], P1['y']))
if P2['alt']:
screen.blit(P2['image2'], (P2['x'], P2['y']))
else:
screen.blit(P2['image'], (P2['x'], P2['y']))
pass
#*SETUP_________________________________________________________
pygame.display.set_caption("Prototype") #? Set the title of the window
#? icon = pygame.image.load("assets/icon.png") #? Load the icon
#? pygame.display.set_icon(icon) #? Set the icon
#! _____________________________Main game loop__________________________________
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
elif event.key == pygame.K_F2:
# we resize the window to 2x its size
if screencount < 2:
width, height = screen.get_size()
screen = pygame.display.set_mode((width * 1.5, height * 1.5), pygame.RESIZABLE)
screencount += 1
else:
screencount = 0
screen = pygame.display.set_mode(original_resolution, pygame.RESIZABLE)
elif event.key == pygame.K_SPACE:
# toggle bool value alt from P1 between True and False
if P1['alt']:
P1['alt'] = False
else:
P1['alt'] = True
keys = pygame.key.get_pressed() # get the state of all keyboard keys
handle_movement(P1, keys)
handle_movement(P2, keys)
P1hitbox = pygame.Rect(P1['x'], P1['y'], P1['image'].get_width() - 50, P1['image'].get_height())
P1ALTbox = pygame.Rect(P1['x'], P1['y'], P1['image'].get_width() + 100, P1['image'].get_height() + 31)
P2ALTbox = pygame.Rect(P2['x'], P2['y'], P2['image'].get_width() + 100, P2['image'].get_height() + 31)
P2hitbox = pygame.Rect(P2['x'], P2['y'], P2['image'].get_width() - 50, P2['image'].get_height())
#if alt is true we make a colision check p1 with p2 alt hitbox
if P1['alt']:
if P1ALTbox.colliderect(P2ALTbox):
print("alt collided with alt")
P2['alt'] = True
else:
P2['alt'] = False
if P1hitbox.colliderect(P2hitbox):
print("Collision detected!")
render_handler()
# *after* drawing everything, flip the display
clock.tick(60)
pygame.display.flip()
# !End of main game loop
pygame.quit()