pygame游戏之旅 添加icon和bgm音效的方法


Posted in Python onNovember 21, 2018

本文为大家分享了pygame游戏之旅的第14篇,供大家参考,具体内容如下

添加icon需要用的函数是:

gameIcon = pygame.image.load("carIcon.png")
pygame.display.set_icon(gameIcon)

添加bgm和音效的函数是:

crash_sound = pygame.mixer.Sound("crashed.wav")
pygame.mixer.music.load("bgm.wav")

源码:

import pygame
import time
import random
 
pygame.init()
 
crash_sound = pygame.mixer.Sound("crashed.wav")
pygame.mixer.music.load("bgm.wav")
 
 
white = (255,255,255)
black = (0,0,0)
gray = (128,128,128)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
blue = (0,0,255)
 
 
car_width = 100
 
display_width = 800
display_height = 600
 
 
gameDisplay = pygame.display.set_mode( (display_width,display_height) )
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
 
carImg = pygame.image.load('car.png')
gameIcon = pygame.image.load("carIcon.png")
pygame.display.set_icon(gameIcon)
 
 
pause = False
##crash = True
 
def things_dodged(count):
 font = pygame.font.SysFont(None, 25)
 text = font.render("Dodged:"+str(count), True, black)
 gameDisplay.blit(text,(0,0))
 
def things(thingx, thingy, thingw, thingh, color):
 pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
 
 
 
def car(x, y):
 gameDisplay.blit(carImg, (x,y))
 
def text_objects(text, font):
 textSurface = font.render(text, True, black)
 return textSurface, textSurface.get_rect()
 
##def message_diaplay(text):
## largeText = pygame.font.Font('freesansbold.ttf',115)
## TextSurf, TextRect = text_objects(text, largeText)
## TextRect.center = ((display_width/2),(display_height/2))
## gameDisplay.blit(TextSurf, TextRect)
## pygame.display.update()
## time.sleep(2)
## game_loop()
 
 
 
