python使用pygame实现笑脸乒乓球弹珠球游戏
作者:HelloWorld! 时间:2021-07-26 09:49:47
今天我们用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()
代码中用的乒乓球是如下图片。
总结
1、通过上述代码,功能基本实现
2、可以有很多改进,如通过键盘来操控球拍,如给游戏加上背景音乐,其中加音乐的方法是
pop = pygame.mixer.Sound("pop.wav")
pop.play()
来源:https://blog.csdn.net/weixin_45690272/article/details/103218445
标签:pygame,乒乓球,弹珠球
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
面向对象CSS FAQ[译]
2009-10-27 15:59:00
Python2和Python3之间的str处理方式导致乱码的讲解
2022-10-01 09:46:53
Python格式化日期时间操作示例
2022-04-23 23:07:19
升级SQL Server 2008数据库引擎
2009-03-25 12:58:00
详解python3中socket套接字的编码问题解决
2023-09-11 20:28:48
Python处理json字符串转化为字典的简单实现
2022-05-15 01:42:58
Access数据库用另一种方式管理密码
2008-10-13 12:25:00
JavaScript,5种调用函数的方法[译]
2009-02-24 16:26:00
一个不错的js+css二级分类菜单代码
2007-12-28 21:22:00
![](https://img.aspxhome.com/file/UploadPic/200712/28/20071228213234672s.gif)
Python/Django后端使用PIL Image生成头像缩略图
2021-12-18 09:46:27
golang定时任务cron项目实操指南
2023-08-26 00:21:40
![](https://img.aspxhome.com/file/2023/8/99648_0s.jpg)
wxPython中wx.gird.Gird添加按钮的实现
2021-07-05 15:13:19
![](https://img.aspxhome.com/file/2023/9/86269_0s.png)
公用样式模板的设计制作
2009-09-13 21:27:00
![](https://img.aspxhome.com/file/UploadPic/20099/13/sys1-12s.jpg)
python使用js2py库运行js代码
2022-09-06 17:06:55
学习javascript,实现插入排序实现代码
2023-08-07 10:41:08
python中的format是什么意思,format怎么用
2022-07-31 08:51:45
![](https://img.aspxhome.com/file/2023/4/75354_0s.jpg)
python技巧分享Excel创建和修改
2022-01-09 12:04:59
![](https://img.aspxhome.com/file/2023/4/72234_0s.jpg)
win10子系统python开发环境准备及kenlm和nltk的使用教程
2023-07-31 01:48:28
python中的classmethod与staticmethod
2021-07-14 00:43:29
![](https://img.aspxhome.com/file/2023/4/70234_0s.png)
asp.net aspnetpager分页统计时与实际不符的解决办法
2023-06-30 04:02:32