用python实现打砖块小游戏

本文实例为大家分享了python实现打砖块小游戏的具体代码,供大家参考,具体内容如下

开发益智的打砖块小游戏,你可以试一下能打几块

import pygame,sys,time,random
from pygame.locals import *        #
from static_params import *   #引入所有静态参数
from GameClass import *

pygame.init()   #初始化游戏
mainclock = pygame.time.Clock() #时钟设置
Exit =0
global Surface 
Surface = pygame.display.set_mode([WindowWidth,WindowHeight],0,32) #窗口设置
pygame.display.set_caption('打砖块游戏')    #设置窗口标题
def BeforeGame():
    StartImage = pygame.image.load('intro_Ball.png').convert_alpha() #开始图像的界面
    button = Button(Surface,FontColor,TextLocation,'StartGame')
    flag = True
    while flag:
        for event in pygame.event.get():
            if event.type ==QUIT:
                Exit = 1
                pygame.quit()
                exit()
            if event.type == MOUSEBUTTONUP:
                if button.is_overed():
                    flag = False
        Surface.blit(StartImage,ImageLocation)
        button.ButtonBlit()
        pygame.display.update()
        mainclock.tick(100)

def Gaming():
    #设置一个暂停函数
    def pause():
        button = Button(Surface,FontColor,TextLocation,'Continue')
        Surface.fill((0,0,0))
        flag = True
        while flag:
            for event in pygame.event.get():
                if event.type ==QUIT:
                    Exit = 1
                    pygame.quit()
                    exit()
                if event.type == MOUSEBUTTONUP:
                    if button.is_overed():
                        flag = False
            pygame.mouse.set_visible(True)
            button.ButtonBlit()
            pygame.display.update()
            mainclock.tick(100)

    Ball = ball(BallCenter,BallRadius,BallColor,BallSpeed,MoveAngle,Surface)
    paddle = Paddle(0,WindowHeight-PaddleHeight,PaddleWidth,PaddleHeight,PaddleColor,Surface)
    # 设置一个砖块类的矩阵
    BrickMatrix = [[Brick(i,j,BrickWidth,BrickHeight,BrickHitNumber,BrickColor,Surface) for i in range(0,100,BrickWidth+3) if i+BrickWidth<640]\
    for j in range(0,50,BrickHeight+2)]
    mouse_x,mouse_y = pygame.mouse.get_pos()
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                exit()
            if event.type == MOUSEMOTION:
                mouse_x, mouse_y = event.pos  #判断鼠标的位置
            if event.type == KEYDOWN:   #按下空格键暂停
                if event.key == K_SPACE:
                    pause()
        Surface.fill((0,0,0))
        #设置鼠标为不可见状态
        pygame.mouse.set_visible(False)
        #判断球的运动
        #判断是否撞上了边界或者挡板
        if Ball.center[1]+Ball.radius+paddle.height > WindowHeight:
            if Ball.center[0]>paddle.left and Ball.center[0]<paddle.left+paddle.width:
                Ball.rebound4()
        #判断是否装上了左边界
        elif Ball.center[0]-Ball.radius<interval:
            Ball.rebound1()
        elif Ball.center[0]+Ball.radius>WindowWidth-interval:
            Ball.rebound2()
        #判断是否撞上了上边界
        elif Ball.center[1]-Ball.radius<interval:
            Ball.rebound3()
        for brickline in BrickMatrix:
            for brick in brickline:    
                if brick.exist == 1:    
                    if brick.top >Ball.center[1] and brick.top-Ball.center[1]-Ball.radius<interval and Ball.speedy>0 and Ball.center[0]>brick.left and Ball.center[0]<brick.right:
                        print(1,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
                        Ball.rebound4()
                        brick.hitnumber =brick.hitnumber-1
                    if Ball.center[1]>brick.bottom and Ball.center[1]-Ball.radius-brick.bottom<interval and Ball.speedy<0 and Ball.center[0]>brick.left and Ball.center[0]<brick.right:
                        print(2,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
                        Ball.rebound3()
                        brick.hitnumber =brick.hitnumber-1
                    if Ball.center[0]< brick.left and brick.left-Ball.center[0]-Ball.radius<interval and Ball.speedx>0 and Ball.center[1]>brick.top and Ball.center[1]<brick.bottom:
                        print(3,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
                        Ball.rebound2()
                        brick.hitnumber =brick.hitnumber-1
                    if Ball.center[0]>brick.right and Ball.center[0]-Ball.radius-brick.right<interval and Ball.speedx<0 and Ball.center[1]>brick.top and Ball.center[1]<brick.bottom:
                        print(4,Ball.center,brick.left,brick.right,brick.top,brick.bottom,Ball.radius)
                        Ball.rebound1()
                        brick.hitnumber =brick.hitnumber-1
                    if brick.hitnumber <= 0:
                        brick.exist = 0
        #所有的砖块都不存在了,则游戏胜利
        if all([not any([brick.exist for brick in line]) for line in BrickMatrix] ):
            return 'Win'
        # print(brick.hitnumber,brick.exist)
        Ball.move()
        paddle.get_pos(mouse_x)
        if Ball.fall():
            return 'Fail'
        #画出图形
        for brickline in BrickMatrix:
            for brick in brickline:
                brick.draw()
        Ball.draw()
        paddle.draw()
        pygame.display.update()
        #每秒钟执行100次该代码,用来控制游戏循环频率
        mainclock.tick(100)
    

def AfterGame(text):
    result = pygame.font.SysFont('comicsansms',100).render(text,1,(0,255,0))
    Surface.blit(result,ImageLocation)
    button1 = Button(Surface,FontColor,TextLocation,'PLAY IT AGAIN')
    button2 = Button(Surface,FontColor,TextLocation2,'QUIT')
    flag = True
    while flag:
        pygame.mouse.set_visible(True)
        for event in pygame.event.get():
            if event.type == QUIT:
                Exit = 1
                pygame.quit()
                exit()
            if event.type == MOUSEBUTTONUP:
                if button1.is_overed():
                    flag = False
                if button2.is_overed():
                    Exit = 1
                    pygame.quit()
                    exit()
        button1.ButtonBlit()
        button2.ButtonBlit()
        pygame.display.update()
        mainclock.tick(100)

def main():
    #展示游戏开始前的信息
    BeforeGame()
    print(Exit)
    #开始游戏循环
    while not Exit:
        com=Gaming()
        AfterGame(com)

if __name__ =='__main__':
    main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Python Pygame实战在打砖块游戏的实现

    目录 导语 开发工具 环境搭建 效果展示 原理简介 导语 想起来好久没更这个系列的文章了,周末过来补一波好了.本期我们将利用python制作一个打砖块小游戏,废话不多说,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块. 环境搭建 安装Python并添加到环境变量,pip安装需要的相关模块即可. 效果展示 在cmd窗口运行Game18.py文件即可. 效果如下: 视频链接 原理简介 游戏规则(摘自维基百科): 打砖块是一

  • Python Pygame实战之打砖块小游戏

    目录 导语 一.准备中 1)游戏规则: 2)环境安装 二.开始敲代码 1)配置文件 2)定义一些类 3)定义开始.结束界面 4)定义游戏 5)主函数与运行界面 三.效果展示 导语 嘿!前不久刚刚给大家过一款反弹球的小游戏嘛! 不知道大家还记得不?不记得可以看下往期的内容呢,在上一期的基础上升级了这款打砖块的小游戏,界面的话也挺简单的,经典配色原汁原味哈哈哈. 大家好,我是木木子,一个上的编程下的厅堂的女码农!今天带大家编写一款经典的打砖块儿小游戏! 小科普: 打砖块最早是由雅达利公司开发的一款独

  • 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 #绘制游戏窗口,设置窗口尺

  • Python实现打砖块小游戏代码实例

    这次用Python实现的是一个接球打砖块的小游戏,需要导入pygame模块,有以下两条经验总结: 1.多父类的继承2.碰撞检测的数学模型 知识点稍后再说,我们先看看游戏的效果和实现: 一.游戏效果 二.游戏代码 #导入模块 import pygame from pygame.locals import * import sys,random,time,math class GameWindow(object): '''创建游戏窗口类''' def __init__(self,*args,**kw

  • python pygame实现打砖块游戏

    本文实例为大家分享了python pygame实现打砖块游戏的具体代码,供大家参考,具体内容如下 最近在尝试着写一个用强化学习的方法玩打砖块的游戏,首先将游戏环境做些改动,以便产生需要的数据 游戏环境的界面以及代码如下 import sys sys.path.append(r'E:\anaconda\Lib\site-packages') import pygame import sys import random import time import math from tkinter imp

  • 用python实现打砖块小游戏

    本文实例为大家分享了python实现打砖块小游戏的具体代码,供大家参考,具体内容如下 开发益智的打砖块小游戏,你可以试一下能打几块 import pygame,sys,time,random from pygame.locals import *        # from static_params import *   #引入所有静态参数 from GameClass import * pygame.init()   #初始化游戏 mainclock = pygame.time.Clock(

  • Java编程经典小游戏设计-打砖块小游戏源码

    [程序中使用的数据结构和符号说明] HitBrick类 GreenBallThread控制小球路线 xUp,yUp,bouncing定义变量存储16位数值形式 x,y小球坐标 xDx,yDy坐标增量 MAX_X,MAX_Y坐标最大值 renew初始化 label标签 Rx,Ry横板坐标 Brick[]砖块 ball小球 HitBrick()定义小球横板及砖块位置坐标 keyPressd(keyEent)定义小球启动键(按空格键启动) keyReleased(keyEvent)接收键盘事件侦听器接

  • java实现打砖块小游戏

    本文实例为大家分享了java实现打砖块小游戏的具体代码,供大家参考,具体内容如下 源码共包含两个文件 文件1:play_zhuankuai.java import java.awt.*; import javax.swing.JPanel; @SuppressWarnings("serial") public class play_zhuankuai extends JPanel implements Runnable{ boolean exit=false; boolean end=

  • 用Python设计一个经典小游戏

    本文主要介绍如何用Python设计一个经典小游戏:猜大小. 在这个游戏中,将用到前面我介绍过的所有内容:变量的使用.参数传递.函数设计.条件控制和循环等,做个整体的总结和复习. 游戏规则: 初始本金是1000元,默认赔率是1倍,赢了,获得一倍金额,输了,扣除1倍金额. 玩家选择下注,押大或押小: 输入下注金额: 摇3个骰子,11≤骰子总数≤18为大,3≤骰子总数≤10为小: 如果赢了,获得1倍金额,输了,扣除1倍金额,本金为0时,游戏结束. 程序运行结果是这样的: 现在,我们来梳理下思路. 我们

  • Python实现的弹球小游戏示例

    本文实例讲述了Python实现的弹球小游戏.分享给大家供大家参考,具体如下: 弹球 1. Ball 类 draw负责移动Ball 碰撞检测,反弹,Ball检测Paddle 2.Paddle类 draw负责移动Paddle 碰撞检测,确定能不能继续 监听键盘事件 3.主循环 绘制Ball和Paddle update sleep 代码 from Tkinter import * import random import time class Ball: def __init__(self, canv

  • jQuery网页版打砖块小游戏源码分享

    这是一款基于jQuery实现网页版打砖块小游戏源码,满满的童年回忆. 为大家分享的jQuery实现网页版打砖块小游戏源码如下 效果演示 源码下载 <!DOCTYPE html> <html lang="en" > <head> <meta charset="gb2312" /> <title>jQuery网页版打砖块小游戏源码</title> <link href="css/ma

  • Python实现Pig Latin小游戏实例代码

    前言: 本文研究的主要是Python实现pig Latin小游戏的简单代码,具体介绍如下. Pig Latin是一个语言游戏. 步骤: 1.让用户输入一个英文单词 2.确保用户输入一个有效单词 3.将单词转换成Pig Latin 4.显示转换结果 一.Input 函数:raw_input()用于输出一个字符串并等待键盘输入某字符串,最后以Enter(或Return)结束输入 original = raw_input("Enter a word:") print original 上述中

  • python编写猜数字小游戏

    本文实例为大家分享了python编写猜数字小游戏的具体代码,供大家参考,具体内容如下 import random secret = random.randint(1, 30) guess = 0 tries = 0 print("我叫丁丁,我有一个秘密数字!") print("数字从1到30,你只有6次机会!") while int(guess) != secret and tries < 6: print("你猜的数字是?") guess

随机推荐