鱼C论坛

 找回密码
 立即注册
查看: 700|回复: 2

[已解决]help,救救孩子

[复制链接]
发表于 2020-2-26 21:41:29 | 显示全部楼层 |阅读模式

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

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

x
这是按照82课敲的
为什么效果不对
  1. import pygame
  2. from pygame.locals import*
  3. import sys


  4. #ygame初始化
  5. pygame.init()


  6. size = width, height = 600, 400
  7. bg = (255, 255, 255)

  8. screen = pygame.display.set_mode(size)
  9. clock = pygame.time.Clock()

  10. #放置图像
  11. xier = pygame.image.load('1.png')

  12. #0:未选择 1:选择中 2:完成选择
  13. select = 0
  14. select_rect = pygame.Rect(0, 0, 0, 0)

  15. #0:未选择 1:拖拽中 2:完成拖拽
  16. drag = 0

  17. position = xier.get_rect()
  18. position.center = width//2, height//2


  19. while True:
  20.     for event in pygame.event.get():
  21.         if event.type == QUIT:
  22.             sys.exit()

  23.         elif event.type == MOUSEBUTTONDOWN:
  24.             if event.button == 1:
  25.                 #第一次点击:选择范围
  26.                 if select == 0 and drag == 0:
  27.                     pos_start = event.pos
  28.                     select == 1
  29.                 #第二次点击:拖动图像
  30.                 elif select == 2 and drag == 0:
  31.                     copyimage = screen.subsurface(select_rect).copy()
  32.                     copy_image = copyimage.get_rect()
  33.                     drag = 1
  34.                 #第三次点击:初始化
  35.                 elif select == 2 and drag == 2:
  36.                     select = 0
  37.                     drag =0

  38.         elif event.type == MOUSEBUTTONUP:
  39.             if event.button == 1:
  40.                 #第一次释放:选择结束
  41.                 if select == 1 and drag == 0:
  42.                     pos_stop = event.pos
  43.                     select = 2
  44.                 #第二次释放:拖拽结束
  45.                 elif select == 2 and drag == 1:
  46.                     drag =2

  47.     screen.fill(bg)
  48.     screen.blit(xier, position)

  49.     if select:
  50.         mose_pos = pygame.mouse.get_pos()
  51.         if select == 1:
  52.             pos_stop = mouse_pos
  53.         select_rect.left, select_rect.top = pos_start
  54.         select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
  55.         pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)

  56.     if drag:
  57.         if drag == 1:
  58.             copy_image.center = mouse_pos
  59.         screen.blit(copyimage, copy_image)

  60.     pygame.display.flip()
  61.     clock.tick(30)
复制代码

求救!!!
最佳答案
2020-2-27 16:46:24
nick5185 发表于 2020-2-27 07:43
没有报错,只是效果不对
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. size = width, height = 800, 600
  6. bg = (255, 255, 255)

  7. clock = pygame.time.Clock()
  8. screen = pygame.display.set_mode(size)
  9. pygame.display.set_caption("FishC Demo")

  10. turtle = pygame.image.load("turtle.png")

  11. # 0 -> 未选择,1 -> 选择中,2 -> 完成选择
  12. select = 0
  13. select_rect = pygame.Rect(0, 0, 0, 0)
  14. # 0 -> 未拖拽,1 -> 拖拽中,2 -> 完成拖拽
  15. drag = 0

  16. position = turtle.get_rect()
  17. position.center = width // 2, height // 2

  18. while True:
  19.     for event in pygame.event.get():
  20.         if event.type == QUIT:
  21.             sys.exit()

  22.         elif event.type == MOUSEBUTTONDOWN:
  23.             if event.button == 1:
  24.                 # 第一次点击,选择范围
  25.                 if select == 0 and drag == 0:
  26.                     pos_start = event.pos
  27.                     select = 1
  28.                 # 第二次点击,推拽图像
  29.                 elif select == 2 and drag == 0:
  30.                     capture = screen.subsurface(select_rect).copy()
  31.                     cap_rect = capture.get_rect()
  32.                     drag = 1
  33.                 # 第三次点击,初始化
  34.                 elif select == 2 and drag == 2:
  35.                     select = 0
  36.                     drag = 0

  37.         elif event.type == MOUSEBUTTONUP:
  38.             if event.button == 1:
  39.                 # 第一次释放,结束选择
  40.                 if select == 1 and drag == 0:
  41.                     pos_stop = event.pos
  42.                     select = 2
  43.                 # 第二次释放,结束拖拽
  44.                 if select == 2 and drag == 1:
  45.                     drag = 2
  46.                
  47.     screen.fill(bg)
  48.     screen.blit(turtle, position)
  49.    
  50.     # 实时绘制选择框
  51.     if select:
  52.         mouse_pos = pygame.mouse.get_pos()
  53.         if select == 1:
  54.             pos_stop = mouse_pos
  55.         select_rect.left, select_rect.top = pos_start
  56.         select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
  57.         pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)

  58.     # 拖拽裁剪的图像
  59.     if drag:
  60.         if drag == 1:
  61.             cap_rect.center = mouse_pos
  62.         screen.blit(capture, cap_rect)
  63.            
  64.     pygame.display.flip()
  65.    
  66.     clock.tick(30)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-27 07:43:43 | 显示全部楼层
