|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# http://stackoverflow.com/questions/20180594/pygame-collision-by-sides-of-sprite |
| 4 | +# https://pl.python.org/forum/index.php?topic=5727.msg24549#msg24549 |
| 5 | + |
| 6 | +import pygame |
| 7 | + |
| 8 | +WHITE = (255,255,255) |
| 9 | +BLACK = ( 0, 0, 0) |
| 10 | +RED = (255, 0, 0) |
| 11 | +GREEN = ( 0,255, 0) |
| 12 | +BLUE = ( 0, 0,255) |
| 13 | + |
| 14 | +GREY = (128,128,128) |
| 15 | + |
| 16 | +RED_1 = (128, 0, 0) |
| 17 | +GREEN_1 = ( 0,128, 0) |
| 18 | + |
| 19 | +class Player(): |
| 20 | + |
| 21 | + def __init__(self, x=0, y=0, width=150, height=150): |
| 22 | + |
| 23 | + self.rect = pygame.Rect(x, y, width, height) |
| 24 | + |
| 25 | + self.rect_75 = pygame.Rect(x, y, width*0.75, height*0.75) |
| 26 | + self.rect_75.center=self.rect.center |
| 27 | + self.rect_50 = pygame.Rect(x, y, width*0.5, height*0.5) |
| 28 | + self.rect_50.center=self.rect.center |
| 29 | + |
| 30 | + self.speed_x = 5 |
| 31 | + self.speed_y = 5 |
| 32 | + |
| 33 | + self.move_x = 0 |
| 34 | + self.move_y = 0 |
| 35 | + |
| 36 | + self.collision = [False] * 9 |
| 37 | + |
| 38 | + self.font = pygame.font.SysFont("", 32) |
| 39 | + self.text = ""; |
| 40 | + |
| 41 | + def set_center(self, screen): |
| 42 | + self.rect.center = screen.get_rect().center |
| 43 | + |
| 44 | + def event_handler(self, event): |
| 45 | + if event.type == pygame.KEYDOWN: |
| 46 | + if event.key == pygame.K_LEFT: |
| 47 | + self.move_x -= self.speed_x |
| 48 | + elif event.key == pygame.K_RIGHT: |
| 49 | + self.move_x += self.speed_x |
| 50 | + elif event.key == pygame.K_UP: |
| 51 | + self.move_y -= self.speed_y |
| 52 | + elif event.key == pygame.K_DOWN: |
| 53 | + self.move_y += self.speed_y |
| 54 | + |
| 55 | + elif event.type == pygame.KEYUP: |
| 56 | + if event.key == pygame.K_LEFT: |
| 57 | + self.move_x += self.speed_x |
| 58 | + elif event.key == pygame.K_RIGHT: |
| 59 | + self.move_x -= self.speed_x |
| 60 | + elif event.key == pygame.K_UP: |
| 61 | + self.move_y += self.speed_y |
| 62 | + elif event.key == pygame.K_DOWN: |
| 63 | + self.move_y -= self.speed_y |
| 64 | + |
| 65 | + def update(self): |
| 66 | + self.rect.x += self.move_x |
| 67 | + self.rect.y += self.move_y |
| 68 | + self.rect_75.center=self.rect.center |
| 69 | + self.rect_50.center=self.rect.center |
| 70 | + |
| 71 | + def draw(self, screen): |
| 72 | + |
| 73 | + pygame.draw.rect(screen, RED_1, self.rect_75, 1) |
| 74 | + pygame.draw.rect(screen, GREEN_1, self.rect_50, 1) |
| 75 | + pygame.draw.circle(screen, GREY, self.rect.center, self.rect.width/2, 1) |
| 76 | + |
| 77 | + pygame.draw.rect(screen, WHITE, self.rect, 2) |
| 78 | + self.draw_point(screen, self.rect.topleft, self.collision[0]) |
| 79 | + self.draw_point(screen, self.rect.topright, self.collision[1]) |
| 80 | + self.draw_point(screen, self.rect.bottomleft, self.collision[2]) |
| 81 | + self.draw_point(screen, self.rect.bottomright, self.collision[3]) |
| 82 | + |
| 83 | + self.draw_point(screen, self.rect.midleft, self.collision[4]) |
| 84 | + self.draw_point(screen, self.rect.midright, self.collision[5]) |
| 85 | + self.draw_point(screen, self.rect.midtop, self.collision[6]) |
| 86 | + self.draw_point(screen, self.rect.midbottom, self.collision[7]) |
| 87 | + |
| 88 | + self.draw_point(screen, self.rect.center, self.collision[8]) |
| 89 | + |
| 90 | + |
| 91 | + def draw_point(self, screen, pos, collision): |
| 92 | + if not collision: |
| 93 | + pygame.draw.circle(screen, GREEN, pos, 5) |
| 94 | + else: |
| 95 | + pygame.draw.circle(screen, RED, pos, 5) |
| 96 | + |
| 97 | + def check_collision(self, rect): |
| 98 | + self.collision[0] = rect.collidepoint(self.rect.topleft) |
| 99 | + self.collision[1] = rect.collidepoint(self.rect.topright) |
| 100 | + self.collision[2] = rect.collidepoint(self.rect.bottomleft) |
| 101 | + self.collision[3] = rect.collidepoint(self.rect.bottomright) |
| 102 | + |
| 103 | + self.collision[4] = rect.collidepoint(self.rect.midleft) |
| 104 | + self.collision[5] = rect.collidepoint(self.rect.midright) |
| 105 | + self.collision[6] = rect.collidepoint(self.rect.midtop) |
| 106 | + self.collision[7] = rect.collidepoint(self.rect.midbottom) |
| 107 | + |
| 108 | + self.collision[8] = rect.collidepoint(self.rect.center) |
| 109 | + |
| 110 | + def render_collision_info(self): |
| 111 | + |
| 112 | + text = "collision: " |
| 113 | + #print "collision:", |
| 114 | + |
| 115 | + if self.collision[0] or self.collision[2] or self.collision[4]: |
| 116 | + text += "left " |
| 117 | + #print "left", |
| 118 | + |
| 119 | + if self.collision[1] or self.collision[3] or self.collision[5]: |
| 120 | + text += "right " |
| 121 | + #print "right", |
| 122 | + |
| 123 | + if self.collision[0] or self.collision[1] or self.collision[6]: |
| 124 | + text += "top " |
| 125 | + #print "top", |
| 126 | + |
| 127 | + if self.collision[2] or self.collision[3] or self.collision[7]: |
| 128 | + text += "bottom " |
| 129 | + #print "bottom", |
| 130 | + |
| 131 | + if self.collision[8]: |
| 132 | + text += "center " |
| 133 | + #print "center", |
| 134 | + |
| 135 | + #print |
| 136 | + |
| 137 | + self.text = self.font.render(text, 1, WHITE) |
| 138 | + |
| 139 | + def draw_collision_info(self, screen, pos): |
| 140 | + screen.blit(self.text, pos) |
| 141 | + |
| 142 | +#---------------------------------------------------------------------- |
| 143 | + |
| 144 | +class Game(): |
| 145 | + |
| 146 | + def __init__(self): |
| 147 | + |
| 148 | + pygame.init() |
| 149 | + |
| 150 | + self.screen = pygame.display.set_mode( (800,600) ) |
| 151 | + pygame.display.set_caption("Side Collision") |
| 152 | + |
| 153 | + self.player = Player() |
| 154 | + self.enemy = Player() |
| 155 | + self.enemy.set_center(self.screen) |
| 156 | + |
| 157 | + self.font = pygame.font.SysFont("", 32) |
| 158 | + self.text = '' |
| 159 | + |
| 160 | + |
| 161 | + def check_collisions(self): |
| 162 | + |
| 163 | + text = 'collisions: ' |
| 164 | + |
| 165 | + if pygame.sprite.collide_rect(self.player, self.enemy): |
| 166 | + text += 'rect' |
| 167 | + |
| 168 | + if pygame.sprite.collide_rect_ratio(0.75)(self.player, self.enemy): |
| 169 | + text += ', rect_ratio(0.75)' |
| 170 | + |
| 171 | + if pygame.sprite.collide_rect_ratio(0.5)(self.player, self.enemy): |
| 172 | + text += ', rect_ratio(0.50)' |
| 173 | + |
| 174 | + if pygame.sprite.collide_circle(self.player, self.enemy): |
| 175 | + text += ', circle' |
| 176 | + |
| 177 | + self.text = self.font.render(text, 1, WHITE) |
| 178 | + |
| 179 | + |
| 180 | + def draw_collisions_info(self, screen, pos): |
| 181 | + screen.blit(self.text, pos) |
| 182 | + |
| 183 | + |
| 184 | + def run(self): |
| 185 | + clock = pygame.time.Clock() |
| 186 | + |
| 187 | + RUNNING = True |
| 188 | + |
| 189 | + while RUNNING: |
| 190 | + |
| 191 | + # --- events ---- |
| 192 | + |
| 193 | + for event in pygame.event.get(): |
| 194 | + if event.type == pygame.QUIT: |
| 195 | + RUNNING = False |
| 196 | + |
| 197 | + elif event.type == pygame.KEYDOWN: |
| 198 | + if event.key == pygame.K_ESCAPE: |
| 199 | + RUNNING = False |
| 200 | + |
| 201 | + self.player.event_handler(event) |
| 202 | + |
| 203 | + # --- updates --- |
| 204 | + |
| 205 | + self.player.update() |
| 206 | + self.enemy.update() |
| 207 | + |
| 208 | + self.player.check_collision(self.enemy.rect) |
| 209 | + self.enemy.check_collision(self.player.rect) |
| 210 | + self.player.render_collision_info() |
| 211 | + self.enemy.render_collision_info() |
| 212 | + |
| 213 | + self.check_collisions() |
| 214 | + |
| 215 | + # --- draws ---- |
| 216 | + |
| 217 | + self.screen.fill(BLACK) |
| 218 | + |
| 219 | + self.player.draw(self.screen) |
| 220 | + self.enemy.draw(self.screen) |
| 221 | + |
| 222 | + self.player.draw_collision_info(self.screen, (0,0)) |
| 223 | + self.enemy.draw_collision_info(self.screen, (0,32)) |
| 224 | + |
| 225 | + self.draw_collisions_info(self.screen, (0, 600-32)) |
| 226 | + |
| 227 | + pygame.display.update() |
| 228 | + |
| 229 | + # --- FPS --- |
| 230 | + |
| 231 | + clock.tick(30) |
| 232 | + |
| 233 | + pygame.quit() |
| 234 | + |
| 235 | +#---------------------------------------------------------------------- |
| 236 | + |
| 237 | +Game().run() |
0 commit comments