-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_game.py
125 lines (81 loc) · 2.83 KB
/
example_game.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
import pygame
# --- constants --- (UPPER_CASE names)
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 600
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
FPS = 30
# --- classses --- (CamelCase names)
# empty
# --- functions --- (lower_case names)
# empty
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
#screen_rect = screen.get_rect()
pygame.display.set_caption("Puzzle Solver")
# - objects -
rectangle = pygame.rect.Rect(176, 134, 17, 17)
rectangle_draging = False
class ImagePiece(pygame.sprite.Sprite):
def __init__(self, pos=(0, 0)):
super(ImagePiece, self).__init__()
path = r'.\\image_processing\\pieces\\game_images\\Piece 0.png'
self.original_image = pygame.image.load(path).convert()
self.image = self.original_image # This will reference our rotated image.
self.rect = self.image.get_rect().move(pos)
self.angle = 0
self.chosen=False
def update(self, angle):
self.image = pygame.transform.rotate(self.original_image, self.angle)
self.angle += angle % 360 # Value will reapeat after 359. This prevents angle to overflow.
image = ImagePiece()
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(image)
# - mainloop -
clock = pygame.time.Clock()
running = True
rotating = 0
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if rectangle.collidepoint(event.pos):
rectangle_draging = True
mouse_x, mouse_y = event.pos
offset_x = rectangle.x - mouse_x
offset_y = rectangle.y - mouse_y
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
rectangle_draging = False
elif event.type == pygame.MOUSEMOTION:
if rectangle_draging:
mouse_x, mouse_y = event.pos
rectangle.x = mouse_x + offset_x
rectangle.y = mouse_y + offset_y
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rotating = 1
if event.key == pygame.K_RIGHT:
rotating = -1
elif event.type == pygame.KEYUP: # Added keyup
if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:
rotating = 0
if rotating != 0:
image.update(rotating)
# - updates (without draws) -
# empty
# - draws (without updates) -
screen.fill(WHITE)
pygame.draw.rect(screen, RED, rectangle)
all_sprites_list.draw(screen)
pygame.display.flip()
# - constant game speed / FPS -
clock.tick(FPS)
# - end -
pygame.quit()