我写了个pygame程序
```python
import pygame
import os
pygame.font.init()
pygame.mixer.init()
WIDTH,HEIGHT=900,500 # 窗口大小
BULLET_VEL = 7#子弹速度
WIN = pygame.display.set_mode((WIDTH,HEIGHT)) #创建窗口屏幕对象,可以绘制东西
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
FPS=100 # 帧数
VEL=5 # 飞船速度
MAX_BULLETS=3 #子弹
SPACESHIP_WIDTH=55 # 飞船宽
SPACESHIP_HEIGHT=40 # 飞船高
BORDER=pygame.Rect(WIDTH//2,0,10,HEIGHT) # 中间线大小
BLAKE=(0,0,0) # 颜色
RED=(255,0,0)
YELLOW=(255,100,0) # 使黄色更显眼
BULLET_HIT_SOUND = pygame.mixer.Sound("Assets/Grenade+1.mp3")
BULLET_FIRE_SOUND = pygame.mixer.Sound("Assets/Gun+Silencer.mp3")
YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join("Assets","spaceship_yellow.png")) # 图片加载1
YELLOW_SPACESHIP=pygame.transform.rotate(pygame.transform.scale(YELLOW_SPACESHIP_IMAGE,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)),90) # 图片设置1
RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join("Assets","spaceship_red.png")) # 图片加载2
RED_SPACESHIP=pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)),270) # 图片设置2
SPACE=pygame.transform.scale(pygame.image.load(os.path.join("Assets", "space.png")), (WIDTH, HEIGHT))
MUSIC=pygame.mixer.music.load("Assets/BackGround.mp3") # 这个就是背景音乐
pygame.display.set_caption("first Game!") #标题
def handle_bullets(yellow_bullets,red_bullets,yellow,red):
for bullet in yellow_bullets:
bullet.x+=BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)
for bullet in red_bullets:
bullet.x-=BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
def yello_handle_movement(keys_pressed,yellow): # 1飞船按键运动,遇到边缘就停下
if keys_pressed[pygame.K_a] and yellow.x-VEL>0:
yellow.x-=VEL
if keys_pressed[pygame.K_d] and yellow.x+VEL+yellow.width<BORDER.x:
yellow.x+=VEL
if keys_pressed[pygame.K_w] and yellow.y-VEL>0:
yellow.y-=VEL
if keys_pressed[pygame.K_s] and yellow.y+VEL+yellow.height<HEIGHT-15:
yellow.y+=VEL
def red_handle_movement(keys_pressed,red): # 2飞船按键运动,遇到边缘就停下
if keys_pressed[pygame.K_LEFT] and red.x-VEL>BORDER.x + BORDER.width:
red.x-=VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH:
red.x+=VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0:
red.y-=VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT:
red.y+=VEL
def draw_window(red,yellow,red_bullets,yellow_bullets): # 窗口渲染
WIN.blit(SPACE,(0,0))
pygame.draw.rect(WIN,BLAKE,BORDER)
WIN.blit(YELLOW_SPACESHIP,(yellow.x,yellow.y))
WIN.blit(RED_SPACESHIP,(red.x,red.y))
for bullet in red_bullets:
pygame.draw.rect(WIN,RED,bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN,YELLOW,bullet)
pygame.display.update()
if __name__=="__main__": # 主函数
red=pygame.Rect(700,300,SPACESHIP_WIDTH,SPACESHIP_HEIGHT) # 1飞机
yellow=pygame.Rect(100,300,SPACESHIP_WIDTH,SPACESHIP_HEIGHT) # 2飞机
clock=pygame.time.Clock() # Clock(帧数)对象
red_bullets=[]
yellow_bullets=[]
run=True # 游戏是否运行
while run:
clock.tick(FPS) # 设置帧数
for event in pygame.event.get():
pygame.mixer.music.play()
if event.type == pygame.QUIT: # 游戏窗口关闭
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
if event.key == pygame.K_RETURN and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
keys_pressed = pygame.key.get_pressed() # 按键侦测
yello_handle_movement(keys_pressed,yellow)
red_handle_movement(keys_pressed,red)
handle_bullets(yellow_bullets,red_bullets,yellow,red) # 子弹侦测
draw_window(red,yellow,red_bullets,yellow_bullets)
pygame.quit() # 退出游戏
但音效和背景音乐总是有矛盾
移动飞船,背景音乐就会重新播放
发射子弹,背景音乐也会重新播放
求帮助