|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小章郎 于 2019-1-3 11:43 编辑
代码如下:
- import pygame as pg
- import sys
- from pygame.locals import *
- from random import *
- class Ball(pg.sprite.Sprite):
- def __init__(self, gray_ball, color_ball, position, speed, bg_size, target):
- # Call the parent class (Sprite) constructor
- pg.sprite.Sprite.__init__(self)
- self.gray_ball = pg.image.load(gray_ball).convert_alpha()
- self.color_ball = pg.image.load(color_ball).convert_alpha()
- self.rect = self.gray_ball.get_rect()
- # 小球的位置
- self.rect.left, self.rect.top = position
- self.speed = speed
- self.side = [choice([-1, 1]), choice([-1, 1])]
- self.width, self.height = bg_size[0], bg_size[1]
- self.target = target
- self.control = False
- self.collide = False
- def move(self):
- if self.control:
- self.rect = self.rect.move(self.speed)
- else:
- self.rect = self.rect.move([self.side[0] * self.speed[0], \
- self.side[1] * self.speed[1]])
- # 类似实现贪吃蛇穿入墙壁从对面墙壁出来(左右方向)
- if self.rect.right < 0:
- self.rect.left = self.width
- elif self.rect.left > self.width:
- self.rect.right = 0
- # (上下方向) 从下往上 和 从上往下
- elif self.rect.bottom < 0:
- self.rect.top = self.height
- elif self.rect.top > self.height:
- self.rect.bottom = 0
- def check(self, motion):
- if self.target < motion < self.target + 5:
- return True
- else:
- return False
- class Glass(pg.sprite.Sprite):
- def __init__(self, glass_image, mouse_image, bg_size):
- #初始化动画精灵
- pg.sprite.Sprite.__init__(self)
- self.glass_image = pg.image.load(glass_image).convert()
- self.glass_image.set_alpha(100)
- self.glass_rect = self.glass_image.get_rect()
- self.glass_rect.left, self.glass_rect.top = \
- (bg_size[0] - self.glass_rect.width) // 2, \
- bg_size[1] - self.glass_rect.height
- self.mouse_image = pg.image.load(mouse_image).convert_alpha()
- self.mouse_rect = self.mouse_image.get_rect()
- self.mouse_rect.left, self.mouse_rect.top = \
- self.glass_rect.left, self.glass_rect.top
- #隐藏鼠标实际位置
- pg.mouse.set_visible(False)
- def main():
- pg.init()
- gray_ball = 'gray_ball.png'
- color_ball = 'color_ball.png'
- glass_image = 'glass.jpg'
- bg_image = 'background.png'
- mouse_image = 'hand.png'
- #添加背景音乐
- pg.mixer.music.load('bg_music.ogg')
- pg.mixer.music.play()
- pg.mixer.music.set_volume(0.5)
- #添加音效
- loser_sound = pg.mixer.Sound('loser.wav')
- laugh_sound = pg.mixer.Sound('laugh.wav')
- winner_sound = pg.mixer.Sound('winner.wav')
- hole_sound = pg.mixer.Sound('hole.wav')
- running = True
- #音乐播放完结束游戏,添加用户事件,多个事件,可对USEREVENT进行加一
- GAMEOVER = USEREVENT
- pg.mixer.music.set_endevent(GAMEOVER)
- #根据背景图片指定设置游戏界面尺寸
- bg_size = width, height = 1024, 681
- screen = pg.display.set_mode(bg_size)
- pg.display.set_caption('Play the ball')
- background = pg.image.load(bg_image).convert_alpha()
- hole = [(117, 119, 199, 201), (225, 227, 390, 392), \
- (503, 505, 320, 322), (698, 700, 192, 194), \
- (906, 908, 419, 421)]
- msgs = []
- #用来存放小球的列表
- balls = []
- group = pg.sprite.Group()
- #创建五个小球
- for i in range(5):
- # 球的尺寸是100*100 随机产生小球的位置
- position = randint(0, width - 100), randint(0, height - 100)
- # 两个元素的一个列表,表示x轴和y轴方向的速度
- speed = [randint(1, 10), randint(1, 10)]
- # 实例化小球对象 分别传入Surface对象 位置二元组 速度两元素列表
- ball = Ball(gray_ball, color_ball, position, speed, bg_size, i*(i+1))
- # 碰撞检测之后不从组里面删除
- while pg.sprite.spritecollide(ball, group, False, None):#这个能完美检测碰撞
- #while pg.sprite.spritecollide(ball, group, False, pg.sprite.collide_circle):
- ball.rect.left, ball.rect.top = randint(0, width - 100), \
- randint(0, height - 100)
- balls.append(ball) # 将小球加入到小球列表中
- group.add(ball)
- glass = Glass(glass_image, mouse_image, bg_size)
- #motion变量用于记录鼠标每秒事件数量
- motion = 0
- #设置计时器,计算一秒
- MYTIMER = USEREVENT + 1
- pg.time.set_timer(MYTIMER, 1000)
- #设置重复发送按键事件,按住重复发送,set_rpeat(delay, interval)delay指定第一次发送事件的延迟时间,
- #interval指定重复发事件的时间间隔,如果不带任何参数表示取消重复发送事件
- pg.key.set_repeat(100, 100 )
- clock = pg.time.Clock()
- while running:
- for event in pg.event.get():
- if event.type == QUIT:
- sys.exit()
- elif event.type == GAMEOVER:
- loser_sound.play()
- pg.time.delay(2000)
- laugh_sound.play()
- running = False
- elif event.type == MYTIMER:
- if motion:
- for each in group:
- if each.check(motion):
- each.speed = [0, 0]
- each.control = True
- motion = 0
- elif event.type == MOUSEMOTION:
- motion += 1
- elif event.type == KEYDOWN:
- if event.key == K_w:
- for each in group:
- if each.control:
- each.speed[1] -= 1
- if event.key == K_s:
- for each in group:
- if each.control:
- each.speed[1] += 1
- if event.key == K_a:
- for each in group:
- if each.control:
- each.speed[0] -= 1
- if event.key == K_d:
- for each in group:
- if each.control:
- each.speed[0] += 1
-
- #检测是否入洞
- if event.key == K_SPACE:
- for each in group:
- if each.control:
- for each_hole in hole:
- if each_hole[0] <= each.rect.left <= each_hole[1] and \
- each_hole[2] <= each.rect.top <= each_hole[3]:
- each.speed = [0, 0]
- group.remove(each)
- temp = balls.pop(balls.index(each))
- balls.insert(0, temp)
- hole.remove(each_hole)
-
- #所有的洞填满游戏胜利
- if not hole:
- pg.mixer.music.stop()
- winner_sound.play()
- pg.time.delay(3000)
- msg = pg.image.load('win.png').convert_alpha()
- msg.pos = (width - msg.get_width()) // 2, \
- (height - msg.get_height()) // 2
- msgs.append((msg, msg.pos))
- laugh_sound.play()
- screen.blit(background, (0, 0))
- screen.blit(glass.glass_image, glass.glass_rect)
- #控制鼠标光标不离开玻璃面板
- glass.mouse_rect.left, glass.mouse_rect.top = pg.mouse.get_pos()
- if glass.mouse_rect.left < glass.glass_rect.left:
- glass.mouse_rect.left = glass.glass_rect.left
- if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
- glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
- if glass.mouse_rect.top < glass.glass_rect.top:
- glass.mouse_rect.top = glass.glass_rect.top
- if glass.mouse_rect.bottom > glass.glass_rect.bottom - glass.mouse_rect.height:
- glass.mouse_rect.bottom = glass.glass_rect.bottom - glass.mouse_rect.height
- screen.blit(glass.mouse_image, glass.mouse_rect)
- for each in balls:
- each.move()
- if each.collide:
- each.speed = [randint(1, 10), randint(1, 10)]
- each.collide = False
- if each.control:
- screen.blit(each.color_ball, each.rect)
- else:
- screen.blit(each.gray_ball, each.rect)
- for each in group:
- #检查与其他是否碰撞需要去掉自身
- group.remove(each)
- if pg.sprite.spritecollide(each, group, False, None):#这个能完美检测碰撞
- #if pg.sprite.spritecollide(each, group, False, pg.sprite.collide_circle):
- each.side[0] = -each.side[0]
- each.side[1] = -each.side[1]
- each.collide = True
- if each.control:
- each.side[0] = -1
- each.side[1] = -1
- each.control = False
- #检测碰撞后再次添加回去
- group.add(each)
- for msg in msgs:
- screen.blit(msg[0], msg[1])
- pg.display.flip()
- clock.tick(30)
- if __name__ == '__main__':
- main()
复制代码
为了检测碰撞,用了如下代码
pg.sprite.spritecollide(ball, group, False, None)
pg.sprite.spritecollide(each, group, False, None)
上面两段能正常检测碰撞
可是视频里面用的如下两段
pg.sprite.spritecollide(ball, group, False, pg.sprite.collide_circle)
pg.sprite.spritecollide(each, group, False, pg.sprite.collide_circle)
却远远的没碰到就弹开了,我看小甲鱼的视频里不会啊,能完美检测碰撞,就是上面这样写的,是我上面地方写错了吗?
后边加circle是能完美检测圆形的,默认的是矩形检测不能完美检测碰撞。还有你的素材最好使用小甲鱼的原版
|
|