import random
import pygame
pygame.init()  # 1.游戏初始化screen = pygame.display.set_mode((700,600)) # 2.创建一个窗口,设置大小
pygame.display.set_caption("接小球游戏")
ball_x,ball_y = 400,0
ban_x, ban_y ,ban_width, ban_height = 400,550,220,100
font = pygame.font.Font('ziti.ttf',24)
ball_true1 = pygame.image.load("ball.bmp")
ball_true2 = pygame.image.load("ball1.png")
ball_true3 = pygame.image.load("ball2.png")
ball_true4 = pygame.image.load("ball3.png")# pygame.mixer.init() # 2.加载音效
# hit = pygame.mixer.Sound("hit_wall.wav") # 加载音乐
# hit.set_volume(0.4) # 设置音量
##pygame.mixer.music.load("bg.mp3")
#pygame.mixer.music.set_volume(0.3)
#pygame.mixer.music.plat(-1)
score=0
hp = 3
game_over = True
a_left = 0
a_right = 0
while True:key = pygame.key.get_pressed()for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()elif event.type == pygame.MOUSEBUTTONUP:if game_over:game_over = Falsescore = 0hp = 4elif event.type == pygame.MOUSEMOTION:ban_x,_ = event.poselif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:a_right = 0a_left =a_left+10ban_x = ban_x-(10+a_left)elif event.key == pygame.K_RIGHT:a_left = 0a_right = a_right+10ban_x= ban_x+10+a_rightscreen.fill((255,255,255))if game_over:imgtext = font.render('请点击屏幕开始游戏' , True, (0, 0, 0))  # 设置文字参数,内容,锯齿话,颜色screen.blit(imgtext, (350, 300))  # 将文字放在屏幕上else:ball_y = ball_y + 1# 没接到小球的判定if ball_y>600:ball_x = random.randint(0,600)ball_y = 0hp = hp-1if hp ==0:game_over=True# 接到小球的判定结果if ban_x <=ball_x<=ban_x+220 and ban_y <=ball_y<=ban_y+100:score = score+1# hit.play()ball_x = random.randint(0, 600)ball_y = 0imgtext = font.render('分数:%d'%score,True,(0,0,0)) # 设置文字参数,内容,锯齿话,颜色screen.blit(imgtext,(0,0)) # 将文字放在屏幕上imgtext2 = font.render("生命值:%d"%hp,True,(150,162,22))screen.blit(imgtext2,(580,0))# pygame.draw.circle(screen,(100,255,0),(ball_x,ball_y),30,0)if 0< score <=5:screen.blit(ball_true1,(ball_x,ball_y))elif 10> score>=5:screen.blit(ball_true2,(ball_x,ball_y))elif 15>score>=10:screen.blit(ball_true3, (ball_x, ball_y))elif 20>score>=15:screen.blit(ball_true4, (ball_x, ball_y))pygame.draw.rect(screen,(100,255,0),(ban_x, ban_y ,ban_width, ban_height),0)pygame.display.update() #3.不断循环pygame.display.update()  # 3.不断循环

2.0

