|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import pygame |
| 4 | + |
| 5 | +# --- constants --- |
| 6 | + |
| 7 | +W = 25 |
| 8 | +H = 25 |
| 9 | +M = 2 |
| 10 | + |
| 11 | +SIZE = (550, 500) |
| 12 | + |
| 13 | +BLACK = ( 0, 0, 0) |
| 14 | +WHITE = (255, 255, 255) |
| 15 | +RED = (255, 0, 0) |
| 16 | + |
| 17 | +FPS = 25 |
| 18 | + |
| 19 | +# --- classes --- |
| 20 | + |
| 21 | +class Player: |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + # to keep position and size |
| 25 | + self.rect = pygame.Rect(0, 0, 20, 20) |
| 26 | + |
| 27 | + # set start position |
| 28 | + self.rect.center = 149, 14 |
| 29 | + |
| 30 | + self.r = 10 |
| 31 | + |
| 32 | + self.speed_x = 0 |
| 33 | + self.speed_y = 0 |
| 34 | + |
| 35 | + self.row = 0 |
| 36 | + self.col = 0 |
| 37 | + |
| 38 | + current_time = pygame.time.get_ticks() |
| 39 | + self.next_move = current_time |
| 40 | + self.move_delay = 200 |
| 41 | + |
| 42 | + def convert(): |
| 43 | + self.rect.center = + self.col*W, M + self.row*H |
| 44 | + return |
| 45 | + |
| 46 | + def draw(self, screen): |
| 47 | + self.rect.topleft = 2+(W + M) * self.col + M, 2+((H + M) * self.row + M) |
| 48 | + pygame.draw.circle(screen, RED, self.rect.center, self.r) |
| 49 | + |
| 50 | + def handle_event(self, event): |
| 51 | + if event.type == pygame.KEYDOWN: |
| 52 | + |
| 53 | + if event.key == pygame.K_LEFT: |
| 54 | + self.speed_x -= 1 |
| 55 | + elif event.key == pygame.K_RIGHT: |
| 56 | + self.speed_x += 1 |
| 57 | + elif event.key == pygame.K_UP: |
| 58 | + self.speed_y -= 1 |
| 59 | + elif event.key == pygame.K_DOWN: |
| 60 | + self.speed_y += 1 |
| 61 | + elif event.key == pygame.K_LSHIFT: |
| 62 | + self.move_delay = 10 |
| 63 | + |
| 64 | + elif event.type == pygame.KEYUP: |
| 65 | + |
| 66 | + if event.key == pygame.K_LEFT: |
| 67 | + self.speed_x += 1 |
| 68 | + elif event.key == pygame.K_RIGHT: |
| 69 | + self.speed_x -= 1 |
| 70 | + elif event.key == pygame.K_UP: |
| 71 | + self.speed_y += 1 |
| 72 | + elif event.key == pygame.K_DOWN: |
| 73 | + self.speed_y -= 1 |
| 74 | + elif event.key == pygame.K_LSHIFT: |
| 75 | + self.move_delay = 200 |
| 76 | + |
| 77 | + def update(self): |
| 78 | + current_time = pygame.time.get_ticks() |
| 79 | + |
| 80 | + if current_time >= self.next_move: |
| 81 | + |
| 82 | + self.next_move = current_time + self.move_delay |
| 83 | + |
| 84 | + print(self.speed_x, self.speed_y) |
| 85 | + |
| 86 | + # create copy of position |
| 87 | + newrect = self.rect.copy() |
| 88 | + |
| 89 | + newrect.x += self.speed_x * 27 |
| 90 | + |
| 91 | + # check if "copy" is still in rectangles |
| 92 | + for rectangle in all_rectangles: |
| 93 | + if newrect.colliderect(rectangle): |
| 94 | + # now you can set new position |
| 95 | + self.rect = newrect |
| 96 | + self.col += self.speed_x |
| 97 | + # don't check other rectangles |
| 98 | + break |
| 99 | + |
| 100 | + # create copy of position |
| 101 | + newrect = self.rect.copy() |
| 102 | + |
| 103 | + newrect.y += self.speed_y * 27 |
| 104 | + |
| 105 | + # check if "copy" is still in rectangles |
| 106 | + for rectangle in all_rectangles: |
| 107 | + if newrect.colliderect(rectangle): |
| 108 | + # now you can set new position |
| 109 | + self.rect = newrect |
| 110 | + self.row += self.speed_y |
| 111 | + # don't check other rectangles |
| 112 | + break |
| 113 | + |
| 114 | +# --- main --- |
| 115 | + |
| 116 | +# - init - |
| 117 | + |
| 118 | +pygame.init() |
| 119 | + |
| 120 | +screen = pygame.display.set_mode(SIZE) |
| 121 | +screen_rect = screen.get_rect() |
| 122 | + |
| 123 | +# - objects - |
| 124 | + |
| 125 | +player = Player() |
| 126 | + |
| 127 | +# create list with rectangles (but not draw them) |
| 128 | + |
| 129 | +map = [ |
| 130 | + "######## #######", |
| 131 | + "# #### #", |
| 132 | + "# # # #", |
| 133 | + "# ######## #", |
| 134 | + "###### # #", |
| 135 | + " # #####", |
| 136 | + " # # ", |
| 137 | + " ############ ", |
| 138 | + " ############ ", |
| 139 | + " ############ ", |
| 140 | + " ############ ", |
| 141 | +] |
| 142 | + |
| 143 | +all_rectangles = [] |
| 144 | + |
| 145 | +for r, row in enumerate(map): |
| 146 | + for c, item in enumerate(row): |
| 147 | + if item == '#': |
| 148 | + all_rectangles.append(pygame.Rect((W + M) * c + M, ((H + M) * r + M), W, H)) |
| 149 | + |
| 150 | +# - mainloop - |
| 151 | + |
| 152 | +clock = pygame.time.Clock() |
| 153 | +running = True |
| 154 | + |
| 155 | +while running: |
| 156 | + |
| 157 | + # - events (without draws) - |
| 158 | + |
| 159 | + for event in pygame.event.get(): |
| 160 | + if event.type == pygame.QUIT: |
| 161 | + running = False |
| 162 | + |
| 163 | + if event.type == pygame.KEYDOWN: |
| 164 | + if event.key == pygame.K_ESCAPE: |
| 165 | + running = False |
| 166 | + |
| 167 | + player.handle_event(event) |
| 168 | + |
| 169 | + # - updates (without draws) - |
| 170 | + |
| 171 | + player.update() |
| 172 | + # - draws (everything in one place) - |
| 173 | + |
| 174 | + screen.fill(WHITE) |
| 175 | + |
| 176 | + for rectangle in all_rectangles: |
| 177 | + pygame.draw.rect(screen, BLACK, rectangle, 1) |
| 178 | + |
| 179 | + player.draw(screen) |
| 180 | + |
| 181 | + pygame.display.flip() |
| 182 | + |
| 183 | + # - FPS - keep the same speed on all computers - |
| 184 | + |
| 185 | + clock.tick(FPS) |
| 186 | + |
| 187 | +# - end - |
| 188 | + |
| 189 | +pygame.quit() |
0 commit comments