鱼C论坛

 找回密码
 立即注册
查看: 953|回复: 4

P82 帮我看下我的乌龟为啥在左侧死循环了

[复制链接]
发表于 2020-4-21 16:24:03 | 显示全部楼层 |阅读模式

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

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

x
import pygame
import sys
from pygame.locals import *

#初始化Pygame
pygame.init()

size=width,height=600,400
bg=(255,255,255)

fullscreen = False

clock = pygame.time.Clock()

#创建指定大小的窗口
screen=pygame.display.set_mode(size,RESIZABLE)
#设置窗口标特
pygame.display.set_caption('初次见面,请大家多多关照!')

#加载图片
turtle = pygame.image.load('turtle.png')

#获得图像的位置矩形
position = turtle.get_rect()

speed=[5,0]

turtle_right = pygame.transform.rotate(turtle,90)
turtle_top = pygame.transform.rotate(turtle,180)
turtle_left = pygame.transform.rotate(turtle,270)
turtle_bottom = turtle


l_head = turtle
r_head = pygame.transform.flip(turtle,True,False)

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

        if event.type == pygame.KEYDOWN:
            #全屏(F11)
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    screen = pygame.display.set_mode((1024,768),FULLSCREEN | HWSURFACE)
                    width,height = 1024,768

                else:
                    screen=pygame.display.set_mode(size)

            #用户调整窗口尺寸
        if event.type == VIDEORESIZE:
            size = event.size
            width,height = size
            print(size)
            screen=pygame.display.set_mode(size,RESIZABLE)

               

    #移动图像
    position = position.move(speed)

    if position.right > width:
        turtle = turtle_right
        position = turtle_rect = turtle.get_rect()
        position.left = width - turtle_rect.width
        speed = [0,5]

    if position.bottom > height:
        turtle = turtle_bottom
        position = turtle_rect = turtle.get_rect()
        position.left = width - turtle_rect.width
        position.top = height - turtle_rect.height
        speed = [-5,0]

    if position.left< 0:
        turtle = turtle_left
        position = turtle_rect = turtle.get_rect()
        position.top = height - turtle_rect.height
        speed = [0,-5]

    if position.top<0:
        turtle = turtle_top
        position = turtle_rect = turtle.get_rect()
        speed = [-5,0]
        


        
    #填充背景
    screen.fill(bg)
    #更新图像
    screen.blit(turtle,position)
    #更新界面
    pygame.display.flip()
    #延迟10毫秒
    pygame.time.delay(10)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-21 16:47:33 | 显示全部楼层
本帖最后由 wuqramy 于 2020-4-21 17:19 编辑

这一段
  1.     if position.top<0:
  2.         turtle = turtle_top
  3.         position = turtle_rect = turtle.get_rect()
  4.         speed = [-5,0]
复制代码

speed应该是[5,0]
正确代码:
  1. import pygame
  2. import sys
  3. from pygame.locals import *

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

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

  8. fullscreen = False

  9. clock = pygame.time.Clock()

  10. #创建指定大小的窗口
  11. screen=pygame.display.set_mode(size,RESIZABLE)
  12. #设置窗口标特
  13. pygame.display.set_caption('初次见面,请大家多多关照!')

  14. #加载图片
  15. turtle = pygame.image.load('turtle.png')

  16. #获得图像的位置矩形
  17. position = turtle.get_rect()

  18. speed=[5,0]

  19. turtle_right = pygame.transform.rotate(turtle,90)
  20. turtle_top = pygame.transform.rotate(turtle,180)
  21. turtle_left = pygame.transform.rotate(turtle,270)
  22. turtle_bottom = turtle


  23. l_head = turtle
  24. r_head = pygame.transform.flip(turtle,True,False)

  25. while True:
  26.     for event in pygame.event.get():
  27.         if event.type == pygame.QUIT:
  28.             sys.exit()

  29.         if event.type == pygame.KEYDOWN:
  30.             #全屏(F11)
  31.             if event.key == K_F11:
  32.                 fullscreen = not fullscreen
  33.                 if fullscreen:
  34.                     screen = pygame.display.set_mode((1024,768),FULLSCREEN | HWSURFACE)
  35.                     width,height = 1024,768

  36.                 else:
  37.                     screen=pygame.display.set_mode(size)

  38.             #用户调整窗口尺寸
  39.         if event.type == VIDEORESIZE:
  40.             size = event.size
  41.             width,height = size
  42.             print(size)
  43.             screen=pygame.display.set_mode(size,RESIZABLE)



  44.     #移动图像
  45.     position = position.move(speed)

  46.     if position.right > width:
  47.         turtle = turtle_right
  48.         position = turtle_rect = turtle.get_rect()
  49.         position.left = width - turtle_rect.width
  50.         speed = [0,5]

  51.     if position.bottom > height:
  52.         turtle = turtle_bottom
  53.         position = turtle_rect = turtle.get_rect()
  54.         position.left = width - turtle_rect.width
  55.         position.top = height - turtle_rect.height
  56.         speed = [-5,0]

  57.     if position.left< 0:
  58.         turtle = turtle_left
  59.         position = turtle_rect = turtle.get_rect()
  60.         position.top = height - turtle_rect.height
  61.         speed = [0,-5]

  62.     if position.top<0:
  63.         turtle = turtle_top
  64.         position = turtle_rect = turtle.get_rect()
  65.         speed = [5,0]




  66.     #填充背景
  67.     screen.fill(bg)
  68.     #更新图像
  69.     screen.blit(turtle,position)
  70.     #更新界面
  71.     pygame.display.flip()
  72.     #延迟10毫秒
  73.     pygame.time.delay(10)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-4-21 16:50:06 | 显示全部楼层
wuqramy 发表于 2020-4-21 16:47
这一段

speed应该是[5,0]

感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-21 16:51:41 | 显示全部楼层
wuqramy 发表于 2020-4-21 16:47
这一段

speed应该是[5,0]

改版了吗,没有设置最佳答案了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-21 17:20:37 | 显示全部楼层
CD84380973 发表于 2020-4-21 16:51
改版了吗,没有设置最佳答案了?

这好像不是一个求助帖,重新选择分类试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-7 08:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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