python实现打砖块游戏
作者:_Getwebshell_ 发布时间:2023-11-09 21:27:15
标签:python,打砖块
本文实例为大家分享了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 = (135,206,250)
def backgroud(self):
#绘制游戏窗口背景颜色
self.game_window.fill(self.window_color)
class Ball(object):
'''创建球类'''
def __init__(self,*args,**kw):
#设置球的半径、颜色、移动速度参数
self.ball_color = (255,215,0)
self.move_x = 1
self.move_y = 1
self.radius = 10
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 = (255,0,0)
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:
self.mouse_x = self.window_length-self.rect_length//2
if self.mouse_x <= self.rect_length//2:
self.mouse_x = self.rect_length//2
pygame.draw.rect(self.game_window,self.rect_color,((self.mouse_x-self.rect_length//2),(self.window_wide-self.rect_wide),self.rect_length,self.rect_wide))
class Brick(object):
def __init__(self,*args,**kw):
#设置砖块颜色参数
self.brick_color = (139,126,102)
self.brick_list = [[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]]
self.brick_length = 80
self.brick_wide = 20
def brickarrange(self):
for i in range(5):
for j in range(6):
self.brick_x = j*(self.brick_length+24)
self.brick_y = i*(self.brick_wide+20)+40
if self.brick_list[i][j] == 1:
#绘制砖块
pygame.draw.rect(self.game_window,self.brick_color,(self.brick_x,self.brick_y,self.brick_length,self.brick_wide))
#调用碰撞检测函数
self.ball_brick()
if self.distanceb < self.radius:
self.brick_list[i][j] = 0
self.score += self.point
#设置游戏胜利条件
if self.brick_list == [[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0],[0,0,0,0,0,0]]:
self.win = self.win_font.render("You Win",False,(0,0,0))
self.game_window.blit(self.win,(100,130))
self.win_sign = 1
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()
来源:https://blog.csdn.net/qq_43776408/article/details/100149017
0
投稿
猜你喜欢
- 其实这里的静态页面并不是真正意义上的静态,但可以达到了静态页面的解析效率,还未经项目测试,拿来分享。代码如下:<% Cons
- 无意中看到以前在电脑上保存的一个html页面,关于div水平垂直居中的问题。如何实现div水平垂直居中呢?1.已知宽高度水平垂直居中posi
- 每个程序员都绝对必须知道的关于字符集和Unicode的那点儿事(别找借口!)Unicode与字符集你曾经是否觉得HTML中的"Co
- setTimeoutsetTimeout 语法例子用 setTimeout 来执行 function不断重复执行的 setTimeout设定
- 啥都不说了,直接奉献原代码 代码如下:'==========注意==================================
- 今天看YUI的视频教程,YUI的工程师介绍的一款在线的图片压缩工具,也许你用过,也许没有,不过我这里强烈推荐大家用一下,我用smush.it
- 本文实例为大家分享了python实现录音功能的具体代码,供大家参考,具体内容如下# -*- coding: utf-8 -*-import
- 写个小教程,以期能让大家对jQuery有所了解,甚至喜欢上它(请不要怪我)。不废话,先说明我们的目的。我们知道,当代浏览器(modern b
- 1,SELECT 语句 在SQL的世界里,最最基础的操作就是SELECT 语句了。在数据库工具下直接采用SQL
- 在asp里通过以下两个函数实现javascript里的escape函数和unescape函数加密功能。在ajax post或get时内容存在
- 调用数据库存储过程见下:<%Set Dataconn = Server.CreateObject(&qu
- 先举个例子,以前负责教育培训类网站的时候,曾经接到过这样一个项目,需求方希望做一个充满趣味性的新手入门频道,页面要炫,最好是flash,用户
- CSS代码更清晰高效的一些经验是许多网页制作者与开发者都关心的问题。但是大概没有什么魔法,可以保证一下就把你的样式表缩小到百分之多少,合理的
- 缘起最近复习设计模式拜读谭勇德的<<设计模式就该这样学>>该书以java语言演绎了常见设计模式本系列笔记拟采用gol
- MS SQL Server 2000通过企业管理器设置数据库的定期自动备份计划。1、打开企业管理器,双击打开你的服务器2、然后点上面菜单中的
- 前端开发中两个很不错的小技巧, CSS三角形与圆角背景. 的确, 它们都可以通过图片来实现, 但, 抛开用代码实现可以减小图片加载量不说,
- 本文实例讲述了Linux下安装Memcached服务器和客户端与php使用。分享给大家供大家参考,具体如下:Memcached是高性能的分布
- Internet Explorer 9 Platform 1.9.7916.6000 Preview 4微软今天公布了IE9 Platfor
- 一、简介是一个 python 内置包,不需要额外安装即可使用urllib 是 Python 标准库中用于网络请求的库,内置四个模块,分别是u
- 保持良好的代码风格是每个Coder必学的课程,同样在HTML设计的时候也要特别注意代码的规范性,虽然说不规范的代码不会直接造成严重的后果,但