python游戏开发之pygame实现接球小游戏


Posted in Python onApril 22, 2022

一、介绍模块

最小开发框架:

python游戏开发Pygame框架

1、Pygame和sys模块

import pygame #制作游戏时要使用的模块
import sys #python的标准库,对内部各功能模块进行初始化创建,系统模块

2、random模块

需要在屏幕上随机生成小球

from random import randint

详情请看此文章:random模块和相关函数详解

二、相关功能

1、窗口尺寸改变

可以调节游戏屏幕的大小

# 改变窗口尺寸
        elif event.type == pygame.VIDEORESIZE:
            size = w,h = event.w,event.h
            screen = pygame.display.set_mode(size,pygame.RESIZABLE)

pygame.VIDEORESIZE 这是窗口大小改变事件,事件发生后,返回event.size元组,包含新窗口的宽度和高度。 .size[0] 高度,也可以用event.w表示 .size[1] 宽度,也可以用event.h表示 返回参数仅在事件发生时有用

2、键盘控制挡板

# 键盘控制挡板
        elif event.type == pygame.KEYDOWN: #键盘按下事件检测
            if event.key == pygame.K_LEFT:  # 判断挡板是否左移
                if board_rect.left > 0 and board_rect.left <= w - 186:
                    board_rect.left -= board_x
                elif board_rect.left <= 0:  # 判断挡板左边的坐标是否小于0
                    board_rect.left = 0
                    board_rect.top -= board_y
            elif event.key == pygame.K_RIGHT:  # 判断挡板是否右移
                if board_rect.right >= 186 and board_rect.right < w:
                    board_rect.right += board_x
                elif board_rect.right >= w:  # 判断挡板右边的坐标是否大于屏幕的宽度                        board_rect.right = w
                    board_rect.bottom += board_y

3、鼠标控制

#鼠标控制挡板
        elif event.type == pygame.MOUSEMOTION:
            # 鼠标左键按下并跟随鼠标移动
            if event.buttons[0] == 1:
                if event.pos[0] >= 0 and event.pos[0] < w - 186:#判断鼠标的位置
                    board_rect.left = event.pos[0] #将鼠标的x坐标给Rect对象的左边
                elif event.pos[0] >= w - 186 and event.pos[0] <= w:
                    board_rect.left = w - 186
                # board_rect.top = h - 17 #档板位置在底部
        elif event.type == pygame.MOUSEBUTTONDOWN:  #鼠标按键按下
            # 将鼠标当前位置给挡板
            if event.button == 1:
                if event.pos[0] >= 0 and event.pos[0] < w - 186:#判断鼠标的位置
                    board_rect.left = event.pos[0] #将鼠标的x坐标给Rect对象的左边
                if event.pos[0] >= w - 186 and event.pos[0] <= w:
                    board_rect.left = w - 186
                # board_rect.top = h - 17

4、挡板接住小球并得分

# 下方挡板接到小球
    if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x <= board_rect.left + 206):
        move_y = - move_y  # y方向速度反向
        score += points  #得分
        count += 1   #次数增加1次
        if count == 5:  # 每满五次,难度和单次接球得分增加
            count = 0  # 接球得分的次数清零
            points += points
            # x方向速度增加
            if move_x > 0:
                move_x += 1
            else:
                move_x -= 1
            move_y -= 1

5、小球未接住小球

# 下方挡板未接到小球
    if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206):
        # 游戏结束
        ball_y = 200  #小球所在的位置
        break

6、小球移动

# 移动小球
    ball_x += move_x
    ball_y += move_y
    if ball_x <= 20 or ball_x >= w - 20:  # 碰到左右两侧墙壁
        move_x = - move_x  # x方向速度反向
    if ball_y <= 20:  # 碰到上方墙壁
        move_y = - move_y  # y方向速度反向

7、显示分数

my_score = font.render(str(score), False, (255, 255, 0))  # 创建文字对象(文字,是否平滑,文字颜色)
    screen.blit(my_score, (w - 100, 30))  # 将文字添加到窗口上

三、完整代码

import sys
from random import randint
import pygame

pygame.init() # 初始化

size = w, h = (600,500) # 屏幕显示的区域,高度和宽度
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
pygame.display.set_caption("接球游戏") # 屏幕的标题
fpsClock = pygame.time.Clock() # 帧速率 窗口刷新速度 越大运行越快

