-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame_7.py
65 lines (46 loc) · 1.54 KB
/
pygame_7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pygame
from pygame.locals import *
from random import randint
class Star(object):
def __init__(self, x, y, speed):
self.x = x
self.y = y
self.speed = speed
def run():
pygame.init()
screen = pygame.display.set_mode((640, 480)) # , FULLSCREEN)
stars = []
# 在第一帧,画上一些星星
for n in range(200):
x = float(randint(0, 639))
y = float(randint(0, 479))
speed = float(randint(10, 300))
stars.append(Star(x, y, speed))
clock = pygame.time.Clock()
white = (255, 255, 255)
while True:
for event in pygame.event.get():
if event.type == QUIT:
return
if event.type == KEYDOWN:
return
# 增加一颗新的星星
y = float(randint(0, 479))
speed = float(randint(10, 300))
star = Star(640., y, speed)
stars.append(star)
time_passed = clock.tick()
time_passed_seconds = time_passed / 1000.
screen.fill((0, 0, 0))
# 绘制所有的星
for star in stars:
new_x = star.x - time_passed_seconds * star.speed
pygame.draw.aaline(screen, white, (new_x, star.y), (star.x + 1., star.y))
star.x = new_x
#def on_screen(star):
# return star.x > 0
# 星星跑出了画面,就删了它
#stars = filter(on_screen, stars)
pygame.display.update()
if __name__ == "__main__":
run()