鱼C论坛

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

[技术交流] 零基础入门学习python 第81课pygame课后作业第一草案

[复制链接]
发表于 2017-3-30 02:12:02 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 长颈鹿摘月亮 于 2017-3-30 02:16 编辑

小甲鱼老师在课上留了个乌龟移动的debug作业,由于没找到小甲鱼老师的作业答案,于是就把自己写的发上来希望能得到一些意见,如果有我没发现的bug或者有跟好跟简洁的语句,非常欢迎指出。写这个的时候也是纠结了好几天,经常遇到的情况是‘旧bug未平,新bug又起’
  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. width,height=600,400
  6. walk=1
  7. speed=[walk,0]
  8. bg=(255,255,255)

  9. screen=pygame.display.set_mode((width,height),RESIZABLE)
  10. pygame.display.set_caption('pygame入门')

  11. ori_turtle=pygame.image.load('turtle.png')
  12. turtle=pygame.transform.flip(ori_turtle,True,False)
  13. ori_position=position=ori_turtle.get_rect()

  14. clock=pygame.time.Clock()
  15. face_left=ori_turtle
  16. face_right=turtle
  17. walk_right=True       #判断头朝向
  18. ratio=1.0

  19. while True:
  20.     for event in pygame.event.get():
  21.         if event.type==QUIT:
  22.             sys.exit()
  23.             
  24.         if event.type==KEYDOWN:   #上下左右移动
  25.             if event.key==K_UP:
  26.                 speed=[0,-walk]
  27.             if event.key==K_DOWN:
  28.                 speed=[0,walk]
  29.             if event.key==K_LEFT:
  30.                 turtle=face_left
  31.                 speed=[-walk,0]
  32.                 walk_right=False
  33.             if event.key==K_RIGHT:
  34.                 turtle=face_right
  35.                 speed=[walk,0]
  36.                 walk_right=True
  37.             
  38.             if event.key== K_s and pygame.key.get_mods() & KMOD_CTRL:  #缩小屏幕
  39.                 width-=100
  40.                 height-=100
  41.                 screen=pygame.display.set_mode((width,height))
  42.             if event.key==K_b and pygame.key.get_mods() & KMOD_CTRL:   #放大屏幕
  43.                 width+=100
  44.                 height+=100
  45.                 screen=pygame.display.set_mode((width,height))
  46.                
  47.             if (event.key==K_EQUALS) or (event.key==K_MINUS) or (event.key==K_SPACE):  #改变乌龟尺寸
  48.                 if (event.key== K_EQUALS) and (ratio<2.0):
  49.                     ratio+=0.1
  50.                 elif (event.key==K_MINUS) and (ratio>0.5):
  51.                     ratio-=0.1
  52.                 elif event.key==K_SPACE:
  53.                     ratio=1.0
  54.                
  55.                 turtle=pygame.transform.smoothscale(ori_turtle,(int(ori_position.width*ratio),int(ori_position.height*ratio)))
  56.                 face_left=turtle
  57.                 face_right=pygame.transform.flip(turtle,True,False)
  58.                 position.size=(int(ori_position.width*ratio), int(ori_position.height*ratio))   #改变rect对象尺寸使其不出界
  59.                
  60.                 if walk_right:         
  61.                     
  62.                     turtle=face_right
  63.                 else:
  64.                     turtle=face_left
  65.         
  66.                
  67.         if event.type==MOUSEBUTTONDOWN:  #加速运动
  68.             if event.button==1:
  69.                 speed=[i*2 for i in speed]
  70.                 walk*=2
  71.                
  72.                
  73.     if speed[0]<0:     #判断运动方向
  74.         walk_right=False
  75.     if speed[0]>0:
  76.         walk_right=True
  77.     screen.fill(bg)
  78.     screen.blit(turtle,position)
  79.     pygame.display.flip()
  80.     position=position.move(speed)
  81.    
  82.    
  83.     if position.left<0 or position.right>width:          #限制在屏幕中运动
  84.         if position.left<0:
  85.             position=position.move(0-position.left,0)
  86.         else:
  87.             position=position.move(width-position.right,0)
  88.         turtle=pygame.transform.flip(turtle,True,False)
  89.         speed[0]=-speed[0]
  90.    
  91.     if position.top<0 or position.bottom>height:
  92.         if position.top<0:
  93.             position=position.move(0,0-position.top)
  94.         else:
  95.             position=position.move(0,height-position.bottom)
  96.         speed[1]=-speed[1]
  97.         
  98.     clock.tick(40)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-20 16:22:05 | 显示全部楼层
