def withspeech(): playerX = 370 playerY = 480 playerX_change = 0 playerY_change = 0 bulletX = 0 bulletY = 480 bulletX_change = 0 bulletY_change = 10 bullet_state = "ready" textX = 10 testY = 10 global history history = [] global ttl_history ttl_history = " " global score_value ryClock = pgTime.Clock() ryLoopRate = 30 # loop/sec recProbToConfirm = 0.2 running = True while running: # 掌握一下這個 while 迴圈的 循環速度 fps= frames (loops) per sec dt = ryClock.tick(ryLoopRate) fps = ryClock.get_fps() #print(f'dt= {dt:.2f}, fps= {fps:.0f}') #< 語音辨識在此執行 ...., # #y, prob= ryGet1secSpeechAndRecogItWithProb() # #'ryQ', 'ryRecogQGet' yL = ryRecogQGet(ryQ) #print(yL) recResult = y = '' prob = 0.0 for y, prob in yL: recResult = y if prob > recProbToConfirm: if y == 'left': playerX_change = -1 ck_history(y) elif y == 'right': playerX_change = +1 ck_history(y) elif y == 'up': playerY_change = -1 ck_history(y) elif y == 'down': playerY_change = +1 ck_history(y) elif y in ['yes', 'on', 'go']: ck_history(y) if bullet_state == "ready": bulletSound = mixer.Sound(dataBasePath + "laser.wav") bulletSound.play() # Get the current x cordinate of the spaceship bulletX = playerX bulletY = playerY fire_bullet(bulletX, bulletY) elif y in ['no', 'off', 'stop']: ck_history(y) playerX_change = 0 playerY_change = 0 else: pass #> 語音辨識在此執行 .... # RGB = Red, Green, Blue win.fill((0, 0, 0)) # Background Image win.blit(background, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # if keystroke is pressed check whether its right or left if event.type == pygame.KEYDOWN: playerY_change = -5 if event.key == pygame.K_LEFT: playerX_change = -5 playerImg = pygame.image.load('player_l.png') if event.key == pygame.K_RIGHT: playerX_change = 5 playerImg = pygame.image.load('player_r.png') if event.key == pygame.K_SPACE: if bullet_state == "ready": bulletSound = mixer.Sound("laser.wav") bulletSound.play() # Get the current x cordinate of the spaceship bulletX = playerX fire_bullet(bulletX, bulletY) if event.type == pygame.KEYUP: playerY_change = 5 # 5 = 5 + -0.1 -> 5 = 5 - 0.1 # 5 = 5 + 0.1 playerX += playerX_change if playerX <= 0: playerX = 0 elif playerX >= 736: playerX = 736 playerY += playerY_change if playerY <= 0: playerY = 0 elif playerY >= 480: playerY = 480 # Apple Movement for i in range(num_of_apples): # Game Over if appleY[i] > 440: for j in range(num_of_apples): appleY[j] = 2000 game_over_text() break appleX[i] += appleX_change[i] if appleX[i] <= 0: appleX_change[i] = 4 appleY[i] += appleY_change[i] elif appleX[i] >= 736: appleX_change[i] = -4 appleY[i] += appleY_change[i] # Collision collision = isCollision(appleX[i], appleY[i], bulletX, bulletY) if collision: explosionSound = mixer.Sound("coin.wav") explosionSound.play() score_value += 1 appleX[i] = random.randint(0, 736) appleY[i] = random.randint(50, 150) apple(appleX[i], appleY[i], i) # Bullet Movement if bulletY <= 0: bulletY = 480 bullet_state = "ready" if bullet_state == "fire": fire_bullet(bulletX, bulletY) bulletY -= bulletY_change history_label = font.render(ttl_history, True, (30, 30, 255)) win.blit(history_label, (10, 550)) player(playerX, playerY) show_score(textX, testY) ryShow_recognition(playerX, playerY, recResult=recResult) pygame.display.update() pygame.quit() ryStream.stop() ryStream.close()
def play(self, num): return mixer.Sound(self.filename[num]).play()
def withkey(): global score_value # Player playerX = 370 playerY = 480 playerX_change = 0 playerY_change = 0 running = True while running: # RGB = Red, Green, Blue win.fill((0, 0, 0)) # Background Image win.blit(background, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # if keystroke is pressed check whether its right or left if event.type == pygame.KEYDOWN: playerY_change = -5 if event.key == pygame.K_LEFT: playerX_change = -5 playerImg = pygame.image.load('ele/player_l.png') if event.key == pygame.K_RIGHT: playerX_change = 5 playerImg = pygame.image.load('ele/player_r.png') if event.type == pygame.KEYUP: playerY_change = 5 # 5 = 5 + -0.1 -> 5 = 5 - 0.1 # 5 = 5 + 0.1 playerX += playerX_change playerY += playerY_change if playerX <= 0: playerX = 0 elif playerX >= X - 64: playerX = X - 64 if playerY <= 0: playerY = 0 elif playerY >= Y - 64: playerY = Y - 64 # Apple Movement for i in range(num_of_apples): # Game Over if appleY[i] > 440: for j in range(num_of_apples): appleY[j] = 2000 game_over_text() break appleX[i] += appleX_change[i] if appleX[i] <= 0: appleX_change[i] = 4 appleY[i] += appleY_change[i] elif appleX[i] >= 736: appleX_change[i] = -4 appleY[i] += appleY_change[i] # Collision collision = isCollision(appleX[i], appleY[i], playerX, playerY) if collision: explosionSound = mixer.Sound("ele/coin.wav") explosionSound.play() score_value += 1 appleX[i] = random.randint(0, 736) appleY[i] = random.randint(50, 150) apple(appleX[i], appleY[i], i) player(playerX, playerY) show_score(10, 10) pygame.display.update()