board = pygame.image.load(r"D:\pycharm\WorkTime(大二上)\挡板.jpg")
board_rect = board.get_rect() #对图片进行加框 利用surface生成rect

color = pygame.Color(255,255,255) # 屏幕(窗口)的颜色:白色
Green = pygame.Color('green') # 小球的颜色:绿色

# 随机生成小球的x、y坐标(整数,包括两端)
ball_x = randint(20,580)
ball_y = randint(20,200)

# 小球x、y坐标变化量
move_x = 1
move_y = 1

# 挡板x、y坐标变化量
board_x = 46
board_y = 0

score=0	#得分
font=pygame.font.Font(r'D:\字库\书法和新增字库\微软雅黑.ttf',60) #设置字体(前者是字体路径)和字体大小
points=1 #一次接球的加分
count=0	#接球得分的次数

# size1 = board.get_size() #获取图片大小
# print(size1)
while True:
    board_rect.top = h - 17
    for event in pygame.event.get(): # pygame.event.get() 从事件队列中取出事件,并从队列中删除该事件
        if event.type == pygame.QUIT:
            sys.exit()

        # 改变窗口尺寸
        elif event.type == pygame.VIDEORESIZE:
            size = w,h = event.w,event.h
            screen = pygame.display.set_mode(size,pygame.RESIZABLE)


        # 键盘控制挡板
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:  # 挡板左移
                if board_rect.left > 0 and board_rect.left <= w - 186:
                    board_rect.left -= board_x
                elif board_rect.left <= 0:  # 判断挡板左边的坐标是否小于0
                    board_rect.left = 0
                    board_rect.top -= board_y
            elif event.key == pygame.K_RIGHT:  # 挡板右移
                if board_rect.right >= 186 and board_rect.right < w:
                    board_rect.right += board_x
                elif board_rect.right >= w:  # 判断挡板右边的坐标是否大于屏幕的宽度                        board_rect.right = w
                    board_rect.bottom += board_y

        #鼠标控制挡板
        elif event.type == pygame.MOUSEMOTION:
            # 鼠标左键按下并跟随鼠标移动
            if event.buttons[0] == 1:
                if event.pos[0] >= 0 and event.pos[0] < w - 186:#判断鼠标的位置
                    board_rect.left = event.pos[0] #将鼠标的x坐标给Rect对象的左边
                elif event.pos[0] >= w - 186 and event.pos[0] <= w:
                    board_rect.left = w - 186
                # board_rect.top = h - 17 #档板位置在底部
        elif event.type == pygame.MOUSEBUTTONDOWN:  #鼠标按键按下
            # 将鼠标当前位置给挡板
            if event.button == 1:
                if event.pos[0] >= 0 and event.pos[0] < w - 186:#判断鼠标的位置
                    board_rect.left = event.pos[0] #将鼠标的x坐标给Rect对象的左边
                if event.pos[0] >= w - 186 and event.pos[0] <= w:
                    board_rect.left = w - 186
                # board_rect.top = h - 17

    # 下方挡板接到小球
    if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x <= board_rect.left + 206):
        move_y = - move_y  # y方向速度反向
        score += points
        count += 1
        if count == 5:  # 每满五次,难度和单次接球得分增加
            count = 0  # 接球得分的次数清零
            points += points
            # x方向速度增加
            if move_x > 0:
                move_x += 1
            else:
                move_x -= 1
            move_y -= 1

    # 下方挡板未接到小球
    if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206):
        # 游戏结束
        ball_y = 200
        break

    # 移动小球
    ball_x += move_x
    ball_y += move_y
    if ball_x <= 20 or ball_x >= w - 20:  # 碰到左右两侧墙壁
        move_x = - move_x  # x方向速度反向
    if ball_y <= 20:  # 碰到上方墙壁
        move_y = - move_y  # y方向速度反向

    fpsClock.tick(200)
    screen.fill(color)
    # 显示分数
    my_score = font.render(str(score), False, (255, 255, 0))  # 创建文字对象(文字,是否平滑,文字颜色)
    screen.blit(my_score, (w - 100, 30))  # 将文字添加到窗口上
    screen.blit(board,board_rect)  #将一个图像绘制在另一个图像上 把surface对象覆盖到移动后的rect对象
    pygame.draw.circle(screen, Green, (ball_x, ball_y), 20)  # 绘制小球
    pygame.display.update() # 对显示窗口进行更新,默认窗口全部重绘

