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实现矩阵相乘的三种方法小结
Jul 26 Python
Pandas之groupby( )用法笔记小结
Jul 23 Python
Python实现二叉树的最小深度的两种方法
Sep 30 Python
python发qq消息轰炸虐狗好友思路详解(完整代码)
Feb 15 Python
Python获取对象属性的几种方式小结
Mar 12 Python
Python pexpect模块及shell脚本except原理解析
Aug 03 Python
了解一下python内建模块collections
Sep 07 Python
python 密码学示例——凯撒密码的实现
Sep 21 Python
python 实现图片修复(可用于去水印)
Nov 19 Python
PyCharm Ctrl+Shift+F 失灵的简单有效解决操作
Jan 15 Python
python 制作网站筛选工具(附源码)
Jan 21 Python
python实现求纯色彩图像的边框
Apr 08 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网页游戏学习之Xnova(ogame)源码解读(一)
2014/06/23 PHP
跟我学Laravel之安装Laravel
2014/10/15 PHP
WordPress中编写自定义存储字段的相关PHP函数解析
2015/12/25 PHP
javascript 继承实现方法
2009/08/26 Javascript
基于MooTools的很有创意的滚动条时钟动画
2010/11/14 Javascript
js 判断上传文件大小及格式代码
2013/11/13 Javascript
JQuery右键菜单插件ContextMenu使用指南
2014/12/19 Javascript
jquery实现鼠标滑过小图时显示大图的方法
2015/01/14 Javascript
使用jQuery实现input数值增量和减量的方法
2015/01/24 Javascript
jQuery密码强度检测插件passwordStrength用法实例分析
2015/10/30 Javascript
require.js+vue开发微信上传图片组件
2016/10/27 Javascript
vue+axios实现登录拦截的实例代码
2017/05/22 Javascript
AngularJS中使用ngModal模态框实例
2017/05/27 Javascript
Angular.JS中指令ng-if的注意事项小结
2017/06/21 Javascript
JavaScript 对引擎、运行时、调用堆栈的概述理解
2018/10/22 Javascript
javascript实现简易聊天室
2019/07/12 Javascript
vue学习之Vue-Router用法实例分析
2020/01/06 Javascript
[01:46]TI4西雅图DOTA2前线报道 中国选手抱团调时差
2014/07/08 DOTA
python 获取当天凌晨零点的时间戳方法
2018/05/22 Python
对Python 3.2 迭代器的next函数实例讲解
2018/10/18 Python
使用Python的toolz库开始函数式编程的方法
2018/11/15 Python
5分钟 Pipenv 上手指南
2018/12/20 Python
python matplotlib画盒图、子图解决坐标轴标签重叠的问题
2020/01/19 Python
python selenium操作cookie的实现
2020/03/18 Python
英国轻奢珠宝品牌:Astley Clarke
2016/12/18 全球购物
工程地质勘察专业大学生求职信
2013/10/13 职场文书
区域销售主管岗位职责
2014/06/15 职场文书
奶茶店创业计划书
2014/08/14 职场文书
亲属关系公证书样本
2015/01/23 职场文书
寻衅滋事罪辩护词
2015/05/21 职场文书
故意杀人案辩护词
2015/05/21 职场文书
鲁滨逊漂流记读书笔记
2015/06/26 职场文书
运动会班级前导词
2015/07/20 职场文书
2016年质量月活动总结报告
2016/04/05 职场文书
html5表单的required属性使用
2021/07/07 HTML / CSS
Appium中scroll和drag_and_drop根据元素位置滑动
2022/02/15 Python