python使用pygame实现笑脸乒乓球弹珠球游戏


Posted in Python onNovember 25, 2019

今天我们用python和pygame实现一个乒乓球的小游戏,或者叫弹珠球游戏。

笑脸乒乓球游戏功能介绍

乒乓球游戏功能如下:

乒乓球从屏幕上方落下,用鼠标来移动球拍,使其反弹回去,并获得得分,如果没有接到该球,则失去一条命。玩家有一定数量的命如5。

游戏设计思路

根据游戏规则,我们需要

1、初始化游戏环境
2、画出乒乓球,球拍等
3、设置乒乓球的运动,并监听鼠标,以移动球拍
4、判断乒乓球被接住与否
5、游戏是否结束,是否再玩。

代码实现

import pygame
pygame.init()
screen_width=800
screen_height=600
screen=pygame.display.set_mode([screen_width,screen_height])
pygame.display.set_caption("笑脸乒乓球")
keepGoing=True
pic=pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx=0
picy=0
BLACK=(0,0,0)
WHITE=(255,255,255)
timer=pygame.time.Clock()
paddle_width=200
paddle_height=25
paddle_x=300
paddle_y=550

speedx=5
speedy=5
#图片的高度和宽度
pic_width=pic.get_width()
pic_height=pic.get_height()
#分数和命
points=0
lives=5
font=pygame.font.SysFont("Times",24)
pop = pygame.mixer.Sound("pop.wav")

while keepGoing:
 for event in pygame.event.get():
 if event.type==pygame.QUIT:
  keepGoing=False
 if event.type == pygame.KEYDOWN:
  if event.key == pygame.K_F1: # F1 = New Game
  points = 0
  lives = 5
  picx = 0
  picy = 0
  speedx = 5
  speedy = 5

 pop.play()
 picx += speedx
 picy += speedy
 if picx <= 0 or picx >= 700:
 speedx = -speedx * 1.1
 if picy <= 0:
 speedy = -speedy + 1
 if picy >= 500:
 lives -= 1
 speedy = -5
 speedx = 5
 picy = 499
 # if picx <= 0 or picx + pic_width > screen_width:
 # speedx = -speedx
 # if picy <= 0:
 # speedy = -speedy
 # if picy >= 500:
 # lives -= 1
 # speedy = -speedy
 screen.fill(BLACK)
 screen.blit(pic, (picx, picy))
 # 画出球拍
 paddle_x = pygame.mouse.get_pos()[0]
 paddle_x -= paddle_width / 2
 pygame.draw.rect(screen, WHITE, (paddle_x, paddle_y, paddle_width, paddle_height))
 #判断接住乒乓球
 if picy + pic_width > paddle_y and picy + pic_height < paddle_y + paddle_height and speedy > 0:
 if picx + pic_width / 2 > paddle_x and picx + pic_width / 2 < paddle_x + paddle_width:
  points += 1
  speedy = -speedy
 # 在屏幕上画出得分

 draw_string = "Lives: " + str(lives) + " Points: " + str(points)
 if lives<1:
 draw_string="Game Over. Your scores is "+str(points)
 draw_string+="press F1 to play again"
 text = font.render(draw_string, True, WHITE)
 text_rect = text.get_rect()
 text_rect.centerx = screen.get_rect().centerx
 text_rect.y = 10
 screen.blit(text, text_rect)
 pygame.display.update()
 timer.tick(60)

pygame.quit()

代码中用的乒乓球是如下图片。

python使用pygame实现笑脸乒乓球弹珠球游戏

总结

1、通过上述代码,功能基本实现
2、可以有很多改进,如通过键盘来操控球拍,如给游戏加上背景音乐,其中加音乐的方法是

pop = pygame.mixer.Sound("pop.wav")
pop.play()

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

Python 相关文章推荐
Python正则表达式的使用范例详解
Aug 08 Python
Python内置函数dir详解
Apr 14 Python
Python 获取主机ip与hostname的方法
Dec 17 Python
Python常用爬虫代码总结方便查询
Feb 25 Python
Python实现查找字符串数组最长公共前缀示例
Mar 27 Python
python二进制文件的转译详解
Jul 03 Python
解决django后台管理界面添加中文内容乱码问题
Nov 15 Python
python3的UnicodeDecodeError解决方法
Dec 20 Python
python Manager 之dict KeyError问题的解决
Dec 21 Python
Python正则表达式学习小例子
Mar 03 Python
python GUI库图形界面开发之PyQt5布局控件QGridLayout详细使用方法与实例
Mar 06 Python
Python切片列表字符串如何实现切换
Aug 06 Python
Django项目基础配置和基本使用过程解析
Nov 25 #Python
nginx+uwsgi+django环境搭建的方法步骤
Nov 25 #Python
python找出列表中大于某个阈值的数据段示例
Nov 24 #Python
python对Excel按条件进行内容补充(推荐)
Nov 24 #Python
使用Python的datetime库处理时间(RPA流程)
Nov 24 #Python
Python 中判断列表是否为空的方法
Nov 24 #Python
python3中利用filter函数输出小于某个数的所有回文数实例
Nov 24 #Python
You might like
PHP高级OOP技术演示
2009/08/27 PHP
php下清空字符串中的HTML标签的代码
2010/09/06 PHP
自定义php类(查找/修改)xml文档
2013/03/26 PHP
ubutu 16.04环境下,PHP与mysql数据库,网页登录验证实例讲解
2017/07/20 PHP
js实现目录定位正文示例
2013/11/14 Javascript
Javscript调用iframe框架页面中函数的方法
2014/11/01 Javascript
JavaScript计算两个日期时间段内日期的方法
2015/03/16 Javascript
jQuery实现响应鼠标滚动的动感菜单效果
2015/09/21 Javascript
jQuery ajax分页插件实例代码
2016/01/27 Javascript
Windows环境下npm install 报错: operation not permitted, rename的解决方法
2016/09/26 Javascript
微信小程序  网络请求API详解
2016/10/25 Javascript
DOM 事件的深入浅出(二)
2016/12/05 Javascript
JS之if语句对接事件动作逻辑(详解)
2017/06/28 Javascript
微信小程序授权获取用户详细信息openid的实例详解
2017/09/20 Javascript
使用ef6创建oracle数据库的实体模型遇到的问题及解决方案
2017/11/09 Javascript
详解es6超好用的语法糖Decorator
2018/08/01 Javascript
基于js实现的图片拖拽排序源码实例
2020/11/04 Javascript
在主机商的共享服务器上部署Django站点的方法
2015/07/22 Python
Python实现感知器模型、两层神经网络
2017/12/19 Python
python thrift搭建服务端和客户端测试程序
2018/01/17 Python
python通过配置文件共享全局变量的实例
2019/01/11 Python
在Python 中实现图片加框和加字的方法
2019/01/26 Python
如何通过雪花算法用Python实现一个简单的发号器
2019/07/03 Python
对tensorflow 中tile函数的使用详解
2020/02/07 Python
python dict如何定义
2020/09/02 Python
python编程的核心知识点总结
2021/02/08 Python
HTML5 Canvas绘制文本及图片的基础教程
2016/03/14 HTML / CSS
JINS眼镜官方网站:日本最大的眼镜邮购
2016/10/14 全球购物
高中生毕业自我鉴定范文
2013/12/22 职场文书
销售员个人求职的自我评价
2014/02/10 职场文书
进口业务员岗位职责
2014/04/06 职场文书
服务行业口号
2014/06/11 职场文书
人力资源管理毕业生自荐信
2014/06/26 职场文书
教师个人师德工作总结2015
2015/05/12 职场文书
手把手教你怎么用Python实现zip文件密码的破解
2021/05/27 Python
Tomcat项目启动失败的原因和解决办法
2022/04/20 Servers