作者:刘旭濠
查找资料的过程中在网上发现了这个打砖块小游戏的代码,接着就试着玩玩还有点意思,然后自己也对着弄了一个更有意思,疯狂版的打砖块游戏,速度越来越快,
import pygame
from pygame.locals import *
import sys, random, time, math
class GameWindow(object):
‘’‘创建游戏窗口类’’’
def __init__(self, *args, **kw):
self.window_length = 600
self.window_wide = 500
# 绘制游戏窗口,设置窗口尺寸
self.game_window = pygame.display.set_mode((self.window_length, self.window_wide))
# 设置游戏窗口标题
pygame.display.set_caption("CatchBallGame")
# 定义游戏窗口背景颜色参数
self.window_color = (88, 207, 250)
def backgroud(self):
# 绘制游戏窗口背景颜色
self.game_window.fill(self.window_color)
class Ball(object):
‘’‘创建球类’’’
def __init__(self, *args, **kw):
# 设置球的半径、颜色、移动速度参数
self.ball_color = (66, 77, 20)
self.move_x = 1
self.move_y = 1
self.radius = 50
def ballready(self):
# 设置球的初始位置、
self.ball_x = self.mouse_x
self.ball_y = self.window_wide - self.rect_wide - self.radius
# 绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)
def ballmove(self):
# 绘制球,设置反弹触发条件
pygame.draw.circle(self.game_window, self.ball_color, (self.ball_x, self.ball_y), self.radius)
self.ball_x += self.move_x
self.ball_y -= self.move_y
# 调用碰撞检测函数
self.ball_window()
self.ball_rect()
# 每接5次球球速增加一倍
if self.distance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
# 设置游戏失败条件
if self.ball_y > 520:
self.gameover = self.over_font.render("Game Over", False, (0, 0, 0))#只能输入英文
self.game_window.blit(self.gameover, (100, 130))
self.over_sign = 1
class Rect(object):
‘’‘创建球拍类’’’
def __init__(self, *args, **kw):
# 设置球拍颜色参数
self.rect_color = (28, 26, 10)
self.rect_length = 100
self.rect_wide = 10
def rectmove(self):
# 获取鼠标位置参数
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
# 绘制球拍,限定横向边界
if self.mouse_x >= self.window_length -