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实现字符串反转的常用方法分析【4种方法】
Sep 30 Python
Python元字符的用法实例解析
Jan 17 Python
详解Django 中是否使用时区的区别
Jun 14 Python
Django配置celery(非djcelery)执行异步任务和定时任务
Jul 16 Python
Pycharm无法显示动态图片的解决方法
Oct 28 Python
python 处理数字,把大于上限的数字置零实现方法
Jan 28 Python
用Python写一个自动木马程序
Sep 17 Python
Python写出新冠状病毒确诊人数地图的方法
Feb 12 Python
python itsdangerous模块的具体使用方法
Feb 17 Python
Python是什么 Python的用处
May 26 Python
python利用文件时间批量重命名照片和视频
Feb 09 Python
字典算法实现及操作 --python(实用)
Mar 31 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 UTF-8、Unicode和BOM问题
2010/05/18 PHP
PHP创建/删除/复制文件夹、文件
2016/05/03 PHP
PHP中trait使用方法详细介绍
2017/05/21 PHP
PHP利用Socket获取网站的SSL证书与公钥
2017/06/18 PHP
PHP实现数组转JSon和JSon转数组的方法示例
2018/06/14 PHP
javascript客户端遍历控件与获取父容器对象示例代码
2014/01/06 Javascript
理解 JavaScript Scoping &amp; Hoisting(二)
2015/11/18 Javascript
AngularJS 简单应用实例
2016/07/28 Javascript
分分钟学会vue中vuex的应用(入门教程)
2017/09/14 Javascript
Node.JS 循环递归复制文件夹目录及其子文件夹下的所有文件
2017/09/18 Javascript
vue组件与复用详解
2018/04/08 Javascript
vue-cli设置css不生效的解决方法
2020/02/07 Javascript
vue 路由缓存 路由嵌套 路由守卫 监听物理返回操作
2020/08/06 Javascript
原生JS生成指定位数的验证码
2020/10/28 Javascript
python检测远程服务器tcp端口的方法
2015/03/14 Python
编写Python脚本来实现最简单的FTP下载的教程
2015/05/04 Python
python简单实现刷新智联简历
2016/03/30 Python
matplotlib绘制符合论文要求的图片实例(必看篇)
2017/06/02 Python
Python爬取豆瓣视频信息代码实例
2019/11/16 Python
python 输入字符串生成所有有效的IP地址(LeetCode 93号题)
2020/10/15 Python
python中requests模拟登录的三种方式(携带cookie/session进行请求网站)
2020/11/17 Python
selenium框架中driver.close()和driver.quit()关闭浏览器
2020/12/08 Python
实习生个人的自我评价
2013/12/08 职场文书
学前教育求职自荐信范文
2013/12/25 职场文书
职业生涯规划书前言
2014/04/15 职场文书
大学新闻系自荐书
2014/05/31 职场文书
大学生创业计划书
2014/08/14 职场文书
八荣八耻的活动方案
2014/08/16 职场文书
做一个有道德的人活动方案
2014/08/25 职场文书
班主任2015新年寄语
2014/12/08 职场文书
公证书格式
2015/01/23 职场文书
2015年酒店年度工作总结
2015/05/23 职场文书
毕业典礼主持词
2015/06/29 职场文书
英语导游欢迎词
2015/09/30 职场文书
加强党性修养心得体会
2016/01/21 职场文书
pytorch 权重weight 与 梯度grad 可视化操作
2021/06/05 Python