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 Django做网页
Nov 04 Python
python单链表实现代码实例
Nov 21 Python
Python编写检测数据库SA用户的方法
Jul 11 Python
Python标准库笔记struct模块的使用
Feb 22 Python
Python读取视频的两种方法(imageio和cv2)
Apr 15 Python
python判断一个集合是否为另一个集合的子集方法
May 04 Python
用PyInstaller把Python代码打包成单个独立的exe可执行文件
May 26 Python
Python中利用LSTM模型进行时间序列预测分析的实现
Jul 26 Python
8段用于数据清洗Python代码(小结)
Oct 31 Python
Pycharm内置终端及远程SSH工具的使用教程图文详解
Mar 19 Python
Python预测2020高考分数和录取情况
Jul 08 Python
用Python制作mini翻译器的实现示例
Aug 17 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管理内存函数 memory_get_usage()使用介绍
2012/09/23 PHP
给ECShop添加最新评论
2015/01/07 PHP
php构造函数与析构函数
2016/04/23 PHP
javascript中input中readonly和disabled区别介绍
2012/10/23 Javascript
JS中setTimeout()的用法详解
2013/04/14 Javascript
javascript数组操作方法小结和3个属性详细介绍
2014/07/05 Javascript
js使用正则实现ReplaceAll全部替换的方法
2014/07/18 Javascript
jQuery实现的超酷苹果风格图标滑出菜单效果代码
2015/09/16 Javascript
JavaScript 对象深入学习总结(经典)
2015/09/29 Javascript
javascript 中关于array的常用方法详解
2017/05/05 Javascript
jQuery 实现双击编辑表格功能
2017/06/19 jQuery
BetterScroll 在移动端滚动场景的应用
2017/09/18 Javascript
webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)
2018/01/09 Javascript
微信小程序五子棋游戏的悔棋实现方法【附demo源码下载】
2019/02/20 Javascript
vuejs移动端实现div拖拽移动
2019/07/25 Javascript
解决vue-photo-preview 异步图片放大失效的问题
2020/07/29 Javascript
使用python的chardet库获得文件编码并修改编码
2014/01/22 Python
Python守护进程用法实例分析
2015/06/04 Python
python获得文件创建时间和修改时间的方法
2015/06/30 Python
python实现指定文件夹下的指定文件移动到指定位置
2018/09/17 Python
对python过滤器和lambda函数的用法详解
2019/01/21 Python
Python对HTML转义字符进行反转义的实现方法
2019/04/28 Python
基于python实现删除指定文件类型
2020/07/21 Python
Flask-SocketIO服务端安装及使用代码示例
2020/11/26 Python
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
2020/12/09 Python
The Kooples美国官方网站:为情侣提供的法国当代时尚品牌
2019/01/03 全球购物
阿玛尼美妆俄罗斯官网:Giorgio Armani Beauty RU
2020/07/19 全球购物
生日派对邀请函
2014/01/13 职场文书
班队活动设计方案
2014/01/30 职场文书
小学生演讲稿大全
2014/04/25 职场文书
医院领导班子整改方案
2014/10/01 职场文书
个人培训总结
2015/03/05 职场文书
《倍数和因数》教学反思
2016/02/23 职场文书
python中的None与NULL用法说明
2021/05/25 Python
详解Redis复制原理
2021/06/04 Redis
mysql 联合索引生效的条件及索引失效的条件
2021/11/20 MySQL