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 相关文章推荐
Python内置函数的用法实例教程
Sep 08 Python
Python自动重试HTTP连接装饰器
Apr 28 Python
使用Python的Django框架结合jQuery实现AJAX购物车页面
Apr 11 Python
50行Python代码实现人脸检测功能
Jan 23 Python
Django使用paginator插件实现翻页功能的实例
Oct 24 Python
selenium3+python3环境搭建教程图解
Dec 07 Python
在Pycharm中设置默认自动换行的方法
Jan 16 Python
Python求两点之间的直线距离(2种实现方法)
Jul 07 Python
pandas 对group进行聚合的例子
Dec 27 Python
python爬虫用scrapy获取影片的实例分析
Nov 23 Python
python Polars库的使用简介
Apr 21 Python
Python中的turtle画箭头,矩形,五角星
Mar 16 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
一些花式咖啡的配方
2021/03/03 冲泡冲煮
PHP伪静态写法附代码
2008/06/20 PHP
根据ip调用新浪api获取城市名并转成拼音
2014/03/07 PHP
PHP中你应该知道的require()文件包含的正确用法
2015/06/12 PHP
PHP实现对png图像进行缩放的方法(支持透明背景)
2015/07/15 PHP
PHP使用Mysqli类库实现完美分页效果的方法
2016/04/07 PHP
jQuery LigerUI 插件介绍及使用之ligerDrag和ligerResizable示例代码打包
2011/04/06 Javascript
JavaScript使用shift方法移除素组第一个元素实例分析
2015/04/06 Javascript
js字符串操作方法实例分析
2015/05/06 Javascript
javascript实现网页屏蔽Backspace事件,输入框不屏蔽
2015/07/21 Javascript
关于事件mouseover ,mouseout ,mouseenter,mouseleave的区别
2015/10/12 Javascript
jQuery实现带渐显效果的人物多级关系图代码
2015/10/16 Javascript
javascript 继承学习心得总结
2016/03/17 Javascript
JS中的数组转变成JSON格式字符串的方法
2017/05/09 Javascript
深入理解基于vue-cli的vuex配置
2017/07/24 Javascript
在angularJs中进行数据遍历的2种方法
2018/10/08 Javascript
Python随机数用法实例详解【基于random模块】
2017/04/18 Python
python中找出numpy array数组的最值及其索引方法
2018/04/17 Python
python实现串口自动触发工作的示例
2019/07/02 Python
pytorch 自定义数据集加载方法
2019/08/18 Python
Python 求数组局部最大值的实例
2019/11/26 Python
Python编写一个验证码图片数据标注GUI程序附源码
2019/12/09 Python
关于Python3爬虫利器Appium的安装步骤
2020/07/29 Python
英国第二大营养品供应商:Vitabiotics
2016/10/01 全球购物
.NET面试问题集
2015/12/08 面试题
Java程序员面试题
2013/07/15 面试题
办公室驾驶员岗位职责
2013/11/15 职场文书
父亲追悼会答谢词
2014/01/17 职场文书
羽毛球比赛策划方案
2014/06/13 职场文书
反腐倡廉标语
2014/06/24 职场文书
质量整改报告范文
2014/11/08 职场文书
学校艾滋病宣传活动总结
2015/05/09 职场文书
高中家长意见怎么写
2015/06/03 职场文书
简单实现一个手持弹幕功能+文字抖动特效
2021/03/31 HTML / CSS
Winsows11性能如何? win11性能测评多核竟比Win10差了10%
2021/11/21 数码科技
多台电脑共享文件怎么设置?多台电脑共享文件操作教程
2022/04/08 数码科技