pygame學習之繪制移動的矩形


 1 import pygame
 2 from pygame.locals import *
 3 
 4 pygame.init()
 5 screen = pygame.display.set_mode((600, 500))
 6 pygame.display.set_caption("drawing a rectangle")
 7 pos_x = 300
 8 pos_y = 250
 9 vel_x = 2                            # 設置速度變量
10 vel_y = 1
11 while True:
12     for event in pygame.event.get():
13         if event.type in (QUIT, KEYDOWN):
14             exit()
15     screen.fill((0, 0, 200))
16     pos_x += vel_x
17     pos_y += vel_y
18 
19     if pos_x > 500 or pos_x < 0:
20         vel_x = -vel_x                       # 讓矩形在窗口范圍內移動
21     if pos_y > 400 or pos_y < 0:
22         vel_y = -vel_y
23     color = 255, 255, 0
24     width = 0
25     pos = pos_x, pos_y, 100, 100             # 矩形長和寬都為100
26     pygame.draw.rect(screen, color, pos, width)
27     pygame.display.update()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM