Skip to content

Commit c7dd682

Browse files
committed
init commit
1 parent 8a6b471 commit c7dd682

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+6323
-0
lines changed

Chapter 10/10-1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def stereo_pan(x_coord, screen_width):
2+
3+
right_volume = float(x_coord) / screen_width
4+
left_volume = 1.0 - right_volume
5+
6+
return (left_volume, right_volume)

Chapter 10/10-2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tank.explode() # Do explosion visual
2+
explosion_channel = explosion_sound.play()
3+
if explosion_channel is not None:
4+
left, right = stereo_pan(tank.position.x, SCREEN_SIZE[0])
5+
explosion_channel.set_volume(left, right)

Chapter 10/10-3.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pygame.mixer.set_reserved(2)
2+
reserved_channel_0 = pygame.mixer.Channel(0)
3+
reserved_channel_1 = pygame.mixer.Channel(1)

Chapter 10/10-4.py

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import pygame
2+
from pygame.locals import *
3+
from random import randint
4+
from gameobjects.vector2 import Vector2
5+
6+
SCREEN_SIZE = (640, 480)
7+
8+
# In pixels per second, per second
9+
GRAVITY = 250.0
10+
# Increase for more bounciness, but don't go over 1!
11+
BOUNCINESS = 0.7
12+
13+
def stero_pan(x_coord, screen_width):
14+
15+
right_volume = float(x_coord) / screen_width
16+
left_volume = 1.0 - right_volume
17+
18+
return (left_volume, right_volume)
19+
20+
21+
class Ball(object):
22+
23+
def __init__(self, position, speed, image, bounce_sound):
24+
25+
self.position = Vector2(position)
26+
self.speed = Vector2(speed)
27+
self.image = image
28+
self.bounce_sound = bounce_sound
29+
self.age = 0.0
30+
31+
def update(self, time_passed):
32+
33+
w, h = self.image.get_size()
34+
35+
screen_width, screen_height = SCREEN_SIZE
36+
37+
x, y = self.position
38+
x -= w/2
39+
y -= h/2
40+
41+
# Has the ball bounce
42+
bounce = False
43+
44+
# Has the ball hit the bottom of the screen?
45+
if y + h >= screen_height:
46+
self.speed.y = -self.speed.y * BOUNCINESS
47+
self.position.y = screen_height - h / 2.0 - 1.0
48+
bounce = True
49+
50+
# Has the ball hit the left of the screen?
51+
if x <= 0:
52+
self.speed.x = -self.speed.x * BOUNCINESS
53+
self.position.x = w / 2.0 + 1
54+
bounce = True
55+
56+
# Has the ball hit the right of the screen
57+
elif x + w >= screen_width:
58+
self.speed.x = -self.speed.x * BOUNCINESS
59+
self.position.x = screen_width - w / 2.0 - 1
60+
bounce = True
61+
62+
# Do time based movement
63+
self.position += self.speed * time_passed
64+
# Add gravity
65+
self.speed.y += time_passed * GRAVITY
66+
67+
if bounce:
68+
self.play_bounce_sound()
69+
70+
self.age += time_passed
71+
72+
73+
def play_bounce_sound(self):
74+
75+
channel = self.bounce_sound.play()
76+
77+
if channel is not None:
78+
# Get the left and right volumes
79+
left, right = stero_pan(self.position.x, SCREEN_SIZE[0])
80+
channel.set_volume(left, right)
81+
82+
83+
def render(self, surface):
84+
85+
# Draw the sprite center at self.position
86+
w, h = self.image.get_size()
87+
x, y = self.position
88+
x -= w/2
89+
y -= h/2
90+
surface.blit(self.image, (x, y))
91+
92+
93+
def run():
94+
95+
# Initialise 44KHz 16-bit stero sound
96+
pygame.mixer.pre_init(44100, 16, 2, 1024*4)
97+
pygame.init()
98+
pygame.mixer.set_num_channels(8)
99+
screen = pygame.display.set_mode(SCREEN_SIZE, 0)
100+
101+
print(pygame.display.get_wm_info())
102+
hwnd = pygame.display.get_wm_info()["window"]
103+
x, y = (200, 200)
104+
105+
pygame.mouse.set_visible(False)
106+
107+
clock = pygame.time.Clock()
108+
109+
ball_image = pygame.image.load("ball.png").convert_alpha()
110+
mouse_image = pygame.image.load("mousecursor.png").convert_alpha()
111+
112+
# Load the sound file
113+
bounce_sound = pygame.mixer.Sound("bounce.wav")
114+
115+
balls = []
116+
117+
while True:
118+
119+
for event in pygame.event.get():
120+
121+
if event.type == QUIT:
122+
pygame.quit()
123+
quit()
124+
125+
if event.type == MOUSEBUTTONDOWN:
126+
127+
# Create a new ball at the mouse position
128+
random_speed = ( randint(-400, 400), randint(-300, 0) )
129+
new_ball = Ball( event.pos,
130+
random_speed,
131+
ball_image,
132+
bounce_sound )
133+
balls.append(new_ball)
134+
135+
time_passed_seconds = clock.tick() / 1000.
136+
137+
screen.fill((255, 255, 255))
138+
139+
dead_balls = []
140+
141+
for ball in balls:
142+
143+
ball.update(time_passed_seconds)
144+
ball.render(screen)
145+
146+
# Make not of any balls that are older than 10 seconds
147+
if ball.age > 10.0:
148+
dead_balls.append(ball)
149+
150+
# remove any 'dead' balls from the main list
151+
for ball in dead_balls:
152+
153+
balls.remove(ball)
154+
155+
# Draw the mouse cursor
156+
mouse_pos = pygame.mouse.get_pos()
157+
screen.blit(mouse_image, mouse_pos)
158+
159+
pygame.display.update()
160+
161+
if __name__ == "__main__":
162+
163+
run()