假面的假面 发表于 2020-2-26 22:31
请粘贴报错信息。是不是模块没有下载?pygame?

没有报错,只是效果不对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-27 16:46:24 | 显示全部楼层    本楼为最佳答案   
nick5185 发表于 2020-2-27 07:43
没有报错,只是效果不对
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. size = width, height = 800, 600
  6. bg = (255, 255, 255)

  7. clock = pygame.time.Clock()
  8. screen = pygame.display.set_mode(size)
  9. pygame.display.set_caption("FishC Demo")

  10. turtle = pygame.image.load("turtle.png")

  11. # 0 -> 未选择,1 -> 选择中,2 -> 完成选择
  12. select = 0
  13. select_rect = pygame.Rect(0, 0, 0, 0)
  14. # 0 -> 未拖拽,1 -> 拖拽中,2 -> 完成拖拽
  15. drag = 0

  16. position = turtle.get_rect()
  17. position.center = width // 2, height // 2

  18. while True:
  19.     for event in pygame.event.get():
  20.         if event.type == QUIT:
  21.             sys.exit()

  22.         elif event.type == MOUSEBUTTONDOWN:
  23.             if event.button == 1:
  24.                 # 第一次点击,选择范围
  25.                 if select == 0 and drag == 0:
  26.                     pos_start = event.pos
  27.                     select = 1
  28.                 # 第二次点击,推拽图像
  29.                 elif select == 2 and drag == 0:
  30.                     capture = screen.subsurface(select_rect).copy()
  31.                     cap_rect = capture.get_rect()
  32.                     drag = 1
  33.                 # 第三次点击,初始化
  34.                 elif select == 2 and drag == 2:
  35.                     select = 0
  36.                     drag = 0

  37.         elif event.type == MOUSEBUTTONUP:
  38.             if event.button == 1:
  39.                 # 第一次释放,结束选择
  40.                 if select == 1 and drag == 0:
  41.                     pos_stop = event.pos
  42.                     select = 2
  43.                 # 第二次释放,结束拖拽
  44.                 if select == 2 and drag == 1:
  45.                     drag = 2
  46.                
  47.     screen.fill(bg)
  48.     screen.blit(turtle, position)
  49.    
  50.     # 实时绘制选择框
  51.     if select:
  52.         mouse_pos = pygame.mouse.get_pos()
  53.         if select == 1:
  54.             pos_stop = mouse_pos
  55.         select_rect.left, select_rect.top = pos_start
  56.         select_rect.width, select_rect.height = pos_stop[0] - pos_start[0], pos_stop[1] - pos_start[1]
  57.         pygame.draw.rect(screen, (0, 0, 0), select_rect, 1)

  58.     # 拖拽裁剪的图像
  59.     if drag:
  60.         if drag == 1:
  61.             cap_rect.center = mouse_pos
  62.         screen.blit(capture, cap_rect)
  63.            
  64.     pygame.display.flip()
  65.    
  66.     clock.tick(30)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-29 03:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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