鱼C论坛

 找回密码
 立即注册
查看: 1825|回复: 3

关于=与==的使用

[复制链接]
发表于 2022-7-13 16:14:48 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 lzb1001 于 2022-7-13 21:37 编辑

# 绘制圆形

import pygame
import sys
from pygame.locals import *


pygame.init()



WHITE = (255, 255, 255) # 白色
BLACK = (0, 0, 0) # 黑色
RED = (255, 0, 0) # 红色
GREY = (128, 128, 128) # 灰色
YELLOW = (255, 255, 0) # 黄色
BLUE = (0, 0, 255) # 蓝色
GREEN = (0, 128, 0) # 绿色


size = width, height = 640, 400
screen = pygame.display.set_mode(size) # 生成一个screen的Surface对象
pygame.display.set_caption('FishC Demo')


position = size[0]//2, size[1]//2


moving = False


clock = pygame.time.Clock()



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

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                moving = True

        if event.type == MOUSEBUTTONUP:
            if event.button == 1: # 好像可以省略不要
                moving = False


    if moving: # 也可写成if moving == True:
        position = pygame.mouse.get_pos()
            

    screen.fill(WHITE)


   
    pygame.draw.circle(screen, BLACK, position, 3, 0)
    pygame.draw.circle(screen, RED, position, 25, 1)
    pygame.draw.circle(screen, YELLOW, position, 50, 1)
    pygame.draw.circle(screen, BLUE, position, 75, 1)
    pygame.draw.circle(screen, GREEN, position, 100, 1)
    pygame.draw.circle(screen, GREY, position, 125, 1)
    pygame.draw.circle(screen, BLACK, position, 150, 1)
  
   

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

------------------------------------------
Windows 10 专业版 | Python 3.7.6
------------------------------------------

【我的问题】

1、=是赋值,比如moving = False,而==是判断,比如if moving == True或if event.button == 1

上面这样理解不知道对不对?

2、见代码中的蓝色字体,该行代码是否可以省略?

3、当if moving:时,也就是鼠标左键按下时,实时获得鼠标当前的坐标,而如果把if moving:改成if moving == False时,为何不需要鼠标左键,即只要鼠标移动到哪,同心圆就移动到哪呢?这个问题不知道大家理不理解我的困惑在哪 - > 代码中if moving == False与if event.button == 1:关联不是嘛?

******************************

感谢大神不吝赐教,为新手解疑释惑。

赠人玫瑰,手有余香,好人一生平安!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-13 18:29:00 | 显示全部楼层
1.对
2.不可以省略,我给你分析一下:
 if event.type == MOUSEBUTTONUP: #这里表示如果按下鼠标,并不直接代表按下鼠标左键,所以需要下面进一步判断是按下鼠标哪一个键,不能省略,不然按下鼠标右键也算了
            if event.button == 1: # 判断是否按下的为鼠标左键,1 表示鼠标左键
                moving = False
3.因为你前面代码:
if event.type == MOUSEBUTTONDOWN:
            if event.button == 1: #当按下鼠标左键即可以移动圆
                moving = True
所以如果改成:
if moving == False
就成了:
如果不按下鼠标左键
因为:
moving 为 True 时,就代表按下鼠标左键,反之松开,所以会导致你说的效果相反!
4.对
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-13 20:45:19 | 显示全部楼层
本帖最后由 lzb1001 于 2022-7-13 21:33 编辑
python爱好者. 发表于 2022-7-13 18:29
1.对
2.不可以省略,我给你分析一下:


谢谢大神指点。

2、我的想法是:

---当按下鼠标按键的时候,才需要进一步判断按下的是鼠标的哪个键,也就是当按下鼠标按键的时候,if event.button == 1这行代码为必须
---而当释放鼠标按键的时候,可以不需要判断释放的是鼠标的哪个键,也就是当释放鼠标按键的时候,不需要if event.button == 1这行代码

经测试,如果把释放鼠标按键时候的该行代码删除,运行起来效果好像不受任何影响。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-13 21:21:53 | 显示全部楼层
本帖最后由 lzb1001 于 2022-7-13 22:13 编辑
python爱好者. 发表于 2022-7-13 18:29
1.对
2.不可以省略,我给你分析一下:


我将代码中while循环语句修改为如下:
……
while True:
   
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()

        if event.button == 1:
            
            if event.type == MOUSEBUTTONDOWN:

                moving = True

            if event.type == MOUSEBUTTONUP:
            
                moving = False
……

结果运行后返回如下错误提示!
====================== RESTART: D:\work\p16_6_pg_3 - 副本.py ==============
pygame 2.1.2 (SDL 2.0.18, Python 3.7.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "D:\work\p16_6_pg_3 - 副本.py", line 51, in <module>
    if event.button == 1:
AttributeError: 'Event' object has no attribute 'button'
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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