P90 为啥 我的小球停在黑洞上没反应
import pygameimport sys
import math
from pygame.locals import *
from random import *
#球类继承自Sprirte类
class Ball(pygame.sprite.Sprite):
def __init__(self,glayball_image,greenball_image,position,speed,bg_size,target):
#初始化动作精灵
pygame.sprite.Sprite.__init__(self)
self.glayball_image = pygame.image.load(glayball_image).convert_alpha()
self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
self.rect = self.glayball_image.get_rect()
#将小球放在指定位置
self.rect.left,self.rect.top = position
self.side = ),choice([-1,1])]
self.speed = speed
self.collide = False
self.target = target
self.control = False
self.width,self.height = bg_size,bg_size
self.radius = self.rect.width /2
def move(self):
if self.control:
self.rect = self.rect.move(self.speed)
else:
self.rect = self.rect.move(self.side * self.speed,\
self.side* self.speed)
#如果小球的左侧出了边界,那么将小球左侧改为右侧的边界
#这样便实现了左边进入,右边出来的效果
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(pygame.sprite.Sprite):
def __init__(self,glass_image,mouse_image,bg_size):
#初始化动作精灵
pygame.sprite.Sprite.__init__(self)
self.glass_image = pygame.image.load(glass_image).convert_alpha()
self.glass_rect = self.glass_image.get_rect()
self.glass_rect.left,self.glass_rect.top = \
(bg_size - self.glass_rect.width)//2,\
bg_size- self.glass_rect.height
self.mouse_image = pygame.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
pygame.mouse.set_visible(False)
def main():
pygame.init()
glayball_image = 'ball.png'
greenball_image='greenball.png'
glass_image = 'boli.png'
mouse_image = 'sz.png'
bg_image = 'bg2.png'
running = True
#添加背景音乐
pygame.mixer.music.load('1.mp3')
pygame.mixer.music.play()
#添加音效
loser_sound = pygame.mixer.Sound('1.wav')
laugh_sound = pygame.mixer.Sound('2.wav')
winner_sound = pygame.mixer.Sound('3.wav')
hole_sound = pygame.mixer.Sound('4.wav')
#音乐播放完游戏结束
GAMEOVER = USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER)
#根据背景图片指定游戏界面尺寸
bg_size = width,height = 1024,576
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('Play the ball - FishC Demo')
background = pygame.image.load(bg_image).convert_alpha()
hole = [(86,88,72,74),(86,88,376,378),(463,465,223,225),
(803,805,73,75),(805,807,375,377)]
msgs = []
#用来存放小球对象的列表
balls=[]
group = pygame.sprite.Group()
#创建五个小球
for i in range(5):
#位置随机,速度随机
position = randint(0,width-100),randint(0,height-100)
speed =
ball = Ball(glayball_image,greenball_image,position,speed,bg_size,5*(i+1))
while pygame.sprite.spritecollide(ball,group,False,pygame.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 = 0
MYTIMER = USEREVENT +1
pygame.time.set_timer(MYTIMER,1000)
pygame.key.set_repeat(100,100)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == GAMEOVER:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running = False
elif event.type == MYTIMER:
if motion:
for each in group:
if each.check(motion):
each.speed =
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
if event.key == K_s:
for each in group:
if each.control:
each.speed += 1
if event.key == K_a:
for each in group:
if each.control:
each.speed -= 1
if event.key == K_d:
for each in group:
if each.control:
each.speed += 1
if event.key == K_SPACE:
for each in group:
if each.control:
for i in hole:
if i <= each.rect.left <= i and \
i <= each.rect.top <= i:
hole_sound.play
each.speed =
group.remove(each)
temp = balls.pop(balls.index(each))
balls.insert(0,temp)
hole.remove(i)
if not hole:
pygame.mixer.music.stop()
winner_sound.play
pygame.time.delay(3000)
msg = pygame.image.load('sl.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 = pygame.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.top > glass.glass_rect.bottom- glass.mouse_rect.height:
glass.mouse_rect.top = 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 =
each.collide = False
if each.control:
screen.blit(each.greenball_image,each.rect)
else:
screen.blit(each.glayball_image,each.rect)
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
each.side = -each.side
each.side = -each.side
each.collide = True
if each.control:
each.side = -1
each.side = -1
each.control = False
group.add(each)
for msg in msgs:
screen,blit(msg,msg)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
Traceback (most recent call last):
File "C:\Users\john\Desktop\python练习\p90.py", line 250, in <module>
main()
File "C:\Users\john\Desktop\python练习\p90.py", line 183, in main
i <= each.rect.top <= i:
IndexError: tuple index out of range 如果你的素材是和小甲鱼课堂上的那份一样的话,那就是五个洞的坐标完全不对了。至于别的问题有没有,先看看是不是坐标吧。 Hoiste 发表于 2020-4-28 10:52
如果你的素材是和小甲鱼课堂上的那份一样的话,那就是五个洞的坐标完全不对了。至于别的问题有没有,先看看 ...
自己做的图,忘记敲回车了,敲了回车就对了,但是入洞没有播放音效 CD84380973 发表于 2020-4-28 10:58
自己做的图,忘记敲回车了,敲了回车就对了,但是入洞没有播放音效
不如说敲回车就对了反而让人没发理解啊。。。 Hoiste 发表于 2020-4-28 11:03
不如说敲回车就对了反而让人没发理解啊。。。
他这个游戏不是设置的把球移动到黑洞然后敲回车吗? 然后我敲了回车现在报错了
Traceback (most recent call last):
File "C:\Users\john\Desktop\python练习\p90.py", line 250, in <module>
main()
File "C:\Users\john\Desktop\python练习\p90.py", line 183, in main
i <= each.rect.top <= i:
IndexError: tuple index out of range Hoiste 发表于 2020-4-28 11:03
不如说敲回车就对了反而让人没发理解啊。。。
说错了不是回车是空格键 if event.key == K_SPACE:这里是响应空格键啊,我翻了代码前后就没看到响应回车的地方,所以我觉得奇怪啊。 Hoiste 发表于 2020-4-28 11:11
这里是响应空格键啊,我翻了代码前后就没看到响应回车的地方,所以我觉得奇怪啊。
恩恩 就是报错这个不知道怎么处理的
Traceback (most recent call last):
File "C:\Users\john\Desktop\python练习\p90.py", line 250, in <module>
main()
File "C:\Users\john\Desktop\python练习\p90.py", line 183, in main
i <= each.rect.top <= i:
IndexError: tuple index out of range CD84380973 发表于 2020-4-28 11:04
他这个游戏不是设置的把球移动到黑洞然后敲回车吗? 然后我敲了回车现在报错了
Traceback (most recen ...
这个试下是不是黑洞的列表问题,那里明显出现了换行没有加反斜杠,所以下一行的几个坐标元组没有正常缩进,不过也可能是没用代码格式的问题,所以看看是不是这个问题先。 Hoiste 发表于 2020-4-28 11:13
这个试下是不是黑洞的列表问题,那里明显出现了换行没有加反斜杠,所以下一行的几个坐标元组没有正常缩进 ...
黑洞列表没有换行是一行的,复制过来到这里自动换行了 Hoiste 发表于 2020-4-28 11:13
这个试下是不是黑洞的列表问题,那里明显出现了换行没有加反斜杠,所以下一行的几个坐标元组没有正常缩进 ...
发现了问题了,有个列表差1个坐标,谢谢 Hoiste 发表于 2020-4-28 11:13
这个试下是不是黑洞的列表问题,那里明显出现了换行没有加反斜杠,所以下一行的几个坐标元组没有正常缩进 ...
想问一下在哪里减慢初始球移动的速度呢 CD84380973 发表于 2020-4-28 11:31
想问一下在哪里减慢初始球移动的速度呢
如果是觉得初始创建出来的五个球的移动速度太快的问题的话就是修改创建时的这个位置:
for i in range(5):
#位置随机,速度随机
position = randint(0,width-100),randint(0,height-100)
speed =
把speed的随机区间弄小一点就可以了。 Hoiste 发表于 2020-4-28 11:37
如果是觉得初始创建出来的五个球的移动速度太快的问题的话就是修改创建时的这个位置:
for i in range( ...
感谢 你好,请问下小球和地图的图片可以在哪里找到,能给共享一下吗?谢谢
页:
[1]