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之赋值,简单也不简单
Sep 24 Python
python如何实现excel数据添加到mongodb
Jul 30 Python
在Python中使用正则表达式的方法
Aug 13 Python
总结网络IO模型与select模型的Python实例讲解
Jun 27 Python
使用python生成目录树
Mar 29 Python
Python编程中NotImplementedError的使用方法
Apr 21 Python
基于python实现聊天室程序
Jul 27 Python
python实现连连看辅助之图像识别延伸
Jul 17 Python
Python3 JSON编码解码方法详解
Sep 06 Python
python调用Matplotlib绘制分布点图
Oct 18 Python
python两种注释用法的示例
Oct 09 Python
python实现学生信息管理系统(面向对象)
Jun 05 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微信开发之文本自动回复
2016/06/23 PHP
去掉gridPanel表头全选框的小例子
2013/07/18 Javascript
JS检测图片大小的实例
2013/08/21 Javascript
有关json_decode乱码及NULL的问题
2015/10/13 Javascript
jQuery Easyui Datagrid实现单行的上移下移及保存移动的结果
2016/08/15 Javascript
JavaScript 身份证号有效验证详解及实例代码
2016/10/20 Javascript
简单谈谈原生js的math对象
2017/06/27 Javascript
JavaScript变量声明var,let.const及区别浅析
2018/04/23 Javascript
Python中的推导式使用详解
2015/06/03 Python
python socket多线程通讯实例分析(聊天室)
2016/04/06 Python
Python使用matplotlib绘制正弦和余弦曲线的方法示例
2018/01/06 Python
Python3单行定义多个变量或赋值方法
2018/07/12 Python
Python Opencv任意形状目标检测并绘制框图
2019/07/23 Python
django-crontab实现服务端的定时任务的示例代码
2020/02/17 Python
浅谈Pytorch中的自动求导函数backward()所需参数的含义
2020/02/29 Python
给ubuntu18安装python3.7的详细教程
2020/06/08 Python
keras自定义损失函数并且模型加载的写法介绍
2020/06/15 Python
sublime3之内网安装python插件Anaconda的流程
2020/11/10 Python
python实现文件分片上传的接口自动化
2020/11/19 Python
python中round函数保留两位小数的方法
2020/12/04 Python
使用Python制作一盏 3D 花灯喜迎元宵佳节
2021/02/26 Python
科颜氏印度官网:Kiehl’s印度
2021/02/20 全球购物
实习医生自我评价
2013/09/22 职场文书
日语专业毕业生自荐信
2013/11/11 职场文书
工艺工程师工作职责
2013/11/23 职场文书
升职自荐书范文
2013/11/28 职场文书
公司道歉信范文
2014/01/09 职场文书
2014端午节活动策划方案
2014/01/27 职场文书
酒店保安领班职务说明书
2014/03/04 职场文书
村干部培训班主持词
2014/03/28 职场文书
出生公证委托书
2014/04/03 职场文书
会计电算化专业求职信
2014/06/10 职场文书
公共场所禁烟倡议书
2014/08/30 职场文书
爱心捐书倡议书
2015/04/27 职场文书
写作之关于描写老人的好段摘抄
2019/11/14 职场文书
JS轻量级函数式编程实现XDM三
2022/06/16 Javascript