在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → 打砖块

打砖块

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:0.01M
  • 下载次数:15
  • 浏览次数:107
  • 发布时间:2021-02-09
  • 实例类别:Python语言基础
  • 发 布 人:ahqo
  • 文件格式:.py
  • 所需积分:1
 相关标签:

实例介绍

【实例简介】

打砖块小游戏

【实例截图】

【核心代码】

#导入模块

import pygame
from pygame.locals import *
import sys,random,time,math
 
class Score(object):
    '''创建分数类'''
    def __init__(self,*args,**kw):      
        #设置初始分数
        self.score = 0
        #设置分数字体
        self.score_font = pygame.font.SysFont('arial',20)
        #设置初始加分点数
        self.point = 1
        #设置初始接球次数
        self.frequency = 0
 
    def countscore(self):
        #绘制玩家分数         
        my_score = self.score_font.render(str(self.score),False,(255,255,255))
        self.game_window.blit(my_score,(555,15))
 
class GameOver(object):
    '''创建游戏结束类'''
    def __init__(self,*args,**kw):
        #设置Game Over字体
        self.over_font = pygame.font.SysFont('arial',80)
        #定义GameOver标识
        self.over_sign = 0
 
class Win(object):
    '''创建游戏胜利类'''
    def __init__(self,*args,**kw):
        #设置You Win字体
        self.win_font = pygame.font.SysFont('arial',80)
        #定义Win标识
        self.win_sign = 0
 
class Collision(object):
    '''碰撞检测类'''
    #球与窗口边框的碰撞检测
    def ball_window(self):
        if self.ball_x <= self.radius or self.ball_x >= (self.window_length-self.radius):
            self.move_x = -self.move_x
        if self.ball_y <= self.radius:
            self.move_y = -self.move_y
 
    #球与球拍的碰撞检测
    def ball_rect(self):
        #定义碰撞标识
        self.collision_sign_x = 0
        self.collision_sign_y = 0
 
        if self.ball_x < (self.mouse_x-self.rect_length//2):
            self.closestpoint_x = self.mouse_x-self.rect_length//2
            self.collision_sign_x = 1
        elif self.ball_x > (self.mouse_x self.rect_length//2):
            self.closestpoint_x = self.mouse_x self.rect_length//2
            self.collision_sign_x = 2
        else:
            self.closestpoint_x = self.ball_x
            self.collision_sign_x = 3
 
        if self.ball_y < (self.window_wide-self.rect_wide):
            self.closestpoint_y = (self.window_wide-self.rect_wide)
            self.collision_sign_y = 1
        elif self.ball_y > self.window_wide:
            self.closestpoint_y = self.window_wide
            self.collision_sign_y = 2
        else:
            self.closestpoint_y = self.ball_y
            self.collision_sign_y = 3
        #定义球拍到圆心最近点与圆心的距离
        self.distance = math.sqrt(math.pow(self.closestpoint_x-self.ball_x,2) math.pow(self.closestpoint_y-self.ball_y,2))
        #球在球拍上左、上中、上右3种情况的碰撞检测
        if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2):
            if self.collision_sign_x == 1 and self.move_x > 0:
                self.move_x = - self.move_x
                self.move_y = - self.move_y
            if self.collision_sign_x == 1 and self.move_x < 0:
                self.move_y = - self.move_y
            if self.collision_sign_x == 2 and self.move_x < 0:
                self.move_x = - self.move_x
                self.move_y = - self.move_y
            if self.collision_sign_x == 2 and self.move_x > 0:
                self.move_y = - self.move_y
        if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
            self.move_y = - self.move_y
        #球在球拍左、右两侧中间的碰撞检测
        if self.distance < self.radius and self.collision_sign_y == 3:
            self.move_x = - self.move_x
 
    #球与砖块的碰撞检测
    def ball_brick(self):
        #定义碰撞标识
        self.collision_sign_bx = 0
        self.collision_sign_by = 0
 
        if self.ball_x < self.brick_x:
            self.closestpoint_bx = self.brick_x
            self.collision_sign_bx = 1
        elif self.ball_x > self.brick_x self.brick_length:
            self.closestpoint_bx = self.brick_x self.brick_length
            self.collision_sign_bx = 2
        else:
            self.closestpoint_bx = self.ball_x
            self.collision_sign_bx = 3
 
        if self.ball_y < self.brick_y:
            self.closestpoint_by = self.brick_y
            self.collision_sign_by = 1
        elif self.ball_y > self.brick_y self.brick_wide:
            self.closestpoint_by = self.brick_y self.brick_wide
            self.collision_sign_by = 2
        else:
            self.closestpoint_by = self.ball_y
            self.collision_sign_by = 3
        #定义砖块到圆心最近点与圆心的距离
        self.distanceb = math.sqrt(math.pow(self.closestpoint_bx-self.ball_x,2) math.pow(self.closestpoint_by-self.ball_y,2))
        #球在砖块上左、上中、上右3种情况的碰撞检测
        if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
            if self.collision_sign_bx == 1 and self.move_x > 0:
                self.move_x = - self.move_x
                self.move_y = - self.move_y
            if self.collision_sign_bx == 1 and self.move_x < 0:
                self.move_y = - self.move_y
            if self.collision_sign_bx == 2 and self.move_x < 0:
                self.move_x = - self.move_x
                self.move_y = - self.move_y
            if self.collision_sign_bx == 2 and self.move_x > 0:
                self.move_y = - self.move_y
        if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
            self.move_y = - self.move_y
        #球在砖块下左、下中、下右3种情况的碰撞检测
        if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
            if self.collision_sign_bx == 1 and self.move_x > 0:
                self.move_x = - self.move_x
                self.move_y = - self.move_y
            if self.collision_sign_bx == 1 and self.move_x < 0:
                self.move_y = - self.move_y
            if self.collision_sign_bx == 2 and self.move_x < 0:
                self.move_x = - self.move_x
                self.move_y = - self.move_y
            if self.collision_sign_bx == 2 and self.move_x > 0:
                self.move_y = - self.move_y
        if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
            self.move_y = - self.move_y
        #球在砖块左、右两侧中间的碰撞检测
        if self.distanceb < self.radius and self.collision_sign_by == 3:
            self.move_x = - self.move_x
 
class Main(GameWindow,Rect,Ball,Brick,Collision,Score,Win,GameOver):
    '''创建主程序类'''
    def __init__(self,*args,**kw):      
        super(Main,self).__init__(*args,**kw)
        super(GameWindow,self).__init__(*args,**kw)
        super(Rect,self).__init__(*args,**kw)
        super(Ball,self).__init__(*args,**kw)
        super(Brick,self).__init__(*args,**kw)
        super(Collision,self).__init__(*args,**kw)      
        super(Score,self).__init__(*args,**kw)
        super(Win,self).__init__(*args,**kw)
        #定义游戏开始标识
        start_sign = 0
 
        while True:         
            self.backgroud()
            self.rectmove()
            self.countscore()           
             
            if self.over_sign == 1 or self.win_sign == 1:
                break
            #获取游戏窗口状态
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if event.type == MOUSEBUTTONDOWN:
                    pressed_array = pygame.mouse.get_pressed()
                    if pressed_array[0]:
                        start_sign = 1
            if start_sign == 0:
                self.ballready()
            else:
                self.ballmove()
 
            self.brickarrange()
 
            #更新游戏窗口
            pygame.display.update()
            #控制游戏窗口刷新频率
            time.sleep(0.010)
 
if __name__ == '__main__':
    pygame.init()
    pygame.font.init()
    catchball = Main()

标签:

实例下载地址

打砖块

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警