def crash():
 pygame.mixer.music.stop()
 pygame.mixer.Sound.play(crash_sound)
 
 largeText = pygame.font.SysFont('comicsansms',115)
 TextSurf, TextRect = text_objects('You Crashed!', largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 
 while True:
  for event in pygame.event.get():
   print(event)
   if event.type == pygame.QUIT:
    pygame.quit()
    quit()
##  gameDisplay.fill(white)
  button("Play Again", 150, 450, 100, 50, green, bright_green,game_loop)
  button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  pygame.display.update()
  clock.tick(15)
 
def button (msg, x, y, w, h, ic, ac, action=None):
 mouse =pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
## print(click)
 if x + w > mouse[0] > x and y + h > mouse[1] > y:
  pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
  if click[0] == 1 and action != None:
   action()
##    if action == "play":
##     action()
##    if action == "quit":
##     pygame.quit()
##     quit()
 else:
  pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
 smallText = pygame.font.SysFont('comicsansms', 20)
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ( (x+(w/2)), (y+(h/2)))
 gameDisplay.blit(textSurf, textRect)
 
def quitgame():
 pygame.quit()
 quit()
def unpause():
 pygame.mixer.music.unpause()
 global pause
 pause = False
 
def paused():
 pygame.mixer.music.pause()
 
 largeText = pygame.font.SysFont('comicsansms',115)
 TextSurf, TextRect = text_objects('Paused', largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 
 while pause:
  for event in pygame.event.get():
   print(event)
   if event.type == pygame.QUIT:
    pygame.quit()
    quit()
##  gameDisplay.fill(white)
  button("Continue", 150, 450, 100, 50, green, bright_green,unpause)
  button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  pygame.display.update()
  clock.tick(15)
 
 
def game_intro():
 global pasue
 pause = False
 intro = True
 while intro:
  for event in pygame.event.get():
   print(event)
   if event.type == pygame.QUIT:
    pygame.quit()
    quit()
  gameDisplay.fill(white)
  largeText = pygame.font.SysFont('comicsansms',115)
  TextSurf, TextRect = text_objects('A bit Racey', largeText)
  TextRect.center = ((display_width/2),(display_height/2))
  gameDisplay.blit(TextSurf, TextRect)
  button("GO", 150, 450, 100, 50, green, bright_green,game_loop)
  button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
  pygame.display.update()
  clock.tick(15)
 
def game_loop():
 global pause
 pygame.mixer.music.play(-1)
 x = display_width * 0.45
 y = display_height * 0.8
 x_change = 0
 
 dodged = 0
 
 gameExit = False
 
 thing_startx = random.randrange(0, display_width)
 thing_starty = -600
 thing_speed = 7
 thing_width = 100
 thing_height = 100
 
 while not gameExit:
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    pygame.quit()
    quit()
   if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
     x_change = -5
    elif event.key == pygame.K_RIGHT:
     x_change = 5
    elif event.key == pygame.K_p:
     pause = True
     paused()
   if event.type == pygame.KEYUP:
    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
     x_change = 0
   print(event)
  x += x_change
  gameDisplay.fill(white)
 
  things(thing_startx, thing_starty, thing_width, thing_height, black)
  thing_starty += thing_speed
  
  car(x,y)
  things_dodged(dodged)
  if x > display_width - car_width or x < 0:
   gameExit = True
  if thing_starty > display_height:
   thing_starty = 0 - thing_height
   thing_startx = random.randrange(0, display_width)
   dodged += 1
   thing_speed += 1
   thing_width += (dodged * 1.2)
  if y < thing_starty + thing_height:
   print('y crossover')
   if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
    print('x crossover')
    crash()
  pygame.display.update()
  clock.tick(60)
#crash()
game_intro()
game_loop()
pygame.quit()
quit()

结果图:

pygame游戏之旅 添加icon和bgm音效的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python动态加载包的方法小结
Apr 18 Python
Python使用正则表达式实现文本替换的方法
Apr 18 Python
python脚本爬取字体文件的实现方法
Apr 29 Python
python中的闭包函数
Feb 09 Python
python使用生成器实现可迭代对象
Mar 20 Python
Python Flask 搭建微信小程序后台详解
May 06 Python
Django应用程序入口WSGIHandler源码解析
Aug 05 Python
Python numpy数组转置与轴变换
Nov 15 Python
python实现XML解析的方法解析
Nov 16 Python
python操作docx写入内容,并控制文本的字体颜色
Feb 13 Python
pygame面向对象的飞行小鸟实现(Flappy bird)
Apr 01 Python
Python WSGI 规范简介
Apr 11 Python
pygame游戏之旅 添加游戏暂停功能
Nov 21 #Python
使用50行Python代码从零开始实现一个AI平衡小游戏
Nov 21 #Python
pygame游戏之旅 调用按钮实现游戏开始功能
Nov 21 #Python
pygame游戏之旅 按钮上添加文字的方法
Nov 21 #Python
Face++ API实现手势识别系统设计
Nov 21 #Python
详解django自定义中间件处理
Nov 21 #Python
pygame游戏之旅 添加游戏界面按键图形
Nov 20 #Python
You might like
用js进行url编码后用php反解以及用php实现js的escape功能函数总结
2010/02/08 PHP
基于Linux调试工具strace与gdb的常用命令总结
2013/06/03 PHP
深入理解php的MySQL连接类
2013/06/07 PHP
使用配置类定义Codeigniter全局变量
2014/06/12 PHP
PHP通过反射动态加载第三方类和获得类源码的实例
2015/11/27 PHP
php简单压缩css样式示例
2016/09/22 PHP
thinkPHP5.0框架开发规范简介
2017/03/25 PHP
浅谈PHP中类和对象的相关函数
2017/04/26 PHP
laravel框架创建授权策略实例分析
2019/11/22 PHP
CL vs ForZe BO5 第五场 2.13
2021/03/10 DOTA
javascript实现划词标记+划词搜索功能
2007/03/06 Javascript
JSON 学习之JSON in JavaScript详细使用说明
2010/02/23 Javascript
jquery向上向下取整适合分页查询
2014/09/06 Javascript
jquery实现弹出层登录和全屏层注册特效
2015/08/28 Javascript
JS实现鼠标滑过折叠与展开菜单效果代码
2015/09/06 Javascript
jQuery判断浏览器并动态调整select宽度的方法
2016/03/02 Javascript
JS刷新父窗口的几种方式小结(推荐)
2016/11/09 Javascript
JQuery ZTree使用方法详解
2017/01/07 Javascript
微信小程序实现倒计时调用相机自动拍照功能
2018/06/10 Javascript
详解Vue依赖收集引发的问题
2019/04/22 Javascript
python语音识别实践之百度语音API
2018/08/30 Python
详解python 3.6 安装json 模块(simplejson)
2019/04/02 Python
python爬虫爬取笔趣网小说网站过程图解
2019/11/18 Python
Python读取分割压缩TXT文本文件实例
2020/02/14 Python
python GUI库图形界面开发之PyQt5单行文本框控件QLineEdit详细使用方法与实例
2020/02/27 Python
Python实现aes加密解密多种方法解析
2020/05/15 Python
德国最大的婴儿用品网上商店:Kidsroom.de(支持中文)
2020/09/02 全球购物
.NET面试问题集
2015/12/08 面试题
社区庆中秋节活动方案
2014/02/07 职场文书
国际商贸专业自荐信
2014/06/09 职场文书
护士实习求职信
2014/06/22 职场文书
签约仪式致辞
2015/07/30 职场文书
应收账款管理制度
2015/08/06 职场文书
小学教代会开幕词
2016/03/04 职场文书
《没有任何借口》读后感:完美的执行能力
2020/01/07 职场文书
Django migrate报错的解决方案
2021/05/20 Python