Python打砖块代码

作者:刘旭濠

查找资料的过程中在网上发现了这个打砖块小游戏的代码,接着就试着玩玩还有点意思,然后自己也对着弄了一个更有意思,疯狂版的打砖块游戏,速度越来越快,

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 - self.rect_length // 2:
  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值