|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位鱼油好 我是刚学Python的小白。今天刚刚看完第81讲的视频,因为我找不到在哪里能看到55讲后面的课后作业但是老师却在视频上说过有作业 不过说这次作业有一个是实现老师在视频最后展示的程序功能所以我就争取做了(其实之前的很多作业因为没有鱼币做不了哈哈哈(除了只靠每天抢第一签到拿一点以外)
那么废话了那么久 以下进正题了:
我根据老师给出的需求 写出了以下的代码(我的代码很多跟着老师在视频里打的一样的) 但是还有很多的BUG 还有很多的没完善的地方 还有一些功能并不知道怎么解决 想请各位大神帮我看看我的代码,哪里可以优化一下 哪里的问题可以解决 代码工整和写代码的习惯 我都希望能得到指导一下 先和大家说声谢谢啦
代码如下:
- import pygame
- import sys
- from pygame.locals import *
- #初始化Pygame
- pygame.init()
- modes = pygame.display.list_modes()
- background = pygame.image.load("bg.jpg")
- size = modes[17]
- speed = [1, 0]
- #创建指定大小的窗口 (返回一个Surface对象)
- screen = pygame.display.set_mode(size, RESIZABLE)
- #设置窗口标题
- pygame.display.set_caption("81讲课后作业")
- # 设置放大缩小的比率
- ratio = 1.0
- ophoto = pygame.image.load("QQ.jpg")
- photo = ophoto
- ophoto_rect = ophoto.get_rect()
- position = photo_rect = ophoto_rect
- clock = pygame.time.Clock()
- fullscreen = False
- l_head = photo
- r_head = pygame.transform.flip(photo, True, False)
- #播放背景音乐
- pygame.mixer.music.load("music.ogg")
- pygame.mixer.music.play(loops=-1, start=0.0)
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.mixer.music.fadeout(1000)
- sys.exit()
- # 按鼠标进行加速
- if event.type == MOUSEBUTTONDOWN:
- pressed_button = pygame.mouse.get_pressed()
- for index in range(len(pressed_button)):
- if index == 0:
- if speed[0] != 0 and speed[0] > 0:
- speed[0] += 1
- if speed[1] != 0 and speed[1] > 0:
- speed[1] += 1
- if speed[0] != 0 and speed[0] < 0:
- speed[0] -= 1
- if speed[1] != 0 and speed[1] < 0:
- speed[1] -= 1
-
- if event.type == KEYDOWN:
- if event.key == K_LEFT:
- photo = l_head
- speed = [-1, 0]
- if event.key == K_RIGHT:
- photo = r_head
- speed = [1, 0]
- if event.key == K_UP:
- speed = [0, -1]
- if event.key == K_DOWN:
- speed = [0, 1]
-
- #全屏(F11)
- if event.key == K_F11:
- fullscreen = not fullscreen
- if fullscreen:
- screen = pygame.display.set_mode(modes[0], FULLSCREEN | HWSURFACE)
- width, height = modes[0]
- else:
- screen = pygame.display.set_mode(size)
- #放大、缩小图片(=、-),空格键恢复原始尺寸
- if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
- #最大只能放大一倍,缩小50%
- if event.key == K_EQUALS and ratio < 2:
- ratio += 0.1
- if event.key == K_MINUS and ratio > 0.5:
- ratio -= 0.1
- if event.key == K_SPACE:
- ratio = 1.0
- photo = pygame.transform.smoothscale(ophoto, \
- (int(ophoto_rect.width * ratio), \
- int(ophoto_rect.height * ratio)))
- l_head = photo
- r_head = pygame.transform.flip(photo, True, False)
- #同时按住Ctrl+s缩小分辨率 和 同时按住Ctrl+b增加分辨率
- if event.key == K_b and KMOD_CTRL:
- i = modes.index(size)
- if i != 0:
- screen = pygame.display.set_mode(modes[i-1])
- width, height = modes[i-1]
- else:
- pass
- if event.key == K_s and KMOD_CTRL:
- i = modes.index(size)
- modes_limit = len(modes)
- try:
- if i != modes_limit:
- screen = pygame.display.set_mode(modes[i+1])
- width, height = modes[i+1]
- else:
- pass
- except IndexError:
- pass
-
- #用户调整窗口尺寸
- if event.type == VIDEORESIZE:
- size = event.size
- width, height = size
- screen = pygame.display.set_mode(size, RESIZABLE)
-
- #移动图像
- position = position.move(speed)
- if position.left < 0 or position.right > width:
- #翻转图像
- photo = pygame.transform.flip(photo, True, False)
- #反方向移动
- speed[0] = -speed[0]
- if position.top <0 or position.bottom > height:
- speed[1] = -speed[1]
- #填充背景
- screen.blit(background,(0, 0))
- #更新图像
- screen.blit(photo, position)
- #更新界面
- pygame.display.flip()
- #延迟
- clock.tick(200)
复制代码
以下是我自己总结出自己的代码的一些问题 (很多问题我也有上网查过 但是.....)
1:草地背景----------------------OK(并不完美 在调节大小的时候 分辨率大的时候会超出图片范围)
2:西游记背景音乐----------------OK
3:点击鼠标左键加速--------------OK(存在BUG:不管是左键右键还是中间滑轮 都是会加速的 并未找到解决方案)
4:按Ctrl+s缩小分辨率------------OK
5:按Ctrl+b增加分辨率------------OK
6:滚动滑轮可以使其透明化
7:按+放大-----------------------OK(备注问题未解决)
8:按-减小-----------------------OK(备注问题未解决)
备注:7、8点注意放大的头的方向问题 原始方向是 眼睛在左边
还有放大后走到超过边界会卡在边界(如果缩小的话 还会整个图像不见了)的问题
(第6个要求我还没有去研究找方法) |
评分
-
查看全部评分
|