1,小球隨機運動
import pygame
from random import randint
''' '''
def draw_cirtle(x,y):
pygame.draw.circle(window, ( 255,0,255),(x,y),20)
pygame.display.update()
''' '''
pygame.init()
window = pygame.display.set_mode((400, 600))
window.fill((255, 255, 255))
x=randint(20,580)
y=randint(20,580)
print(x,y)
a=randint(-3,3)
b=randint(-3,3)
add_x=a
add_y=b
# 游戲循環(huán)
while True:
print(add_x,add_y)
pygame.time.delay(10)
# 將之前紙上的內(nèi)容給覆蓋
window.fill((255, 255, 255))
# 不斷的畫圓
draw_cirtle(x,y)
if (x+20)>=400:
x=380
add_x *= -1
elif (x-20)<=0:
x = 20
add_x *= -1
elif (y+20)==600:
y=580
add_y *= -1
elif (y-20)<=0:
y = 20
add_y *= -1
y+=add_y
x+=add_x
# 事件檢測
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()