Friday 15 April 2011

python - How to create sound of ball bouncing off of bar -



python - How to create sound of ball bouncing off of bar -

i working on little pong game, wondering on how add together sound game when ball bounces off of 1 of paddles, create sound want.

this code, , sound file have called "blop.wav".

import pygame pygame.locals import * sys import exit import random pygame.init() screen=pygame.display.set_mode((640,480),0,32) pygame.display.set_caption("pong pong!") #creating 2 bars, ball , background. = pygame.surface((640,480)) background = back.convert() background.fill((0,0,0)) bar = pygame.surface((10,50)) bar1 = bar.convert() bar1.fill((0,0,255)) bar2 = bar.convert() bar2.fill((255,0,0)) circ_sur = pygame.surface((15,15)) circ = pygame.draw.circle(circ_sur,(0,255,0),(15/2,15/2),15/2) circle = circ_sur.convert() circle.set_colorkey((0,0,0)) # definitions bar1_x, bar2_x = 10. , 620. bar1_y, bar2_y = 215. , 215. circle_x, circle_y = 307.5, 232.5 bar1_move, bar2_move = 0. , 0. speed_x, speed_y, speed_circ = 250., 250., 250. bar1_score, bar2_score = 0,0 #clock , font objects clock = pygame.time.clock() font = pygame.font.sysfont("calibri",40) while true: event in pygame.event.get(): if event.type == quit: exit() if event.type == keydown: if event.key == k_up: bar1_move = -ai_speed elif event.key == k_down: bar1_move = ai_speed elif event.type == keyup: if event.key == k_up: bar1_move = 0. elif event.key == k_down: bar1_move = 0. score1 = font.render(str(bar1_score), true,(255,255,255)) score2 = font.render(str(bar2_score), true,(255,255,255)) screen.blit(background,(0,0)) frame = pygame.draw.rect(screen,(255,255,255),rect((5,5),(630,470)),2) middle_line = pygame.draw.aaline(screen,(255,255,255),(330,5),(330,475)) screen.blit(bar1,(bar1_x,bar1_y)) screen.blit(bar2,(bar2_x,bar2_y)) screen.blit(circle,(circle_x,circle_y)) screen.blit(score1,(250.,210.)) screen.blit(score2,(380.,210.)) bar1_y += bar1_move # motion of circle time_passed = clock.tick(30) time_sec = time_passed / 1000.0 circle_x += speed_x * time_sec circle_y += speed_y * time_sec ai_speed = speed_circ * time_sec #ai of computer. if circle_x >= 305.: if not bar2_y == circle_y + 7.5: if bar2_y < circle_y + 7.5: bar2_y += ai_speed if bar2_y > circle_y - 42.5: bar2_y -= ai_speed else: bar2_y == circle_y + 7.5 if bar1_y >= 420.: bar1_y = 420. elif bar1_y <= 10. : bar1_y = 10. if bar2_y >= 420.: bar2_y = 420. elif bar2_y <= 10.: bar2_y = 10. #since don't know collision, ball hitting bars goes this. if circle_x <= bar1_x + 10.: if circle_y >= bar1_y - 7.5 , circle_y <= bar1_y + 42.5: circle_x = 20. speed_x = -speed_x if circle_x >= bar2_x - 15.: if circle_y >= bar2_y - 7.5 , circle_y <= bar2_y + 42.5: circle_x = 605. speed_x = -speed_x if circle_x < 5.: bar2_score += 1 circle_x, circle_y = 320., 232.5 bar1_y,bar_2_y = 215., 215. elif circle_x > 620.: bar1_score += 1 circle_x, circle_y = 307.5, 232.5 bar1_y, bar2_y = 215., 215. if circle_y <= 10.: speed_y = -speed_y circle_y = 10. elif circle_y >= 457.5: speed_y = -speed_y circle_y = 457.5 pygame.display.update()

since you're doing in pygame, want utilize pygame.mixer.

see docs, , linked examples, total details, basically, key points are:

call pygame.mixer.init() construct pygame.mixer.sound(file=path_to_wavfile) each wavfile. every time want play sound, phone call thesound.play().

by default, 8 channels; if seek play 9 simultaneous sounds, 1 of them fail play. if want things like, say, stop background loop temporarily free channel sound effect, can handle manually, or can reserve of channels different purposes, etc. usually, basics enough.

which formats pygame.mixer.sound can handle depend on libraries compiled pygame/sdl build. .wav format should there, if don't want rely on that, can utilize stdlib's wave module read file, sound(buffer=…) constructor.

python python-2.7 audio pygame pong

No comments:

Post a Comment