基本信息
源码名称:pygame画移动矩形
源码大小:0.70KB
文件格式:.py
开发语言:Python
更新时间:2021-12-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 1 元 
   源码介绍
pygame画移动矩形


import pygame
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,0,200))
    #move the rectangle
    pos_x =vel_x
    pos_y =vel_y
    #keep rectangle on the screen
    if pos_x>500 or pos_x<0:
        vel_x=-vel_x
    if pos_y>300 or pos_y<0:
        vel_y=-vel_y
    #draw the rectangle
    color=255,255,0
    width=0 #solid fill
    pos=pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)
    pygame.display.update()