pygame 碰撞检测功能:
添加碰撞检测的代码如下:为了查看效果,加入了音乐的播放代码实现
from random import randint
import sys
import pygame
pygame.init()
width,height = 1000,500
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('学习小游戏')
#加载背景音乐
pygame.mixer.init()
pygame.mixer.music.load(r'H:\temp\bg.mp3')
pygame.mixer.music.play(-1)
#加载音效
music_dd = pygame.mixer.Sound(r'H:\temp\dd.mp3')
#创建精灵
class Gold(pygame.sprite.Sprite):
def __init__(self,image,pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) #加载精灵图片
self.image = pygame.transform.scale(self.image,(20,20)).convert() #改变图片大小
self.rect = self.image.get_rect() #获取图片矩形
self.rect.topleft = pos #设置图片位置
def update(self,speed):
self.speed = speed
self.rect.top += self.speed #向下移动 top一直增加
if self.rect.top >= height:
self.kill() # 超过了屏幕 删除
#gold_img = 'D:\\temp\\gold.png'
gold_img = r'H:\temp\sz.jpg'
#创建一个精灵 放到底部
class Hero(pygame.sprite.Sprite):
def __init__(self,image,pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect = self.image.get_rect() #获取图片矩形
self.rect.center = pos #设置图片位置
def move(self,speed,where):
self.speed = speed
if where == 'right' and self.rect.left <= (width - 40):
self.image = pygame.image.load(hero_img) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect.left += self.speed #向右移动 left一直增加
elif self.rect.left >= 0 and where == 'left':
self.image = pygame.image.load(gold_img) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect.left -= self.speed #向左移动 left一直减少
def draw(self,screen):
screen.blit(self.image,self.rect)
#hero_img = 'D:\\temp\\hero.jpg'
hero_img = r'H:\temp\dw.jpg'
#初始位置居中显示
hero = Hero(hero_img,(width/2,height-22))
#创建精灵组
group = pygame.sprite.Group()
#控制一下精灵出现的数量和频率
gold_count = 0
gold_frep = randint(10,20) #10到20帧出现一个
#创建时钟
clock = pygame.time.Clock()
while True:
#填充屏幕为白色
screen.fill((255,255,255))
clock.tick(40) #每秒刷新60帧
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
hero.move(5,'left')
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
hero.move(5,'right')
gold_count += 1
if gold_count > gold_frep:
gold_frep = randint(10,20) #10到20帧出现一个
#添加精灵到精灵组
gold = Gold(gold_img,(randint(0,width),0))
group.add(gold)
gold_count = 0 #恢复0
#碰撞检测
for gold_of_group in group:
if pygame.sprite.collide_mask(hero,gold_of_group):
group.remove(gold_of_group)
music_dd.play()
hero.draw(screen)
group.update(5) #更新位置
group.draw(screen) #放到屏幕上
pygame.display.update()
在窗口上绘制文字,比如倒计时,分数等
from random import randint
import sys
import pygame
pygame.init()
width,height = 1000,500
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('学习小游戏')
#加载背景音乐
pygame.mixer.init()
pygame.mixer.music.load(r'H:\temp\bg.mp3')
pygame.mixer.music.play(-1)
#加载音效
music_dd = pygame.mixer.Sound(r'H:\temp\dd.mp3')
#创建精灵
class Gold(pygame.sprite.Sprite):
def __init__(self,image,pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) #加载精灵图片
self.image = pygame.transform.scale(self.image,(20,20)).convert() #改变图片大小
self.rect = self.image.get_rect() #获取图片矩形
self.rect.topleft = pos #设置图片位置
def update(self,speed):
self.speed = speed
self.rect.top += self.speed #向下移动 top一直增加
if self.rect.top >= height:
self.kill() # 超过了屏幕 删除
#gold_img = 'D:\\temp\\gold.png'
gold_img = r'H:\temp\sz.jpg'
#创建一个精灵 放到底部
class Hero(pygame.sprite.Sprite):
def __init__(self,image,pos):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect = self.image.get_rect() #获取图片矩形
self.rect.center = pos #设置图片位置
def move(self,speed,where):
self.speed = speed
if where == 'right' and self.rect.left <= (width - 40):
self.image = pygame.image.load(hero_img) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect.left += self.speed #向右移动 left一直增加
elif self.rect.left >= 0 and where == 'left':
self.image = pygame.image.load(gold_img) #加载精灵图片
self.image = pygame.transform.scale(self.image,(40,40)).convert() #改变图片大小
self.rect.left -= self.speed #向左移动 left一直减少
def draw(self,screen):
screen.blit(self.image,self.rect)
#hero_img = 'D:\\temp\\hero.jpg'
hero_img = r'H:\temp\dw.jpg'
#初始位置居中显示
hero = Hero(hero_img,(width/2,height-22))
#创建精灵组
group = pygame.sprite.Group()
#控制一下精灵出现的数量和频率
gold_count = 0
gold_frep = randint(10,20) #10到20帧出现一个
#设置字体
my_font = pygame.font.SysFont(['方正粗黑宋简体','microsoftsansserif'],20)
score = 0 #初始得分为0
#创建时钟
clock = pygame.time.Clock()
while True:
#填充屏幕为白色
screen.fill((255,255,255))
clock.tick(40) #每秒刷新60帧
# --倒计时信息
countdown_text = 'count down: ' + str((90000 - pygame.time.get_ticks()) // 60000) + ":" + str((90000 - pygame.time.get_ticks()) // 1000 % 60).zfill(2)
countdown_text = my_font.render(countdown_text, True, (0, 0, 0))
countdown_rect = countdown_text.get_rect()
countdown_rect.topright = [950, 5]
screen.blit(countdown_text, countdown_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
hero.move(5,'left')
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
hero.move(5,'right')
gold_count += 1
if gold_count > gold_frep:
gold_frep = randint(10,20) #10到20帧出现一个
#添加精灵到精灵组
gold = Gold(gold_img,(randint(0,width),0))
group.add(gold)
gold_count = 0 #恢复0
#碰撞检测
for gold_of_group in group:
if pygame.sprite.collide_mask(hero,gold_of_group): #判断是否碰撞
group.remove(gold_of_group) #移除出组
music_dd.play() #播放音效
score+=1 #加分
#将分数显示到窗口中
score_text = 'score:{}'.format(score)
score_text = my_font.render(score_text,True,(0,0,0))
score_rect = score_text.get_rect()
score_rect.topleft = [5,5]
screen.blit(score_text,score_rect)
hero.draw(screen)
group.update(5) #更新位置
group.draw(screen) #放到屏幕上
# --判断游戏是否结束
if pygame.time.get_ticks() >= 90000:
pygame.time.wait(3000) #暂停三秒后退出
break
pygame.display.update()