我也在学这一课了,乌龟转圈的一直明白不了啊!我也贴上代码咱们相互研究一下。
  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) # RGB

  8. fullscreen = False

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

  13. # 加在图片
  14. turtle = pygame.image.load("turtle.png")
  15. # 获得图像的位置矩形
  16. position = turtle.get_rect()
  17. speed = [5,0]
  18. turtle_right = pygame.transform.rotate(turtle,90)
  19. turtle_top = pygame.transform.rotate(turtle,180)
  20. turtle_left = pygame.transform.rotate(turtle,270)
  21. turtle_bottom = turtle
  22. turtle = turtle_top
  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 == 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.                 else:
  36.                     screen = pygame.display.set_mode(size)

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

  39.     if position.right > width:
  40.         turtle = turtle_right
  41.         position = turtle_rect = turtle.get_rect()
  42.         position.left =width - turtle_rect.width#为什么要这么算,不明白啊!
  43.         speed = [0,5]

  44.     if position.bottom > height:
  45.         turtle = turtle_bottom
  46.         position = turtle_rect = turtle.get_rect()
  47.         position.left =width - turtle_rect.width#这里为什么要两个方向其它只要一个方向!
  48.         position.top = height - turtle_rect.height#这里为什么要两个方向其它只要一个方向!
  49.         speed = [-5,0]

  50.    
  51.     if position.left < 0:
  52.         turtle = turtle_left
  53.         position = turtle_rect = turtle.get_rect()
  54.         position.top = height - turtle_rect.height#为什么要这么算,不明白啊!
  55.         speed = [0,-5]

  56.     if position.top < 0:
  57.         turtle = turtle_top
  58.         position = turtle_rect = turtle.get_rect()
  59.         position.right =turtle_rect.width#为什么要这么算,不明白啊!
  60.         speed = [5,0]

  61.     # 填充背景
  62.     screen.fill(bg)
  63.     # 更新图像
  64.     screen.blit(turtle, position)
  65.     # 更新界面
  66.     pygame.display.flip()
  67.     # 延迟10毫秒
  68.     pygame.time.delay(10)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-4 04:37:28 | 显示全部楼层
隨鈊乄鎍慾 发表于 2017-4-20 16:22
我也在学这一课了,乌龟转圈的一直明白不了啊!我也贴上代码咱们相互研究一下。

这是标准答案?上下左右和放大缩小的操作都没有啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-21 10:58:26 | 显示全部楼层
太多的代码都是我没学过的,看得我是一个头两个大,研究了好久才知道乌龟是怎么走的。。然后再看82课,你妹的又是这种磨脑子的练习。之后我看了好久,我想吐了。暂时先去看爬虫的,之后再来补上。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-8-16 15:39:56 | 显示全部楼层
speed=[i*2 for i in speed]
这句是什么意思呢?求解
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-2 13:41:02 | 显示全部楼层
YogurtX 发表于 2018-8-16 15:39
speed=
这句是什么意思呢?求解

就是两倍速啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-11 22:33:25 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-22 23:17:23 | 显示全部楼层
本帖最后由 月-空轨 于 2019-10-22 23:25 编辑

[code]#支持40-200帧率
import pygame
import sys
#导入所有常量名
from pygame.locals import *


#初始化pygame
pygame.init()

