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编程判断这天是这一年第几天的方法示例
Apr 18 Python
Python实现复杂对象转JSON的方法示例
Jun 22 Python
Python如何快速实现分布式任务
Jul 06 Python
python Spyder界面无法打开的解决方法
Apr 27 Python
Pyqt5实现英文学习词典
Jun 24 Python
django框架实现模板中获取request 的各种信息示例
Jul 01 Python
Python qqbot 实现qq机器人的示例代码
Jul 11 Python
python构建指数平滑预测模型示例
Nov 21 Python
解决matplotlib.pyplot在Jupyter notebook中不显示图像问题
Apr 22 Python
python IDLE添加行号显示教程
Apr 25 Python
Python分类测试代码实例汇总
Jul 23 Python
python 利用opencv实现图像网络传输
Nov 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 网页游戏开发入门教程一(webgame+design)
2009/10/26 PHP
php实现简单的权限管理的示例代码
2017/08/25 PHP
PHP实现通过strace定位故障原因的方法
2018/04/29 PHP
自动更新作用
2006/10/08 Javascript
PJBlog插件 防刷新的在线播放器
2006/10/25 Javascript
javascript 面向对象 function类
2010/05/13 Javascript
Javascript 自适应高度的Tab选项卡
2011/04/05 Javascript
利用JS自动打开页面上链接的实现代码
2011/09/25 Javascript
js一般方法改写成面向对象方法的无限级折叠菜单示例代码
2013/07/04 Javascript
Javascript学习笔记之 对象篇(四) : for in 循环
2014/06/24 Javascript
Jquery中基本选择器用法实例详解
2015/05/18 Javascript
浅析nodejs实现Websocket的数据接收与发送
2015/11/19 NodeJs
js+html5实现半透明遮罩层弹框效果
2020/08/24 Javascript
vue 插值 v-once,v-text, v-html详解
2018/01/19 Javascript
JavaScript如何对图片进行黑白化
2018/04/10 Javascript
微信小程序实现星级评分和展示
2018/07/05 Javascript
JavaScript中发出HTTP请求最常用的方法
2018/07/12 Javascript
JS中自定义事件的使用与触发操作实例分析
2019/11/01 Javascript
Vue-cli3多页面配置详解
2020/03/22 Javascript
Js和VUE实现跑马灯效果
2020/05/25 Javascript
[05:09]第二届DOTA2亚洲邀请赛决赛日比赛集锦:iG 3:0 OG夺冠
2017/04/05 DOTA
Python类的多重继承问题深入分析
2014/11/09 Python
Python爬取当当、京东、亚马逊图书信息代码实例
2017/12/09 Python
python微信跳一跳系列之棋子定位颜色识别
2018/02/26 Python
pytorch 实现tensor与numpy数组转换
2019/12/27 Python
Python 打印自己设计的字体的实例讲解
2021/01/04 Python
实例教程 纯CSS3打造非常炫的加载动画效果
2014/11/05 HTML / CSS
美国领先的户外服装与装备用品店:Moosejaw
2016/08/25 全球购物
Nip + Fab官网:英国美容品牌
2019/08/26 全球购物
大学生通用个人的自我评价
2014/02/10 职场文书
高考1977观后感
2015/06/04 职场文书
新学期开学寄语2016
2015/12/04 职场文书
python 实现定时任务的四种方式
2021/04/01 Python
html5 录制mp3音频支持采样率和比特率设置
2021/07/15 Javascript
pandas数值排序的实现实例
2021/07/25 Python
利用 JavaScript 构建命令行应用
2021/11/17 Javascript