generated from notaSWE/pygameproject
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
75 lines (61 loc) · 2.61 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
from ball import Ball
from board import *
from multis import *
from settings import *
import ctypes, pygame, pymunk, random, sys
# Maintain resolution regardless of Windows scaling settings
ctypes.windll.user32.SetProcessDPIAware()
class Game:
def __init__(self):
# General setup
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE_STRING)
self.clock = pygame.time.Clock()
self.delta_time = 0
# Pymunk
self.space = pymunk.Space()
self.space.gravity = (0, 1800)
# Plinko setup
self.ball_group = pygame.sprite.Group()
self.board = Board(self.space)
# Debugging
self.balls_played = 0
def run(self):
self.start_time = pygame.time.get_ticks()
while True:
# Handle quit operation
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
# Get the position of the mouse click
mouse_pos = pygame.mouse.get_pos()
# Check if the mouse click position collides with the image rectangle
if self.board.play_rect.collidepoint(mouse_pos):
self.board.pressing_play = True
else:
self.board.pressing_play = False
# Spawn ball on left mouse button release
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1 and self.board.pressing_play:
mouse_pos = pygame.mouse.get_pos()
if self.board.play_rect.collidepoint(mouse_pos):
random_x = WIDTH//2 + random.choice([random.randint(-20, -1), random.randint(1, 20)])
click.play()
self.ball = Ball((random_x, 20), self.space, self.board, self.delta_time)
self.ball_group.add(self.ball)
self.board.pressing_play = False
else:
self.board.pressing_play = False
self.screen.fill(BG_COLOR)
# Time variables
self.delta_time = self.clock.tick(FPS) / 1000.0
# Pymunk
self.space.step(self.delta_time)
self.board.update()
self.ball_group.update()
pygame.display.update()
if __name__ == '__main__':
game = Game()
game.run()