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 相关文章推荐
python3.3实现乘法表示例
Feb 07 Python
用Python实现web端用户登录和注册功能的教程
Apr 30 Python
Python通过RabbitMQ服务器实现交换机功能的实例教程
Jun 29 Python
一百多行python代码实现抢票助手
Sep 25 Python
python批量识别图片指定区域文字内容
Apr 30 Python
Python利用scapy实现ARP欺骗的方法
Jul 23 Python
Python 继承,重写,super()调用父类方法操作示例
Sep 29 Python
python 回溯法模板详解
Feb 26 Python
浅谈tensorflow中dataset.shuffle和dataset.batch dataset.repeat注意点
Jun 08 Python
python使用Word2Vec进行情感分析解析
Jul 31 Python
通过代码简单了解django model序列化作用
Nov 12 Python
Django缓存Cache使用详解
Nov 30 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
WML,Apache,和 PHP 的介绍
2006/10/09 PHP
php检查页面是否被百度收录
2015/10/28 PHP
CI映射(加载)数据到view层的方法
2016/03/28 PHP
简单实用的PHP文本缓存类实例
2019/03/22 PHP
Javascript 篱式条件判断
2008/08/22 Javascript
jQuery版Tab标签切换
2011/03/16 Javascript
Javascript中查找不以XX字符结尾的单词示例代码
2013/10/15 Javascript
浅谈js的setInterval事件
2014/12/05 Javascript
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
jquery仿百度经验滑动切换浏览效果
2015/04/14 Javascript
JavaScript对表格或元素按文本,数字或日期排序的方法
2015/05/26 Javascript
深入理解JavaScript的React框架的原理
2015/07/02 Javascript
详解Wondows下Node.js使用MongoDB的环境配置
2016/03/01 Javascript
JavaScript代码性能优化总结篇
2016/05/15 Javascript
浅析JS中对函数function的理解(基础篇)
2016/10/14 Javascript
微信小程序中顶部导航栏的实现代码
2017/03/30 Javascript
详解vue-router2.0动态路由获取参数
2017/06/14 Javascript
js原生实现移动端手指滑动轮播图效果的示例
2018/01/02 Javascript
VUE2 前端实现 静态二级省市联动选择select的示例
2018/02/09 Javascript
vue.js中npm安装教程图解
2018/04/10 Javascript
jQuery实现轮播图及其原理详解
2020/04/12 jQuery
微信小程序登录按钮遮罩浮层效果的实现方法
2018/12/16 Javascript
4 种滚动吸顶实现方式的比较
2019/04/09 Javascript
vue实现路由切换改变title功能
2019/05/28 Javascript
python实现简单聊天应用 python群聊和点对点均实现
2017/09/14 Python
python中sys.argv函数精简概括
2018/07/08 Python
Python预测2020高考分数和录取情况
2020/07/08 Python
结合CSS3的新特性来总结垂直居中的实现方法
2016/05/30 HTML / CSS
html5 Canvas画图教程(9)—canvas中画出矩形和圆形
2013/01/09 HTML / CSS
KIEHL’S科颜氏官方旗舰店:源自美国的顶级护肤品牌
2018/06/07 全球购物
Interflora澳大利亚:同日鲜花速递
2019/06/25 全球购物
高级Java程序员面试题
2016/06/23 面试题
电子商务毕业生求职信
2013/11/10 职场文书
党的群众路线教育实践活动个人整改方案
2014/09/21 职场文书
2014年文明创建工作总结
2014/11/25 职场文书
2015年度环卫处工作总结
2015/07/24 职场文书