import random
import pygame
import sysdef figure(screen, photo, x, y):content = pygame.image.load(photo)screen.blit(content, (x, y))def button(screen, x,y,w,h,color):pygame.draw.rect(screen,color,(x,y,w,h))ziti(screen,'排行榜',(x+10),(y+10))def ziti(screen, text, x, y, size = 24,color=(232, 206, 144)):font = pygame.font.Font("ziti.ttf", size)img_text = font.render(text, True, color)  # 设置文字参数,内容,锯齿话,颜色screen.blit(img_text, (x, y))  # 将文字放在屏幕上def run():screen_w, screen_h = 1080, 652score = 0result = {}play_num = 1hp = 3speed = 5ball_x, ball_y = 400, 0ban_x, ban_y, ban_width, ban_height = 400, screen_h - 40, 360, 53# bg_volume = 0.9# sound_volume = 0.4start = "请点击开始游戏"result = ""title = "接亚瑟"# sound_score = "hit_wall.wav"ball_figure = "yase.png"bj_figure = "bg.jpg"# bg_music = "bj.mp3"ban ="ban5.png"game_over = True# 检测按键是不是按着不放moving_left = Falsemoving_right = Falsemoving_up = Falsemoving_down = Falsescreen = pygame.display.set_mode((screen_w, screen_h))  # 2.创建一个窗口,设置大小pygame.display.set_caption(title)# hit = pygame.mixer.Sound(sound_score)  # 加载音乐# hit.set_volume(sound_volume)  # 设置音量# pygame.mixer.music.load(bg_music)# pygame.mixer.music.set_volume(bg_volume)# pygame.mixer_music.play(-1)while True:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()sys.exit()elif event.type == pygame.MOUSEBUTTONUP:# 判断鼠标的坐标是不是在排行榜那个长方形的中间if # 把分数显示到屏幕上if game_over:game_over = Falsescore = 0hp = 4elif event.type == pygame.MOUSEMOTION:ban_x, _ = event.poselif event.type == pygame.KEYDOWN:if event.key == pygame.K_a or event.key == pygame.K_LEFT:moving_left = Trueelif event.key == pygame.K_d or event.key == pygame.K_RIGHT:moving_right = Trueelif event.key == pygame.K_w or event.key == pygame.K_UP:moving_up = Trueelif event.key == pygame.K_s or event.key == pygame.K_DOWN:moving_down = Trueelif event.type == pygame.KEYUP:if event.key == pygame.K_a or event.key == pygame.K_LEFT:moving_left = Falseelif event.key == pygame.K_d or event.key == pygame.K_RIGHT:moving_right = Falseelif event.key == pygame.K_w or event.key == pygame.K_UP:moving_up = Falseelif event.key == pygame.K_s or event.key == pygame.K_DOWN:moving_down = Falsefigure(screen, bj_figure, 0, 0)if game_over:ziti(screen, start, 100,screen_h / 2,50)ziti(screen, result, 100, screen_h / 2 + 50,50)button(screen, screen_w/2-50, 20,100,50,(0,0,0))else:ball_y = ball_y + speedif moving_right:ban_x += 30elif moving_left:ban_x -= 30elif moving_down:ban_y += 30elif moving_up:ban_y -= 30# 没接到小球的判定if ball_y > screen_h:ball_x = random.randint(0, screen_w)ball_y = 0hp = hp - 1speed = 1if hp == 0:start = "重新开始请点击屏幕"result = "你击杀了%d个小球" % scoregame_over = True# 接到小球的判定结果if ban_x <= ball_x <= ban_x + ban_width and ban_y <= ball_y <= ban_y + ban_height:score = score + 1speed = speed + 1# hit.play()ball_x = random.randint(0, 600)ball_y = 0ziti(screen, '分数:%d' % score, 0, 0)ziti(screen, "生命值:%d" % hp, screen_w - 120, 0)figure(screen, ball_figure, ball_x, ball_y)figure(screen,ban,ban_x, ban_y)#pygame.draw.rect(screen, (100, 255, 0), (ban_x, ban_y, ban_width, ban_height), 0)pygame.display.update()  # 3.不断循环def main():pygame.init()  # 1.游戏初始化# pygame.mixer.init()  # 2.音乐初始化run()if __name__ == '__main__':main()

