''' 兩球會互相碰撞,碰撞期間發生聲音 by Ching-Shoei Chiang ''' import random, pygame, sys from pygame.locals import * pygame.init() FPS = 30 # frames per second setting fpsClock = pygame.time.Clock() # set up the window WINDOWWIDTH = 800 WINDOWHEIGHT = 800 screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) pygame.display.set_caption('object moving') WHITE = (255, 255, 255) WAVFILE = "badswap.wav" circle1Img = pygame.image.load('circle64.png') circle2Img = pygame.image.load('redcircle.jpg') circle1x = circle1y = 600 circle2x = circle2y = 200 dir1x = -1 dir1y = -1 dir2x = 1 dir2y = 1 step = 10 def overlap(a,b,c,d): return not (a+bWINDOWHEIGHT-64 or circle1y<0: dir1y = -1 * dir1y if circle1x>WINDOWWIDTH-64 or circle1x<0: dir1x = -1 * dir1x circle1x = circle1x + dir1x*step circle1y = circle1y + dir1y*step # Move the second circle if circle2y>WINDOWHEIGHT-64 or circle2y<0: dir2y = -1 * dir2y if circle2x>WINDOWWIDTH-64 or circle2x<0: dir2x = -1 * dir2x circle2x = circle2x + dir2x*step circle2y = circle2y + dir2y*step # Collision Detection collision = collision_detection(circle1x, circle1y, 64, 64, circle2x, circle2y, 64, 64) if collision: s = pygame.mixer.Sound(WAVFILE) ch = s.play() while ch.get_busy(): pygame.time.delay(100) screen.fill(WHITE) screen.blit(circle1Img, (circle1x, circle1y)) screen.blit(circle2Img, (circle2x, circle2y)) pygame.display.update() fpsClock.tick(FPS)