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中设置变量访问权限的方法
Apr 27 Python
如何使用python爬取csdn博客访问量
Feb 14 Python
利用python获取某年中每个月的第一天和最后一天
Dec 15 Python
Python编写一个闹钟功能
Jul 11 Python
基于Python函数的作用域规则和闭包(详解)
Nov 29 Python
python实现树形打印目录结构
Mar 29 Python
Python利用lxml模块爬取豆瓣读书排行榜的方法与分析
Apr 15 Python
使用Python实现将list中的每一项的首字母大写
Jun 11 Python
PyCharm 2019.3发布增加了新功能一览
Dec 08 Python
Numpy之reshape()使用详解
Dec 26 Python
Python configparser模块应用过程解析
Aug 14 Python
python利用paramiko实现交换机巡检的示例
Sep 22 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语法速查表
2007/01/02 PHP
php调用Google translate_tts api实现代码
2013/08/07 PHP
php将csv文件导入到mysql数据库的方法
2014/12/24 PHP
学习php设计模式 php实现桥梁模式(bridge)
2015/12/07 PHP
基于thinkPHP类的插入数据库操作功能示例
2017/01/06 PHP
thinkphp5实现无限级分类
2019/02/18 PHP
Laravel5.1 框架响应基本用法实例分析
2020/01/04 PHP
php redis setnx分布式锁简单原理解析
2020/10/23 PHP
javascript表单验证 - Parsley.js使用和配置
2013/01/25 Javascript
javascript实用小函数使用介绍
2013/11/11 Javascript
基于javascript实现窗口抖动效果
2016/01/03 Javascript
el表达式 写入bootstrap表格数据页面的实例代码
2017/01/11 Javascript
webpack学习--webpack经典7分钟入门教程
2017/06/28 Javascript
BootStrap Table复选框默认选中功能的实现代码(从数据库获取到对应的状态进行判断是否为选中状态)
2017/07/11 Javascript
JavaScript实现音乐自动切换和轮播
2017/11/05 Javascript
angularjs实现table增加tr的方法
2018/02/27 Javascript
JavaScript实现百度搜索框效果
2020/03/26 Javascript
Vue渲染过程浅析
2019/03/14 Javascript
浅谈React中组件逻辑复用的那些事儿
2020/05/21 Javascript
[03:00]《DAC最前线》之欧美新秀VS老将
2015/02/01 DOTA
[40:06]DOTA2亚洲邀请赛 4.3 突围赛 Liquid vs VGJ.T 第一场
2018/04/04 DOTA
推荐下python/ironpython:从入门到精通
2007/10/02 Python
Python通过RabbitMQ服务器实现交换机功能的实例教程
2016/06/29 Python
python tornado微信开发入门代码
2018/08/24 Python
python openCV实现摄像头获取人脸图片
2020/08/20 Python
加拿大高尔夫超市:Golf Town
2018/01/12 全球购物
乡镇纠风工作实施方案
2014/03/22 职场文书
施工安全汇报材料
2014/08/17 职场文书
2014优秀大学生简历自我评价
2014/09/15 职场文书
迟到检讨书2000字(精选篇)
2014/10/07 职场文书
党员干部四风问题整改措施思想汇报
2014/10/12 职场文书
写给妈妈的感谢信
2015/01/22 职场文书
2015年学生管理工作总结
2015/05/26 职场文书
家长会主持词开场白
2015/05/29 职场文书
尼克胡哲观后感
2015/06/08 职场文书
2015暑期社会实践个人总结
2015/07/13 职场文书