到此这篇关于python中的pygame实现接球小游戏的文章就介绍到这了!


Tags in this post...

Python 相关文章推荐
Linux下使用python调用top命令获得CPU利用率
Mar 10 Python
Python实现对PPT文件进行截图操作的方法
Apr 28 Python
剖析Django中模版标签的解析与参数传递
Jul 21 Python
python实现文本去重且不打乱原本顺序
Jan 26 Python
Python 中的with关键字使用详解
Sep 11 Python
python记录程序运行时间的三种方法
Jul 14 Python
Python使用遗传算法解决最大流问题
Jan 29 Python
Python 3.6 -win64环境安装PIL模块的教程
Jun 20 Python
Python从文件中读取指定的行以及在文件指定位置写入
Sep 06 Python
Win10里python3创建虚拟环境的步骤
Jan 31 Python
python实现ping命令小程序
Dec 28 Python
如何使用Python实现一个简易的ORM模型
May 12 Python
python游戏开发Pygame框架
Apr 22 #Python
python中的random模块和相关函数详解
Apr 22 #Python
Python写情书? 10行代码展示如何把情书写在她的照片里
Apr 21 #Python
微信小程序调用python模型
Apr 21 #Python
使用python绘制分组对比柱状图
使用python将HTML转换为PDF pdfkit包(wkhtmltopdf) 的使用方法
Apr 21 #Python
Python尝试实现蒙特卡罗模拟期权定价
You might like
PHP中的extract的作用分析
2008/04/09 PHP
PHP笔记之:日期函数的使用介绍
2013/04/24 PHP
PHP源码分析之变量的存储过程分解
2014/07/03 PHP
PHP中Laravel 关联查询返回错误id的解决方法
2017/04/01 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
2020/03/26 PHP
innerHTML 和 getElementsByName 在IE下面的bug 的解决
2010/04/09 Javascript
一段批量给页面上的控件赋值js
2010/06/19 Javascript
js动态创建、删除表格示例代码
2013/08/07 Javascript
Position属性之relative用法
2015/12/14 Javascript
JS实现的仿QQ空间图片弹出效果代码
2016/02/23 Javascript
jquery延迟对象解析
2016/10/26 Javascript
JavaScript 实现 Tab 点击切换实例代码
2017/03/25 Javascript
vue.js中v-on:textInput无法执行事件问题的解决过程
2017/07/12 Javascript
基于JavaScript实现前端数据多条件筛选功能
2020/08/19 Javascript
js 索引下标之li集合绑定点击事件
2018/01/12 Javascript
详解使用React进行组件库开发
2018/02/06 Javascript
让你5分钟掌握9个JavaScript小技巧
2018/06/09 Javascript
vue项目首屏打开速度慢的解决方法
2019/03/31 Javascript
vuejs数据超出单行显示更多,点击展开剩余数据实例
2019/05/05 Javascript
NProgress显示顶部进度条效果及使用详解
2019/09/21 Javascript
使用node.JS中的url模块解析URL信息
2020/02/06 Javascript
javascript实现前端成语点击验证优化
2020/06/24 Javascript
Electron+vue从零开始打造一个本地播放器的方法示例
2020/10/27 Javascript
[02:07]TI9显影之尘系列 - Vici Gaming
2019/08/20 DOTA
Python不规范的日期字符串处理类
2014/06/10 Python
Python打包可执行文件的方法详解
2016/09/19 Python
Python中函数参数调用方式分析
2018/08/09 Python
Python设计模式之抽象工厂模式原理与用法详解
2019/01/15 Python
Python爬虫之Selenium实现键盘事件
2020/12/04 Python
纯CSS3实现滚动的齿轮动画效果
2014/06/05 HTML / CSS
英国家电直销:Appliances Direct
2016/09/22 全球购物
竞选村长演讲稿
2014/04/28 职场文书
餐饮周年庆活动方案
2014/08/14 职场文书
介绍信范文
2015/01/31 职场文书
2015年采购部工作总结
2015/04/23 职场文书
未来,这5大方向都很适合创业
2019/07/22 职场文书