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使用CMD模块更优雅的运行脚本
May 11 Python
Python中用altzone()方法处理时区的教程
May 22 Python
对python 矩阵转置transpose的实例讲解
Apr 17 Python
Python 变量类型详解
Oct 10 Python
浅析python的优势和不足之处
Nov 20 Python
python使用openCV遍历文件夹里所有视频文件并保存成图片
Jan 14 Python
浅谈pytorch、cuda、python的版本对齐问题
Jan 15 Python
django model object序列化实例
Mar 13 Python
使用python实现时间序列白噪声检验方式
Jun 03 Python
安装python3.7编译器后如何正确安装opnecv的方法详解
Jun 16 Python
浅谈python中的多态
Jun 15 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
PHP中的str_repeat函数在JavaScript中的实现
2013/09/16 PHP
PHP无限分类(树形类)
2013/09/28 PHP
phpexcel导入excel数据使用方法实例
2013/12/24 PHP
那些年我们错过的魔术方法(Magic Methods)
2014/01/14 PHP
Laravel5.0+框架邮件发送功能实现方法图文与实例详解
2019/04/23 PHP
JAVASCRIPT下判断IE与FF的比较简单的方式
2008/10/17 Javascript
setTimeout与setInterval在不同浏览器下的差异
2010/01/24 Javascript
JavaScript使用二分查找算法在数组中查找数据的方法
2015/04/07 Javascript
全面解析Javascript无限添加QQ好友原理
2016/06/15 Javascript
JavaScript中的Reflect对象详解(ES6新特性)
2016/07/22 Javascript
浅谈js继承的实现及公有、私有、静态方法的书写
2016/10/28 Javascript
validationEngine 表单验证插件使用实例代码
2017/06/15 Javascript
JS正则表达式完美实现身份证校验功能
2017/10/18 Javascript
详解keep-alive + vuex 让缓存的页面灵活起来
2019/04/19 Javascript
D3.js的基础部分之数组的处理数组的排序和求值(v3版本)
2019/05/09 Javascript
JS在Array数组中按指定位置删除或添加元素对象方法示例
2019/11/19 Javascript
Angular8 简单表单验证的实现示例
2020/06/03 Javascript
ssm+vue前后端分离框架整合实现(附源码)
2020/07/08 Javascript
vue 修改 data 数据问题并实时显示操作
2020/09/07 Javascript
Python中下划线的使用方法
2015/03/27 Python
python实现机械分词之逆向最大匹配算法代码示例
2017/12/13 Python
基于python中theano库的线性回归
2018/08/31 Python
python使用scrapy发送post请求的坑
2018/09/04 Python
Pycharm新建模板默认添加个人信息的实例
2019/07/15 Python
django框架forms组件用法实例详解
2019/12/10 Python
Python3 mmap内存映射文件示例解析
2020/03/23 Python
html5的pushstate以及监听浏览器返回事件的实现
2020/08/11 HTML / CSS
乌克兰数字设备、配件和智能技术的连锁商店:KTC
2020/08/18 全球购物
《盲人摸象》教学反思
2014/02/16 职场文书
餐厅总厨求职信
2014/03/04 职场文书
《庐山的云雾》教学反思
2014/04/22 职场文书
运动会加油稿50字
2015/07/21 职场文书
2017元旦晚会开幕词
2016/03/03 职场文书
如何在Python中创建二叉树
2021/03/30 Python
JS Object构造函数之Object.freeze
2021/04/28 Javascript
一文了解MySQL二级索引的查询过程
2022/02/24 MySQL