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检测手机QQ在线状态的脚本代码
Feb 10 Python
在Python中实现贪婪排名算法的教程
Apr 17 Python
Python实现的数据结构与算法之快速排序详解
Apr 22 Python
使用Python来开发Markdown脚本扩展的实例分享
Mar 04 Python
两个命令把 Vim 打造成 Python IDE的方法
Mar 20 Python
python django 实现验证码的功能实例代码
May 18 Python
Python使用微信接入图灵机器人过程解析
Nov 04 Python
Python读取YAML文件过程详解
Dec 30 Python
基于Python和PyYAML读取yaml配置文件数据
Jan 13 Python
python词云库wordCloud使用方法详解(解决中文乱码)
Feb 17 Python
基于python实现获取网页图片过程解析
May 11 Python
Python Matplotlib简易教程(小白教程)
Jul 28 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实现栈数据结构和括号匹配算法示例
2017/08/10 PHP
PHP实现Redis单据锁以及防止并发重复写入
2018/04/10 PHP
基于thinkphp6.0的success、error实现方法
2019/11/05 PHP
详细分析PHP 命名空间(namespace)
2020/06/30 PHP
javascript函数库-集合框架
2007/04/27 Javascript
jQuery powerFloat万能浮动层下拉层插件使用介绍
2010/12/27 Javascript
js中escape对应的C#解码函数 UrlDecode
2012/12/16 Javascript
js 三级关联菜单效果实例
2013/08/13 Javascript
JavaScript原生编写《飞机大战坦克》游戏完整实例
2017/01/04 Javascript
JS实现PC手机端和嵌入式滑动拼图验证码三种效果
2017/02/15 Javascript
javascript定时器取消定时器及优化方法
2017/07/08 Javascript
微信小程序实现炫酷的弹出式菜单特效
2019/01/28 Javascript
微信小程序云开发之模拟后台增删改查
2019/05/16 Javascript
vue源码nextTick使用及原理解析
2019/08/13 Javascript
[02:58]献给西雅图的情书_高清
2014/05/29 DOTA
Python的Tornado框架的异步任务与AsyncHTTPClient
2016/06/27 Python
Python中is与==判断的区别
2017/03/28 Python
python调用API实现智能回复机器人
2018/04/10 Python
Pycharm 创建 Django admin 用户名和密码的实例
2018/05/30 Python
Python程序包的构建和发布过程示例详解
2019/06/09 Python
pyinstaller打包多个py文件和去除cmd黑框的方法
2019/06/21 Python
基于Tensorflow使用CPU而不用GPU问题的解决
2020/02/07 Python
Python基于pandas爬取网页表格数据
2020/05/11 Python
经典c++面试题六
2012/01/18 面试题
会计学自我鉴定
2014/02/06 职场文书
护士的自我鉴定
2014/02/07 职场文书
党风廉设责任书
2014/04/16 职场文书
保密工作责任书
2014/04/16 职场文书
师德师风剖析材料
2014/09/30 职场文书
群众路线剖析材料范文
2014/10/09 职场文书
员工家属慰问信
2015/03/24 职场文书
小学思想品德教学反思
2016/02/24 职场文书
靠谱的活动总结
2019/04/16 职场文书
创业计划书之熟食店
2019/10/16 职场文书
python_tkinter弹出对话框创建
2022/03/20 Python
iOS 16进一步确认,一共支持16款iPhone
2022/04/28 数码科技