Skip to content

Commit 0d23c07

Browse files
committed
mask-collision
1 parent b49a033 commit 0d23c07

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

pygame/mask-collision/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Mask collision
2+
3+
Not collide
4+
5+
![#1](images/not-collide.png?raw=true)
6+
7+
Collide
8+
9+
![#1](images/collide.png?raw=true)
10+
9.3 KB
Loading
9.29 KB
Loading
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# pygame (empty) template - by furas
5+
#
6+
7+
# ---------------------------------------------------------------------
8+
9+
import pygame
10+
11+
# === CONSTANS === (UPPER_CASE names)
12+
13+
BLACK = ( 0, 0, 0)
14+
WHITE = (255, 255, 255)
15+
16+
RED = (255, 0, 0)
17+
GREEN = ( 0, 255, 0)
18+
BLUE = ( 0, 0, 255)
19+
20+
SCREEN_WIDTH = 600
21+
SCREEN_HEIGHT = 400
22+
23+
# === CLASSES === (CamelCase names)
24+
25+
class Image(pygame.sprite.Sprite):
26+
27+
def __init__(self, screen_rect, angle=15, color=(255,0,0), rotate=5):
28+
super().__init__()
29+
30+
self.angle = angle
31+
self.rotate = rotate
32+
33+
self.original_image = pygame.Surface((100, 25)).convert_alpha()
34+
self.original_image.fill(color)
35+
self.original_image = pygame.transform.scale(self.original_image, (250,10))
36+
37+
self.change_angle = pygame.time.get_ticks()
38+
39+
self.update()
40+
41+
def update(self):
42+
if self.change_angle <= pygame.time.get_ticks():
43+
self.angle = (self.angle + self.rotate) % 360
44+
45+
self.image = pygame.transform.rotate(self.original_image, self.angle)
46+
self.rect = self.image.get_rect(center=screen_rect.center)
47+
self.mask = pygame.mask.from_surface(self.image)
48+
49+
self.olist = self.mask.outline()
50+
51+
#self.mask_image = pygame.Surface(self.rect.size, masks=self.mask) # ??? doesn't work as I expected
52+
53+
if self.olist:
54+
pygame.draw.lines(self.image, (0, 0, 0), 3, self.olist)
55+
56+
self.change_angle = pygame.time.get_ticks() + 50
57+
58+
def draw(self, screen):
59+
screen.blit(self.image, self.rect)
60+
#screen.blit(self.mask_image, self.rect) # ??? doesn't work
61+
62+
# === FUNCTIONS === (lower_case names)
63+
64+
# empty
65+
66+
# === MAIN === (lower_case names)
67+
68+
# --- (global) variables ---
69+
70+
# --- init ---
71+
72+
pygame.init()
73+
74+
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
75+
screen_rect = screen.get_rect()
76+
77+
# --- objects ---
78+
79+
img1 = Image(screen_rect, 45, (0,255,0))
80+
img2 = Image(screen_rect, 0, (0,255,255), 0)
81+
82+
# --- mainloop ---
83+
84+
clock = pygame.time.Clock()
85+
is_running = True
86+
87+
while is_running:
88+
89+
# --- events ---
90+
91+
for event in pygame.event.get():
92+
93+
# --- global events ---
94+
95+
if event.type == pygame.QUIT:
96+
is_running = False
97+
elif event.type == pygame.KEYDOWN:
98+
if event.key == pygame.K_ESCAPE:
99+
is_running = False
100+
elif event.type == pygame.MOUSEMOTION:
101+
img2.rect.center = event.pos
102+
103+
# --- objects events ---
104+
105+
# empty
106+
107+
# --- updates ---
108+
109+
img1.update()
110+
111+
if pygame.sprite.collide_mask(img1, img2):
112+
bg_color = (255,200,200)
113+
else:
114+
bg_color = WHITE
115+
116+
# --- draws ---
117+
118+
screen.fill(bg_color)
119+
120+
img1.draw(screen)
121+
img2.draw(screen)
122+
123+
pygame.display.update()
124+
125+
# --- FPS ---
126+
127+
clock.tick(25)
128+
129+
# --- the end ---
130+
131+
pygame.quit()

pygame/music-playlist/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
I try to use [queue()](http://pygame.org/docs/ref/music.html#pygame.mixer.music.queue) but it doesn't work for me.
2+
Maybe it needs `for event` loop to work but it doesn't work for me too.
3+
4+
In documentation below [queue()](http://pygame.org/docs/ref/music.html#pygame.mixer.music.queue) I found this comment:
5+
6+
> This method only queues one music file.
7+
> If you call it and there already is a queued file, it will be overrided.
8+
9+
so queue is not so usefull.
10+
11+
I use [pygame.mixer.music.set_endevent()](http://pygame.org/docs/ref/music.html#pygame.mixer.music.set_endevent) with `for event` loop to start next track.
12+
13+

pygame/music-playlist/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pygame
2+
3+
NEXT = pygame.USEREVENT + 1
4+
5+
playlist = [
6+
"Muzyka/efect/Shotgun 12 gauge sPAs-12 semi automatic single shot distant.mpeg",
7+
"Muzyka/efect/Shotgun blast shot_BLASTWAVEFX_31658.mp3",
8+
'track_one.ogg',
9+
'track_two.ogg',
10+
'track_three.ogg',
11+
'track_four.ogg',
12+
]
13+
14+
tracks_number = len(playlist)
15+
current_track = 0
16+
17+
pygame.init() # need it for event loop
18+
#screen = pygame.display.set_mode((800,600)) # it can be useful to stop program
19+
20+
pygame.mixer.init(frequency = 48000)
21+
22+
# start first track
23+
pygame.mixer.music.load(playlist[current_track])
24+
pygame.mixer.music.play()
25+
26+
# send event NEXT when tracks ends
27+
pygame.mixer.music.set_endevent(NEXT)
28+
29+
running = True
30+
while running:
31+
32+
for event in pygame.event.get():
33+
if event.type == pygame.QUIT:
34+
running = False
35+
36+
elif event.type == NEXT:
37+
38+
# get next track (modulo number of tracks)
39+
current_track = (current_track + 1) % tracks_number
40+
41+
print("Play:", playlist[current_track])
42+
43+
pygame.mixer.music.load(playlist[current_track])
44+
pygame.mixer.music.play()
45+
46+
47+
pygame.quit()
48+

0 commit comments

Comments
 (0)