鱼C论坛

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

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

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

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

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

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

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

try:
    class Ball(pygame.sprite.Sprite):
        def __init__(self,grayball_image,greenball_image,position,speed,bg_size,target):
            pygame.sprite.Sprite.__init__(self)
            self.grayball_image = pygame.image.load(grayball_image).convert_alpha()
            self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
            self.rect = self.grayball_image.get_rect()
            self.rect.left = position
            self.rect.top = position
            self.side = [choice([-1,1]),choice([-1,1])]
            self.speed = speed
            self.collide = False
            self.target = target
            self.control = False
            self.width = bg_size[0]
            self.height = bg_size[1]
            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[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(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_imagr.get_rect()
            self.glass_rect.left = (bg_size[0] - self.glass_rect.width) // 2
            self.glass_rect.top = bg_size[1] - 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.glass_rect.left
            self.mouse_rect.top = self.glass_rect.top
            pygame.mouse.set_visible(False)

    def main():
        pygame.init()

        grayball_image = "gray_ball.png"
        greenball_image = "green_ball.png"
        glass_image = "glass.png"
        mouse_image = "hand.png"
        bg_image = "background.png"

        running = True

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

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

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

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

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

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

            while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
                ball.rect.left = randint(0,width - 100)
                ball.rect.top = 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:
                    pygame.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 = [0,0]
                                each.control = True
                        motion = 0

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

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

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

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

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

                    elif event.key == K_SPACE:
                        for each in group:
                            if each.control:
                                for i in hole:
                                    if i[0] <= each.rect.left <= i[1] and i[2] <= each.rect.top <= i[3]:
                                        hole_sound.play()
                                        each.speed = [0,0]
                                        group.remove(each)
                                        temp = balls.pop(balls.index(each))
                                        balls.insert(0,temp)
                                        hole.remove(i)

                                if not hole:
                                    pygame.mixer.muisc.stop()
                                    winner_sound.play()
                                    pygame.time.delay(3000)
                                    msg = pygame.image.load("win.png").convert_alpha()
                                    msg_pos = [(width - msg.get_width()) // 2,(height - msg.get_height())]
                                    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 = pygame.mouse.get_pos()
                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 = [randint(1,10),randint(1,10)]
                        each.collide = False

                    if each.control:
                        screen.blit(each.greenball_image,each.rect)
                    else:
                        screen.blit(each.grayball_image,each.rect)

                for each in group:
                    group.remove(each)

                    if pygame.sprite.spritecollide(each,group,False,pygame.sprirte.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])

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

    if __name__ == "__main__":
        try:
            main()
        except SystemExit:
            pass
        except:
            traceback.print_exc()
            pygame.quit()
            input()

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

使用道具 举报

发表于 2022-5-23 08:58:46 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-5-24 17:28:32 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-8-29 20:25:10 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-8-29 21:30:52 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-8-29 21:35:10 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-8-30 10:40:08 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

 楼主| 发表于 2022-8-30 10:44:09 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

发表于 2022-8-30 20:15:37 | 显示全部楼层
此帖仅作者可见
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-28 16:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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