phanbu / gamboge

This is an adventure game developed by me (phanbu) and some high school students learning the Python programming language.
0 stars 0 forks source link

Music and sound effects #11

Open phanbu opened 6 years ago

CrossSpider3456 commented 6 years ago

i don't remember how to add music or sounds and also where should i look for sounds

phanbu commented 6 years ago

Start with the music that was in one of the earlier assignments. Remember the one where the comet kept orbiting around the star field?

You can find the code in there, too, in order to get an idea of what you need to do. Look in the the mixer package on pygame.org to see the documentation.

CrossSpider3456 commented 6 years ago

okay i cant find where that code is though

phanbu commented 6 years ago

Okay... Open a folder ("This PC") and browse to the c: drive. Then in the search box in the top right corner, enter "piano-music.wav" (no quotes) and hit enter. It might take a little while, but eventually you'll find the music file. Somewhere nearby that file, you should be able to find the python file that uses it. Mine is called fireball.py (yours might have a different name). Good luck.

phanbu commented 6 years ago

If you cannot find the python file, here is mine:

import pygame

class Fireball(pygame.sprite.Sprite):
    def __init__(self, screen):
        super().__init__()
        self.direction = 1
        self.speed = 10
        self.screen = screen
        self.sound = pygame.mixer.Sound("./resources/sounds/fireball.wav")
        self.fireball = pygame.image.load("./resources/pics/fireball.png").convert()
        self.image = self.fireball
        self.image.set_colorkey(self.image.get_at((0, 0)))
        self.rect = self.image.get_rect()
        self.rect.topright = (-200, 20)

    def update(self):
        super().update()
        if self.rect.left > self.screen.get_width() + 450:
            self.direction = -1
            self.speed = 5
            self.rect.left = self.screen.get_width()
            self.rect.top = self.screen.get_height() * 1/3
            self.image = pygame.transform.flip(self.fireball, True, False)
            self.image = pygame.transform.smoothscale(self.image, (102, 50))
            self.image.set_colorkey(self.image.get_at((0, 0)))
        elif self.rect.right < 0:
            self.direction = 1
            self.speed = 15
            self.rect.right = 0
            self.rect.centery = self.screen.get_height()/2
            self.image = self.fireball;
            self.sound.play()

        self.rect = self.rect.move(self.direction * self.speed, 0)

    def draw(self):
        self.screen.blit(self.image, self.rect)

def play():
    pygame.init()
    pygame.mixer.music.load("./resources/sounds/piano-music.wav")
    pygame.mixer.music.play(-1)
    screen = pygame.display.set_mode((1530, 300))
    space = pygame.image.load("./resources/pics/space.png").convert()
    space.set_colorkey(space.get_at((0,0)))
    fireball = Fireball(screen)
    finished = False
    clock = pygame.time.Clock()

    while not finished:
        for e in pygame.event.get():
            if e.type == pygame.QUIT \
            or (e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE):
                finished = True

        screen.fill(0)
        fireball.update()

        if fireball.direction > 0:
            screen.blit(space, (0,0))
            fireball.draw()
        else:
            fireball.draw()
            screen.blit(space, (0,0))

        pygame.display.flip()
        clock.tick(60)

if __name__ == '__main__':
    play()
CrossSpider3456 commented 6 years ago

okay got it thanks

CrossSpider3456 commented 6 years ago

okay im working on my homework again today but i dont know how to get a .wav to put into the program where do i get one?

phanbu commented 6 years ago

Simplest thing is to just use the piano-music.wav file from before, and then change it to something else later.

When you are ready to change it to something else, you should find something that fits into the Gamboge world. The main place that I go to get game resources is Open Game Art--I know that "art" is in the name, but it has music files, too. You can also go to Free Sound. You don't have to use a WAV file, the pygame mixer will play MP3 files, too.

CrossSpider3456 commented 6 years ago

okay got it all together ,but those links took me to web pages that weren't working. So I don't know whats up with that

phanbu commented 6 years ago

The links are working. Give them another try.

On the open Game Art site, under the browse menu, you can select music to limit your results.