鱼C论坛

 找回密码
 立即注册
查看: 2184|回复: 8

为啥抛出了异常?设置捕获了呀!

[复制链接]
发表于 2022-5-22 07:48:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 小凯2013 于 2022-5-22 07:52 编辑

前方高能预警!!!代码如下(250行左右):
  1. #p3_85.py/摩擦摩擦(main).py
  2. import pygame
  3. import sys
  4. import traceback
  5. from pygame.locals import *
  6. from random import *

  7. try:
  8.     class Ball(pygame.sprite.Sprite):
  9.         def __init__(self,grayball_image,greenball_image,position,speed,bg_size,target):
  10.             pygame.sprite.Sprite.__init__(self)
  11.             self.grayball_image = pygame.image.load(grayball_image).convert_alpha()
  12.             self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
  13.             self.rect = self.grayball_image.get_rect()
  14.             self.rect.left = position
  15.             self.rect.top = position
  16.             self.side = [choice([-1,1]),choice([-1,1])]
  17.             self.speed = speed
  18.             self.collide = False
  19.             self.target = target
  20.             self.control = False
  21.             self.width = bg_size[0]
  22.             self.height = bg_size[1]
  23.             self.radius = self.rect.width / 2

  24.         def move(self):
  25.             if self.control:
  26.                 self.rect = self.rect.move(self.speed)
  27.             else:
  28.                 self.rect = self.rect.move((self.side[0] * self.speed[0],self.side[1] * self.speed[1]))

  29.             if self.rect.right <= 0:
  30.                 self.rect.left = self.width

  31.             elif self.rect.left >= self.width:
  32.                 self.rect.right = 0

  33.             elif self.rect.bottom <= 0:
  34.                 self.rect.top = self.height

  35.             elif self.rect.top >= self.height:
  36.                 self.rect.bottom = 0

  37.         def check(self,motion):
  38.             if self.target < motion < self.target + 5:
  39.                 return True
  40.             else:
  41.                 return False

  42.     class Glass(pygame.sprite.Sprite):
  43.         def __init__(self,glass_image,mouse_image,bg_size):
  44.             pygame.sprite.Sprite.__init__(self)

  45.             self.glass_image = pygame.image.load(glass_image).convert_alpha()
  46.             self.glass_rect = self.glass_imagr.get_rect()
  47.             self.glass_rect.left = (bg_size[0] - self.glass_rect.width) // 2
  48.             self.glass_rect.top = bg_size[1] - self.glass_rect.height

  49.             self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
  50.             self.mouse_rect = self.mouse_image.get_rect()
  51.             self.mouse_rect.left = self.glass_rect.left
  52.             self.mouse_rect.top = self.glass_rect.top
  53.             pygame.mouse.set_visible(False)

  54.     def main():
  55.         pygame.init()

  56.         grayball_image = "gray_ball.png"
  57.         greenball_image = "green_ball.png"
  58.         glass_image = "glass.png"
  59.         mouse_image = "hand.png"
  60.         bg_image = "background.png"

  61.         running = True

  62.         pygame.mixer.music.load("bg_music.ogg")
  63.         pygame.mixer.music.play()

  64.         loser_sound = pygame.mixer.Sound("loser.wav")
  65.         laugh_sound = pygame.mixer.Sound("laugh.wav")
  66.         winner_sound = pygame.mixer.Sound("winner.wav")
  67.         hole_sound = pygame.mixer.Sound("hole.wav")

  68.         GAMEOVER = USEREVENT
  69.         pygame.mixer.muxic.set_endevent(GAMEOVER)

  70.         bg_size = [width,height] = [1024,681]
  71.         screen = pygame.display.set_mode(bg_size)
  72.         pygame.display.set_caption("摩擦摩擦 - Play The Ball")

  73.         background = pygame.image.load(bg_image).convert_alpha()

  74.         hole = [(117,119,199,201),(255,227,390,392),(503,505,320,322),(698,700,192,194),(906,908,419,421)]
  75.         msgs = []
  76.         balls = []
  77.         group = pygame.sprite.Group()
  78.         for i in range(5):
  79.             position = [randint(0,width - 100),randint(0,height - 100)]
  80.             speed = [randint(1,10),randint(1,10)]
  81.             ball = Ball(grayball_image,greenball_image,position,speed,bg_size,5 * (i + 1))

  82.             while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
  83.                 ball.rect.left = randint(0,width - 100)
  84.                 ball.rect.top = randint(0,height - 100)

  85.             balls.append(ball)
  86.             group.add(ball)

  87.         glass = Glass(glass_image,mouse_image,bg_size)
  88.         motion = 0
  89.         MYTIMER = USEREVENT + 1
  90.         pygame.time.set_timer(MYTIMER,1000)
  91.         pygame.key.set_repeat(100,100)
  92.         clock = pygame.time.Clock()

  93.         while running:
  94.             for event in pygame.event.get():
  95.                 if event.type == QUIT:
  96.                     pygame.quit()
  97.                     sys.exit()

  98.                 elif event.type == GAMEOVER:
  99.                     loser_sound.play()
  100.                     pygame.time.delay(2000)
  101.                     laugh_sound.play()
  102.                     running = False

  103.                 elif event.type == MYTIMER:
  104.                     if motion:
  105.                         for each in group:
  106.                             if each.check(motion):
  107.                                 each.speed = [0,0]
  108.                                 each.control = True
  109.                         motion = 0

  110.                 elif event.type == MOUSEMOTION:
  111.                     motion += 1

  112.                 elif event.type == KEYDOWN:
  113.                     if event.key == Up:
  114.                         for each in group:
  115.                             if each.control:
  116.                                 each.speed[1] -= 1

  117.                     elif event.key == Down:
  118.                         for each in group:
  119.                             if each.control:
  120.                                 each.speed[1] += 1

  121.                     elif event.key == Left:
  122.                         for each in group:
  123.                             if each.control:
  124.                                 each.speed[0] -= 1

  125.                     elif event.key == Right:
  126.                         for each in group:
  127.                             if each.control:
  128.                                 each.speed[0] += 1

  129.                     elif event.key == K_SPACE:
  130.                         for each in group:
  131.                             if each.control:
  132.                                 for i in hole:
  133.                                     if i[0] <= each.rect.left <= i[1] and i[2] <= each.rect.top <= i[3]:
  134.                                         hole_sound.play()
  135.                                         each.speed = [0,0]
  136.                                         group.remove(each)
  137.                                         temp = balls.pop(balls.index(each))
  138.                                         balls.insert(0,temp)
  139.                                         hole.remove(i)

  140.                                 if not hole:
  141.                                     pygame.mixer.muisc.stop()
  142.                                     winner_sound.play()
  143.                                     pygame.time.delay(3000)
  144.                                     msg = pygame.image.load("win.png").convert_alpha()
  145.                                     msg_pos = [(width - msg.get_width()) // 2,(height - msg.get_height())]
  146.                                     msgs.append((msg,msg_pos))
  147.                                     laugh_sound.play()

  148.                 screen.blit(background,(0,0))
  149.                 screen.blit(glass.glass_image,glass.glass_rect)

  150.                 glass.mouse_rect.left = pygame.mouse.get_pos()
  151.                 glass.mouse_rect.top = pygame.mouse.get_pos()

  152.                 if glass.mouse_rect.left < glass.glass_rect.left:
  153.                     glass.mouse_rect.left = glass.glass_rect.left

  154.                 if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
  155.                     glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width

  156.                 if glass.mouse_rect.top < glass.glass_rect.top:
  157.                     glass.mouse_rect.top = glass.glass_rect.top

  158.                 if glass.mouse_rect.top > glass.glass_rect.bottom - glass.mouse_rect.height:
  159.                     glass.mouse_rect.top = glass.glass_rect.bottom - glass.mouse_rect.height

  160.                 screen.blit(glass.mouse_image,glass.mouse_rect)

  161.                 for each in balls:
  162.                     each.move()
  163.                     if each.collide:
  164.                         each.speed = [randint(1,10),randint(1,10)]
  165.                         each.collide = False

  166.                     if each.control:
  167.                         screen.blit(each.greenball_image,each.rect)
  168.                     else:
  169.                         screen.blit(each.grayball_image,each.rect)

  170.                 for each in group:
  171.                     group.remove(each)

  172.                     if pygame.sprite.spritecollide(each,group,False,pygame.sprirte.collide_circle):
  173.                         each.side[0] = -each.side[0]
  174.                         each.side[1] = -each.side[1]
  175.                         each.collide = True

  176.                         if each.control:
  177.                             each.side[0] = -1
  178.                             each.side[1] = -1
  179.                             each.control = False

  180.                     group.add(each)

  181.                 for msg in msgs:
  182.                     screen.blit(msg[0],msg[1])

  183.                 pygame.display.flip()
  184.                 clock.tick(30)

  185.     if __name__ == "__main__":
  186.         try:
  187.             main()
  188.         except SystemExit:
  189.             pass
  190.         except:
  191.             traceback.print_exc()
  192.             pygame.quit()
  193.             input()

  194. except:
  195.     pygame.quit()
  196.     sys.exit()
复制代码
运行后代码如下:
  1. Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)] on win32
  2. Type "help", "copyright", "credits" or "license()" for more information.
  3. >>>
  4. ================ RESTART: C:\Users\Administrator\Desktop\摩擦摩擦.py ===============
  5. pygame 2.1.2 (SDL 2.0.18, Python 3.8.5)
  6. Hello from the pygame community. https://www.pygame.org/contribute.html
  7. Traceback (most recent call last):
  8.   File "C:\Users\Administrator\Desktop\摩擦摩擦.py", line 235, in <module>
  9.     main()
  10.   File "C:\Users\Administrator\Desktop\摩擦摩擦.py", line 76, in main
  11.     pygame.mixer.music.load("bg_music.ogg")
  12. pygame.error: No file 'bg_music.ogg' found in working directory 'C:\Users\Administrator\Desktop'.
复制代码
你们说为啥抛异常?还有,什么是pygame.error???
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-5-23 08:58:46 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-5-24 17:28:32 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-8-29 20:25:10 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-8-29 21:30:52 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-8-29 21:35:10 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-8-30 10:40:08 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-8-30 10:44:09 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

发表于 2022-8-30 20:15:37 | 显示全部楼层
此帖仅作者可见
小甲鱼最新课程 -> https://ilovefishc.com

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 01:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表