Boren-python接小球游戏相关推荐

  1. Python实现小球游戏

    用python实现小球的游戏,小球不断运动,用挡板接住小球使小球一直运动即可得分,参照着输上的介绍写的,很简易的游戏,只需要导入sys 和 pygame模块. 效果图如下所示: # -*- codin ...

  2. Python的Pygame游戏框架(汇总)

    Python的Pygame游戏框架 一.前言 二.初识pygame 1.安装pygame 2.pygame 常用模块 二.Pygame 的基础使用 三.手写小游戏源码下载 一.前言 个人主页: ζ小菜 ...

  3. 用Python写简单游戏的一般步骤

    用Python写简单游戏的一般步骤 前言   自己用python写过几个简单的游戏后,发现对于已经写过的这些游戏而言,其创作流程都有一些相通性,自此小结一下用python写游戏的一般步骤. pytho ...

  4. 《Python和Pygame游戏开发指南》——2.16 pygame.display.update()函数

    本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第2章,第2.16节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...

  5. 《Python和Pygame游戏开发指南》——1.12 图书中的文本折行

    本节书摘来自异步社区<Python和Pygame游戏开发指南>一书中的第1章,第1.12节,作者[美]Al Sweigart(斯维加特), 李强 译,更多章节内容可以访问云栖社区" ...

  6. python猜数字游戏简单-python猜数字游戏快速求解解决方案

    python猜数字游戏快速求解解决方案.使用方法: 1. 保存代码为guessall.py 2. 执行python guessall.py > result.txt 3. 打开result.tx ...

  7. python手机版做小游戏代码大全-python简单小游戏代码 怎么用Python制作简单小游戏...

    1.Python猜拳小游戏代码: 2.import random #导入随机模块 3. 4.num = 1 5.yin_num = 0 6.shu_num = 0 7.while num <= ...

  8. python编程小游戏-python编程游戏有哪些

    python编程游戏有哪些?下面给大家介绍几款由Python开发的游戏: 1.Github上面有个项目Free Python Games,里面集合了不少的Python开发的小游戏,能玩,也适合新手用来 ...

  9. python能做游戏吗-没有Python不能做的游戏,这些游戏都可以做

    简介:Python编程语言的强大,几乎是众所周知的!那么,下面我给大家介绍一下几个用Python实现的各种游戏吧.不仅能用来做web.爬虫.数据分析等,没想到还能用做这么多的游戏,实在令人惊讶不已.注 ...

  10. python小游戏代码大全-python贪吃蛇游戏代码

    本文实例为大家分享了python贪吃蛇游戏的具体代码,供大家参考,具体内容如下 贪吃蛇游戏截图: 首先安装pygame,可以使用pip安装pygame: pip install pygame 运行以下 ...

最新文章

  1. 南京金陵中学2021高考成绩查询,2021高考成绩出炉 南京各大高中喜报来了!
  2. java 伪异步 netty,Netty(一) - 不死的达芬奇的个人空间 - OSCHINA - 中文开源技术交流社区...
  3. NYOJ 73 比大小
  4. (三)html5的结构
  5. LevelDB源码解读
  6. .Netcore 2.0 Ocelot Api网关教程(番外篇)- Ocelot v13.x升级
  7. python实时显示温度变化_python实时温度绘制
  8. UIScollView Touch事件
  9. SSL/TLS 工具 OpenSSL
  10. mysql binlog 增量备份
  11. 首个在线教学中文预训练模型TAL-EduBERT
  12. java web 实战开发经典_java web 开发实战经典(一)
  13. 简单实现DButil工具类
  14. IDC发布最新中国AI云服务市场报告,百度智能云排名第一
  15. ps裁剪和裁切的区别_PS详细工具讲解剪切,裁剪的区别
  16. android开发出现错误:Failed to find configured root that contains
  17. 《UnityAPI.GUI界面》(Yanlz+Unity+SteamVR+云技术+5G+AI+VR云游戏+GUI+DrawTexture+FocusControl+Toggle+立钻哥哥++OK++)
  18. linux中的apt命令
  19. 基于51单片机的智能停车场车位收费系统原理图PCB
  20. Android使用SQLiteOpenHelper实现离线浏览

热门文章

  1. PPPOE拨号下MTU设置
  2. 老照片:一个澳洲人在大清国的徒步旅行
  3. CSS+DIV布局中absolute和relative区别
  4. Minecraft 材质包制作入门 保姆级教程 (一)
  5. CC13X0CC26X0EvaluationBoard
  6. python订餐系统_用python编写的公司订餐系统的审核脚本
  7. rhcsa第二天作业9道题
  8. html让登录框3d,CSS3实现3D旋转动态显示登录注册
  9. 用opencv将左右眼视角图片转换为红蓝3D图片
  10. 127.0.0.1 192.168 localhost 之间的区别