学了一天pygame,用python和pygame写一个简单的挡板弹球游戏
GitHub:
EasyBaffleBallGame
# -*- coding:utf-8 -*-
from sys import exit
import pygame
from pygame.locals import *
pygame.init()
# 创建窗口
ScreenWidth = 500
ScreenHright = 720
ScreenSize = (ScreenWidth, ScreenHright)
Screen = pygame.display.set_mode(ScreenSize, 0, 32)
pygame.display.set_caption("Ly's Easy Ball Game")
# 背景音乐
pygame.mixer.music.load('Sugar.mp3')
pygame.mixer.music.play(-1, 0.0)
# 碰撞音效
CollisionMusic = pygame.mixer.Sound('collision.wav')
# 重新开始按钮音效
ButtonMusic = pygame.mixer.Sound('button.wav')
# 游戏结束音效
GameOverMusic = pygame.mixer.Sound('over.wav')
def GameStart():
# 游戏背景Surface对象
Background = pygame.image.load('GameBackground.jpg').convert()
# 挡板Surface对象
Baffle = pygame.image.load('Baffle.png').convert_alpha()
# 球Surface对象
Ball = pygame.image.load('Ball.png').convert_alpha()
# 挡板位置信息
BaffleX = 140
BaffleY = 600
BaffleSpeed = 1000
BaffleXSpeed = BaffleSpeed
BaffleYSpeed = BaffleSpeed
BaffleMove = {K_LEFT: 0, K_RIGHT: 0, K_UP: 0, K_DOWN: 0}
# 球位置信息
BallX = 235
BallY = 0
BallSpeed = 1000.
BallXSpeed = BallSpeed
BallYSpeed = BallSpeed