SHOW:
|
|
- or go back to the newest paste.
1 | import pygame | |
2 | import random | |
3 | ||
4 | pygame.init() | |
5 | ||
6 | color = { | |
7 | 'white': (255, 255, 255), | |
8 | 'red': (255, 0, 0), | |
9 | 'green': (0, 255, 0), | |
10 | 'blue': (0, 0, 255), | |
11 | 'black': (0, 0, 0) | |
12 | } | |
13 | ||
14 | clock = pygame.time.Clock() | |
15 | ||
16 | screen_width = 600 | |
17 | screen_height = 400 | |
18 | ||
19 | gameDisplay = pygame.display.set_mode((screen_width, screen_height)) | |
20 | ||
21 | block_size = 20 | |
22 | ||
23 | font = pygame.font.SysFont(None, 25) | |
24 | ||
25 | def message_to_screen(msg, color): | |
26 | screen_text = font.render(msg, True, color) | |
27 | font_width,font_height = font.size(msg) | |
28 | gameDisplay.blit(screen_text, [(screen_width/2) - (font_width/2), (screen_height/2)-(font_height/2)]) | |
29 | ||
30 | ||
31 | def gameLoop(): | |
32 | gameExit = False | |
33 | gameOver = False | |
34 | gamePause = False | |
35 | ||
36 | lead_x = screen_width/2 | |
37 | lead_y = screen_height/2 | |
38 | ||
39 | lead_x_change = 0 | |
40 | lead_y_change = 0 | |
41 | ||
42 | randAppleX = random.randrange(block_size, screen_width-block_size, block_size) | |
43 | randAppleY = random.randrange(block_size, screen_height-block_size, block_size) | |
44 | ||
45 | resume = False | |
46 | ||
47 | while not gameExit: | |
48 | ||
49 | while gamePause == True: | |
50 | if resume == True: | |
51 | gamePause = False | |
52 | resume = False | |
53 | print("sup") | |
54 | break | |
55 | message_to_screen("Your game has been paused, press p to resume", color['blue']) | |
56 | pygame.display.update() | |
57 | for event in pygame.event.get(): | |
58 | if event.type == pygame.KEYDOWN: | |
59 | if event.key == pygame.K_p: | |
60 | resume = True | |
61 | if event.type == pygame.QUIT: | |
62 | gameExit = True | |
63 | resume = True | |
64 | ||
65 | while gameOver == True: | |
66 | ||
67 | gameDisplay.fill(color['red']) | |
68 | message_to_screen("You have lost, press C to play again or Q to quit", color['blue']) | |
69 | pygame.display.update() | |
70 | ||
71 | for event in pygame.event.get(): | |
72 | if event.type == pygame.KEYDOWN: | |
73 | if event.key == pygame.K_q: | |
74 | gameExit = True | |
75 | gameOver = False | |
76 | elif event.key == pygame.K_c: | |
77 | - | gameLoop() |
77 | + | # gameLoop() # removed |
78 | gameExit = False | |
79 | gameOver = False | |
80 | gamePause = False | |
81 | ||
82 | lead_x = screen_width/2 | |
83 | lead_y = screen_height/2 | |
84 | ||
85 | lead_x_change = 0 | |
86 | lead_y_change = 0 | |
87 | ||
88 | randAppleX = random.randrange(block_size, screen_width-block_size, block_size) | |
89 | randAppleY = random.randrange(block_size, screen_height-block_size, block_size) | |
90 | ||
91 | resume = False | |
92 | ||
93 | if event.type == pygame.QUIT: | |
94 | gameExit = True | |
95 | ||
96 | ||
97 | if gameExit == True: | |
98 | break | |
99 | ||
100 | for event in pygame.event.get(): | |
101 | ||
102 | if event.type == pygame.QUIT: | |
103 | gameExit = True | |
104 | ||
105 | if event.type == pygame.KEYDOWN: | |
106 | ||
107 | if event.key == pygame.K_RIGHT or event.key == pygame.K_d: | |
108 | lead_y_change = 0 | |
109 | lead_x_change = block_size | |
110 | ||
111 | elif event.key == pygame.K_LEFT or event.key == pygame.K_a: | |
112 | lead_y_change = 0 | |
113 | lead_x_change = -block_size | |
114 | ||
115 | elif event.key == pygame.K_UP or event.key == pygame.K_w: | |
116 | lead_x_change = 0 | |
117 | lead_y_change = -block_size | |
118 | ||
119 | elif event.key == pygame.K_DOWN or event.key == pygame.K_s: | |
120 | lead_x_change = 0 | |
121 | lead_y_change = block_size | |
122 | ||
123 | elif event.key == pygame.K_p: | |
124 | gamePause = True | |
125 | ||
126 | lead_x += lead_x_change | |
127 | lead_y += lead_y_change | |
128 | ||
129 | if lead_x + block_size >= screen_width or lead_x <= 0 or lead_y + block_size >= screen_height or lead_y <= 0: | |
130 | ||
131 | gameOver = True | |
132 | ||
133 | if lead_x == randAppleX and lead_y == randAppleY: | |
134 | ||
135 | randAppleX = random.randrange(block_size, screen_width-block_size, block_size) | |
136 | randAppleY = random.randrange(block_size, screen_height-block_size, block_size) | |
137 | ||
138 | pygame.display.set_caption('gameDev') | |
139 | gameDisplay.fill(color['white']) | |
140 | gameDisplay.fill(color['green'], rect=[randAppleX, randAppleY, block_size, block_size]) | |
141 | gameDisplay.fill(color['red'], rect=[lead_x, lead_y, block_size, block_size]) | |
142 | pygame.display.update() | |
143 | ||
144 | clock.tick(6) | |
145 | ||
146 | gameLoop() | |
147 | pygame.quit() | |
148 | quit() |