夜光序言:
希望有那么一个人,在路灯下或转角口安静的等候,用满心的温柔暖化寂冷的夜,让自己不再畏惧孤单。
正文:
# 夜光:矩形可移动的/
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Genius:Drawing Rectangles")
pos_x = 200
pos_y = 210
vel_x = 3
vel_y = 2
while True:
for event in pygame.event.get():
if event.type in (QUIT, KEYDOWN):
pygame.quit()
sys.exit()
screen.fill((0, 200, 200))
# 移动矩形
pos_x += vel_x
pos_y += vel_y
# Genius:使矩形保持在窗口内
if pos_x > 500 or pos_x < 0:
vel_x = -vel_x
if pos_y > 400 or pos_y < 0:
vel_y = -vel_y
# Genius:绘制矩形
color = 200, 255, 0
width = 0 # solid fill
pos = pos_x, pos_y, 100, 100
pygame.draw.rect(screen, color, pos, width)
pygame.display.update()