pygame游戏之旅 添加icon和bgm音效的方法
作者:观月执白 发布时间:2022-02-10 19:25:31
标签:pygame,icon,bgm,音效
本文为大家分享了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()
结果图:
来源:https://blog.csdn.net/pianzang5201/article/details/78406567
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 按照惯例,年底的淘宝的确是到了“需要改版的时候”。这次新版的淘宝首页上线,乍看并没有多少夺人眼球的地方,但仔细揣摩其中的细节,还是发现了不少
- 在工作中出于某些原因,我们可能需要将变量保存下来,这样下次就可以直接去赋值而不用重新执行某些重复耗时的操作了,这里我们用到了Python的p
- PYTHON 操作 XML读取XML文件关于XML的介绍<data> 与 </data> 是一对标签的开始与结束&l
- MNIST 数据集介绍MNIST 包含 0~9 的手写数字, 共有 60000 个训练集和 10000 个测试集. 数据的格式为单通道 28
- js对文字进行编码涉及3个函数:escape,encodeURI,encodeURIComponent,相应3个解码函数:unescape,
- 这篇文章主要介绍了django自定义模板标签过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 很多开发人员在刚开始学Python 时,都考虑过像 c++ 那样来实现 singleton 模式,但后来会发现 c++ 是 c++,Pyth
- 最近在研究WEB布局,遇到<H1>标签了,<H1>标签很重要。在一般教程中都这么说,<H1>标签在同一页
- 在ASP.NET2.0通过SMTP的验证发送EMAIL ,代码如下:’Create a new MailMes
- 从MySQL 5.0 开始,支持了一个全新的SQL句法:PREPARE stmt_name FROM preparable_stmt;EXE
- 本文实例为大家分享了Python3定时发送邮件功能的具体代码,供大家参考,具体内容如下1、 导入模块import osimport date
- 站长用Python写了一个可以提取csv任一列的代码,欢迎使用。Github链接csv是Comma-Separated Values的缩写,
- ajax编程获取Google的PageRank3(PR值)及所在目录,想给你的站增加Google PR查询的功能吗?如果你不会就看看本文吧,
- 前言:线程是指进程内的一个执行单元,也是进程内的可调度实体.与进程的区别:(1) 地址空间:进程内的一个执行单元;进程至少有一个线程;它们共
- 也许你刚刚来到一家公司,他们希望进行一些“可用性”工作。你可能是一名UI设计师,业务分析师,或前端开发人员,一名产品经理,或者负责用户体验部
- 大多数做过网页设计的都知道“CSS框架”,而且肯定有很多设计师已经开始在作品中使用CSS框架。就像其他编程语言一样,CSS也可以把一些重复使
- 指定的代码页特性无效。 codepage属性:是指出网页的代码页 如果制作的网页脚本与WEB服务端的默认代码页不同,则必须指明代码页: 代码
- xml.etree.ElementTree可以通过支持的有限的XPath表达式来定位元素。语法ElementTree支持的语法如下:语法说明
- asp如何获知页面上的图象的实际尺寸大小?见下面的两个asp文件:<!--#include virtual="/i
- 人体的视觉系统是一个相当神奇的存在,对于下面的一串手写图像,可以毫不费力的识别出他们是504192,轻松到让人都忘记了其实这是一个复杂的工作