Chapter 10/10-5.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pygame.mixer.music.load("techno.ogg")
2+
pygame.mixer.music.play()

Chapter 10/10-6.py

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import pygame
2+
from pygame.locals import *
3+
4+
from math import sqrt
5+
import os
6+
import os.path
7+
8+
# Location of music on your computer
9+
MUSIC_PATH = "./MUSIC"
10+
SCREEN_SIZE = (800, 600)
11+
12+
def get_music(path):
13+
14+
# Get the filenames in a folder
15+
raw_filenames = os.listdir(path)
16+
17+
music_files = []
18+
for filename in raw_filenames:
19+
20+
# We only want ogg files
21+
if filename.endswith('.ogg'):
22+
music_files.append(os.path.join(MUSIC_PATH, filename))
23+
24+
return sorted(music_files)
25+
26+
27+
class Button(object):
28+
29+
def __init__(self, image_filename, position):
30+
31+
self.position = position
32+
self.image = pygame.image.load(image_filename)
33+
34+
def render(self, surface):
35+
36+
# Render at the center
37+
x, y = self.position
38+
w, h = self.image.get_size()
39+
x -= w /2
40+
y -= h / 2
41+
42+
surface.blit(self.image, (x, y))
43+
44+
45+
def is_over(self, point):
46+
47+
# Return True if a point is over the button
48+
point_x, point_y = point
49+
x, y = self.position
50+
w, h = self.image.get_size()
51+
x -= w /2
52+
y -= h / 2
53+
54+
in_x = point_x >= x and point_x < x + w
55+
in_y = point_y >= y and point_y < y + h
56+
57+
return in_x and in_y
58+
59+
def run():
60+
61+
pygame.mixer.pre_init(44100, 16, 2, 1024*4)
62+
pygame.init()
63+
screen = pygame.display.set_mode(SCREEN_SIZE, 0)
64+
65+
default_font = pygame.font.get_default_font()
66+
font = pygame.font.SysFont("default_font", 50, False)
67+
68+
# Create our buttons
69+
x = 100
70+
y = 240
71+
button_width = 150
72+
73+
# Store the buttons in a dictionary, so we can assign them names
74+
buttons = {}
75+
buttons["prev"] = Button("prev.png", (x, y))
76+
buttons["pause"] = Button("pause.png", (x+button_width*1, y))
77+
buttons["stop"] = Button("stop.png", (x+button_width*2, y))
78+
buttons["play"] = Button("play.png", (x+button_width*3, y))
79+
buttons["next"] = Button("next.png", (x+button_width*4, y))
80+
81+
music_filenames = get_music(MUSIC_PATH)
82+
83+
if len(music_filenames) == 0:
84+
print("No OGG files found in ", MUSIC_PATH)
85+
return
86+
87+
white = (255, 255, 255)
88+
label_surfaces = []
89+
90+
# Render the track names
91+
for filename in music_filenames:
92+
93+
txt = os.path.split(filename)[-1]
94+
95+
print("Track:", txt)
96+
97+
txt = txt.split('.')[0]
98+
surface = font.render(txt, True, (100, 0, 100))
99+
label_surfaces.append(surface)
100+
101+
current_track = 0
102+
max_tracks = len(music_filenames)
103+
104+
pygame.mixer.music.load( music_filenames[current_track] )
105+
106+
clock = pygame.time.Clock()
107+
108+
playing = False
109+
paused = False
110+
111+
# This event is sent when a music track ends
112+
TRACK_END = USEREVENT + 1
113+
pygame.mixer.music.set_endevent(TRACK_END)
114+
115+
while True:
116+
117+
button_pressed = None
118+
119+
for event in pygame.event.get():
120+
121+
if event.type == QUIT:
122+
pygame.quit()
123+
quit()
124+
125+
if event.type == MOUSEBUTTONDOWN:
126+
127+
# Find the pressed button
128+
for button_name, button in buttons.items():
129+
if button.is_over(event.pos):
130+
print(button_name, "pressed")
131+
button_pressed = button_name
132+
break
133+
134+
if event.type == TRACK_END:
135+
# If the track has ended, simulate pressing the next button
136+
button_pressed = "next"
137+
138+
if button_pressed is not None:
139+
140+
if button_pressed == "next":
141+
current_track = (current_track + 1) % max_tracks
142+
pygame.mixer.music.load( music_filenames[current_track] )
143+
if playing:
144+
pygame.mixer.music.play()
145+
146+
elif button_pressed == "prev":
147+
148+
# If the track has been playing for more that 3 seconds,
149+
# rewind i, otherwise select the previous track
150+
if pygame.mixer.music.get_pos() > 3000:
151+
pygame.mixer.music.stop()
152+
pygame.mixer.music.play()
153+
else:
154+
current_track = (current_track - 1) % max_tracks
155+
pygame.mixer.music.load( music_filenames[current_track] )
156+
if playing:
157+
pygame.mixer.music.play()
158+
159+
elif button_pressed == "pause":
160+
if paused:
161+
pygame.mixer.music.unpause()
162+
paused = False
163+
else:
164+
pygame.mixer.music.pause()
165+
paused = True
166+
167+
elif button_pressed == "stop":
168+
pygame.mixer.music.stop()
169+
playing = False
170+
171+
elif button_pressed == "play":
172+
if paused:
173+
pygame.mixer.music.unpause()
174+
paused = False
175+
else:
176+
if not playing:
177+
pygame.mixer.music.play()
178+
playing = True
179+
180+
181+
screen.fill(white)
182+
183+
184+
# Render the name of the currently track
185+
label = label_surfaces[current_track]
186+
w, h = label.get_size()
187+
screen_w = SCREEN_SIZE[0]
188+
screen.blit(label, ((screen_w - w)/2, 450))
189+
190+
# Render all the buttons
191+
for button in list(buttons.values()):
192+
button.render(screen)
193+
194+
# No animation, 5 frames per second is fine!
195+
clock.tick(5)
196+
pygame.display.update()
197+
198+
199+
if __name__ == "__main__":
200+
201+
run()

Chapter 10/ball.png

4.42 KB
Loading

Chapter 10/bar.png

1.18 KB
Loading

Chapter 10/bounce.wav

432 KB
Binary file not shown.

0 commit comments

Comments
 (0)