• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Jul 23rd, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import pygame
2. from random import randint # 引入randint
3.
4. pygame.init()
5.
6. DISPLAY_WIDTH = 800
7. DISPLAY_HEIGHT = 600
8.
9. gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
10. pygame.display.set_caption('Random Spawn Stone per Second')
11.
12. WHITE = (255, 255, 255)
13.
14. clock = pygame.time.Clock()
15. playing = True
16.
17. rabbitImg = pygame.image.load('rabbit.jpg') # 記得自己去找一張圖片
19.
20. x = (DISPLAY_WIDTH * 0.45)
21. y = (DISPLAY_HEIGHT * 0.75)
22. direction_x = 0
23. speed_x = 5
24.
25. # ---------------新增與修改----------------
26. stone_num = 0
27. stone_x = []
28. stone_y = []
29. stone_y_speed = []
30. last_spawn_stone = pygame.time.get_ticks()
31. # ------------------------------------------
32.
33. def draw_rabbit(x, y):
34.     gameDisplay.blit(rabbitImg, (x, y))
35.
36. def draw_stone(x, y):
37.     gameDisplay.blit(stoneImg, (x, y))
38.
39. while playing:
40.     for event in pygame.event.get():
41.         if event.type == pygame.QUIT:
42.             playing = False
43.         if event.type == pygame.KEYDOWN:
44.             if event.key == pygame.K_LEFT:
45.                 direction_x = -1
46.             elif event.key == pygame.K_RIGHT:
47.                 direction_x = 1
48.         if event.type == pygame.KEYUP:
49.             if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
50.                 direction_x = 0
51.
52.     # ------------------新增----------------------
53.     now = pygame.time.get_ticks()
54.     if now - last_spawn_stone >= 1000:
55.         stone_x.append(randint(0, 750))
56.         stone_y.append(-50)
57.         stone_y_speed.append(randint(3, 7))
58.         stone_num += 1
59.         last_spawn_stone = now
60.     # ----------------------------------------------
61.
62.     x += direction_x * speed_x
63.     if x <= 0:
64.         x = 0
65.     elif x + 100 >= 800:
66.         x = 700
67.
68.     # --------------新增-------------------
69.     i = 0
70.     while i < stone_num:
71.         stone_y[i] += stone_y_speed[i]
72.         if stone_y[i] >= 600:
73.             stone_y.pop(i)
74.             stone_x.pop(i)
75.             stone_y_speed.pop(i)
76.             stone_num -= 1
77.         else:
78.             i += 1
79.     # -------------------------------------
80.
81.     gameDisplay.fill(WHITE)
82.     draw_rabbit(x, y)
83.     # --------------新增-------------------
84.     for i in range(stone_num):
85.         draw_stone(stone_x[i], stone_y[i])
86.     # -------------------------------------
87.
88.     pygame.display.update()
89.     clock.tick(60)
90.
91. pygame.quit()
92. quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top