pygame 绘制移动矩形,并碰到边界时改变颜色

import pygame,sys;
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((700,600))
pygame.display.set_caption("Drawing Rectangles")
pos_x = 300
pos_y = 250
vel_x = 6
vel_y = 3
a = int(10)
b = int(20)
c = int(10)
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,0,200))
    pos_x+=vel_x
    pos_y+=vel_y
    if pos_x > 600 or pos_x < 0:
        vel_x = -vel_x
        if a <= 205 and a >= 50:
            a = a + 50
        elif a > 205:
            a = a - 5
        elif a < 5:
            a = a + 50
        if b <= 205 and b >= 50:
            b = b + 50
        elif b > 205:
            b = b - 50
        elif b < 50:
            b = b + 50
        if c <= 205 and c >= 50:
            c = c + 5
        elif c > 205:
            c = c - 50
        elif c < 50:
            c = c + 20
        print(a)
        print(b)
        print(c)
    if pos_y > 500 or pos_y < 0:
        vel_y = -vel_y
        if a <= 205 and a >= 50:
            a = a + 22
        elif a > 205:
            a = a - 32
        elif a < 50:
            a = a + 21
        if b <= 205 and b >= 50:
            b = b + 23
        elif b > 205:
            b = b - 21
        elif b < 50:
            b = b + 21
        if c <= 205 and c >= 50:
            c = c + 23
        elif c > 205:
            c = c - 32
        elif c < 50:
            c = c + 43
        print(a)
        print(b)
        print(c)
    color = a,b,c
    width = 0
    pos = pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)
    pygame.display.update()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值