1-实例代码
import math,random
import pygame
from pygame import mixer
# 游戏初始化
pygame.init()
# 窗口设置
screen = pygame.display.set_mode((800, 600))
# 背景设置
background = pygame.image.load('background.png')
# 背景音乐,-1表示循环播放
mixer.music.load("background.wav")
mixer.music.play(-1)
# 标题与图标
pygame.display.set_caption("太空大战")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# 人物
playerImg = pygame.image.load('player.png')
playerX = 370
playerY = 480
playerX_change = 0
running = True
while running:
# 复制背景图片
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 绘制显示人物图片
screen.blit(playerImg,(playerX,playerY))
pygame.display.update()