|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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:关联不是嘛?
******************************
感谢大神不吝赐教,为新手解疑释惑。
赠人玫瑰,手有余香,好人一生平安! |
|