size = width,height=1000,600
#位移
speed = [-2,1]

bg = (255,255,255)#RGB颜色。这里白色


fullscreen = False

#设置帧率
clock = pygame.time.Clock()

#创建指定大小的窗口,返回一个Surface对象,RESIZABLE窗口可修改resizable
screen = pygame.display.set_mode(size,RESIZABLE)

#设置窗口标题
pygame.display.set_caption("初次见面,请大家多多关照!")

#设置放大缩小的比例
ratio = 1.0

#加载图片
otrail = pygame.image.load('turtle.png')
trail = otrail
otrail_rect = otrail.get_rect()
#获取图像的位置矩形
position = trail_rect = otrail_rect

#转头/向
l_head = trail
#(trail,True,False) (图像,左右翻转,上下翻转)
r_head = pygame.transform.flip(trail,True,False)

while True:
    #获取事件关闭
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()


        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                trail = l_head
                speed = [-1,0]
            if event.key == K_RIGHT:
                trail = r_head
                speed = [1,0]
            if event.key == K_UP:
                speed = [0,-1]
            if event.key == K_DOWN:
                speed = [0,1]
               
            #全屏
            if event.key == K_F11:
                fullscreen = not fullscreen
                if fullscreen:
                    #FULLSCREEN | HWSURFACE硬件加速hwsurface
                    screen = pygame.display.set_mode((1366,768),FULLSCREEN | HWSURFACE)
                    size = width,height = 1366,768
                else:
                    size = width,height = 1000,600
                    screen = pygame.display.set_mode(size)
            #放大缩小(=,-),空格键恢复尺寸
            if event.key == K_EQUALS or event.key == K_MINUS or event.key == K_SPACE:
                if event.key == K_EQUALS and ratio <2:
                    ratio +=0.1
                if event.key == K_MINUS and ratio >0.2:
                    ratio -=0.1
                if event.key == K_SPACE:
                    ratio = 1.0
                trail = pygame.transform.smoothscale(otrail,\

                                             (int(otrail_rect.width*ratio),\
                                             int(otrail_rect.height*ratio)))
                position = trail_rect = trail.get_rect()
        #VIDEORESIZE用户调整窗口尺寸videoresize
        if event.type == VIDEORESIZE:
            size = event.size
            width,height = size
            print(size)
            #创建指定大小的窗口,返回一个Surface对象,RESIZABLE窗口可修改resizable
            screen = pygame.display.set_mode(size,RESIZABLE)
               

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



    if position.right>width:
        #trail_rect = trail.get_rect()
        #position = trail_rect = trail.get_rect()
        # 这里重新获取了 dog,即 left 和 top 又初始了0
        print(position.left,position.right,position.top,position.bottom)
        # 因此这里需要重新更新下 left 保证沿右边界运行
        position.left = width -  trail_rect.width
        trail = pygame.transform.flip(trail,True,False)
        speed[0]=-speed[0]
        
    if position.left <0 :
        #翻转图像
        #(trail,True,False) (图像,左右翻转,上下翻转)
        trail = pygame.transform.flip(trail,True,False)
        #反方向移动  speed[0]=-(-2)
        speed[0]=-speed[0]
        
    if position.top<0:
        speed[1] = -speed[1]
        

    if position.bottom>height:
        #trail_rect = trail.get_rect()
        position.top = height - trail_rect.bottom
        speed[1] = -speed[1]

    #填充背景
    screen.fill(bg)
    #更新图像 blit 將一个图像画到另一个图像上
    screen.blit(trail,position)
    #更新界面
    pygame.display.flip()
    #延迟10毫秒
    pygame.time.delay(10)
    #一秒一帧
    #clock.tick(1)
   
        
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-22 23:25:52 | 显示全部楼层
月-空轨 发表于 2019-10-22 23:17
[code]#支持40-200帧率
import pygame
import sys

放大缩小要从起始位置开始,怎解????????啊啊啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-23 23:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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