Flowers

Tags: pgzero, game

https://simplegametutorials.github.io/pygamezero/

flowers.py
  1import pgzrun, pygame
  2import random
  3
  4
  5
  6GAME_Y = 14
  7GAME_X = 19
  8CELL_SIZE = 18
  9HUD = 36
 10NUMBER_OF_FLOWERS = 40
 11WIDTH = GAME_X * CELL_SIZE
 12HEIGHT = GAME_Y * CELL_SIZE + HUD
 13
 14
 15def get_surrounding_flower_count(x, y): # {{{
 16    surrounding_flower_count = 0
 17
 18    for dy in range(-1, 2):
 19        for dx in range(-1, 2):
 20            if (
 21                    not (dy == 0 and dx == 0)
 22                    and 0 <= (y+dy) < len(grid)
 23                    and 0 <= (x+dx) < len(grid[y+dy])
 24                    and grid[y+dy][x+dx]['flower']
 25                    ): 
 26                surrounding_flower_count +=1
 27    return surrounding_flower_count
 28# }}}
 29
 30def update(): # {{{
 31    global selected_x, selected_y
 32
 33    mouse_x, mouse_y = pygame.mouse.get_pos()
 34    selected_x = mouse_x // CELL_SIZE
 35    selected_y = (mouse_y - HUD) // CELL_SIZE
 36    selected_x = clamp(selected_x, 0, GAME_X-1)
 37    selected_y = clamp(selected_y, 0, GAME_Y-1)
 38
 39    # }}}
 40def draw(): # {{{
 41    screen.fill((0, 0, 0))
 42    for y in range(GAME_Y):
 43        for x in range(GAME_X):
 44            if grid[y][x]['state'] == 'uncovered':
 45                draw_cell('uncovered', x, y)
 46            else:
 47                if x == selected_x and y == selected_y and not game_over:
 48                    if pygame.mouse.get_pressed()[0] == 1:
 49                        if grid[y][x]['state'] == 'flag':
 50                            image = 'covered'
 51                        else:
 52                            image = 'uncovered'
 53                    else:
 54                        image = 'covered_highlighted'
 55                else:
 56                    image = 'covered'
 57                draw_cell(image, x, y)
 58
 59            surrounding_flower_count = get_surrounding_flower_count(x, y)
 60
 61
 62            if grid[y][x]['flower'] and game_over:
 63                draw_cell('flower', x, y)
 64            elif surrounding_flower_count > 0 and grid[y][x]['state'] == 'uncovered':
 65                draw_cell('%s' % surrounding_flower_count, x, y)
 66
 67
 68            if grid[y][x]['state'] == 'flag':
 69                draw_cell('flag', x, y)
 70            if grid[y][x]['state'] == 'question':
 71                draw_cell('question', x, y)
 72
 73    screen.draw.text("x: %d, y: %d" % (selected_x, selected_y), (2, 2), color='orange')
 74    # }}}
 75def on_mouse_up(button): # {{{
 76    global game_over
 77    global first_click
 78    
 79    if not game_over:
 80        if button == mouse.LEFT and grid[selected_y][selected_x]['state'] != 'flag':
 81            if first_click: first_click = False
 82            if grid[selected_y][selected_x]['flower']:
 83                grid[selected_y][selected_x]['state'] = 'uncovered'
 84                game_over = True
 85            else:
 86                stack = [{
 87                 'x': selected_x,
 88                 'y': selected_y,
 89                 }]
 90
 91                while len(stack) > 0:
 92                    current = stack.pop()
 93                    x = current['x']
 94                    y = current['y']
 95                    grid[y][x]['state'] = 'uncovered'
 96
 97                    if get_surrounding_flower_count(x, y) == 0:
 98                        for dy in range(-1, 2):
 99                            for dx in range(-1, 2):
100                                if (
101                                        not (dy == 0 and dx == 0)
102                                        and 0 <= (y + dy) < GAME_Y
103                                        and 0 <= (x + dx) < GAME_X
104                                        and grid[y+dy][x+dx]['state'] in ('covered', 'question')
105                                        ):
106                                    stack.append({
107                                        'x': x + dx,
108                                        'y': y + dy,
109                                        })
110                complete = True
111
112                for y in range(GAME_Y):
113                    for x in range(GAME_X):
114                        if grid[y][x]['state'] != 'uncovered' and not grid[y][x]['flower']:
115                            complete = False
116
117                if complete: game_over = True
118    else:
119        reset()
120
121    if button == mouse.RIGHT:
122        # flower_state = grid[selected_y][selected_x]['flower']
123        # grid[selected_y][selected_x]['flower'] = not flower_state
124
125        if grid[selected_y][selected_x]['state'] == 'covered':
126            grid[selected_y][selected_x]['state']  = 'flag'
127
128        elif grid[selected_y][selected_x]['state'] == 'flag':
129            grid[selected_y][selected_x]['state']  = 'question'
130
131        elif grid[selected_y][selected_x]['state'] == 'question':
132            grid[selected_y][selected_x]['state']  = 'covered'
133
134
135        # }}}
136def reset(): #{{{
137    global grid
138    global game_over
139    global first_click
140
141    first_click = True
142    game_over = False
143    grid = []
144    possible_flower_positions = []
145
146    for y in range(GAME_Y):
147        grid.append([])
148        for x in range(GAME_X):
149            grid[y].append({
150                'flower': False,
151                'state': 'covered',
152                })
153
154
155    for y in range(GAME_Y):
156        for x in range(GAME_X):
157            # if not (x == selected_x and y == selected_y):
158            possible_flower_positions.append({'x': x, 'y': y})
159
160
161    for i in range(NUMBER_OF_FLOWERS):
162        position = possible_flower_positions.pop(random.randrange(len(possible_flower_positions)))
163        grid[position['y']][position['x']]['flower'] = True
164        # }}}
165def clamp(val, minv, maxv): return minv if val < minv else maxv if val > maxv else val
166def draw_cell(image, x, y): screen.blit(image, (x*CELL_SIZE, y*CELL_SIZE+HUD))
167def on_key_down(key): reset()
168
169reset()
170pgzrun.go()