Last active
December 8, 2016 22:14
-
-
Save Arxcis/3036ca97899dfe95447f63965f90f1e7 to your computer and use it in GitHub Desktop.
This is a suggestion for how to have 2d collision detection in PyGame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a suggestion to how to have 2d collision detection | |
# Inspired by Sentdex - part 20 intermediate Python tutorial | |
WIDTH = 600 | |
HEIGHT = 400 | |
blob_or_zero = [[ 0 for i in range(WIDTH) ] for i in range(HEIGHT) ] # Making a 2d blob-map, which has the same ratio as the screens pixel-map | |
blobs = [ Blob( random.x, random.y ) for i in range(20) ] # Initializing 20 blobs to random position | |
for blob in blobs: # Placing initialized blobs in blob-map | |
if not blob_or_zero[ blob.x ][ blob.y ]: # If there are no blobs already there | |
blob_or_zero [ blob.x ][ blob.y ] = blob | |
while running: # GAME LOOP | |
# Update blobs position and detects collision | |
for blob in blue_blobs: | |
blob_or_zero[ blob.x ][ blob.y ] = 0 # Zero blob-map x and y coordinate before the move | |
blob.move() # Move the blob to a new position x, y | |
if not blob_or_zero[ blob.x ][ blob.y ]: # If there are no blobs at the new position | |
blob_or_zero [ blob.x ][ blob.y ] = blob # Insert this blob to new position | |
else: | |
other_blob = blob_or_zero [ blob.x ][ blob.y ] # Get the other blob | |
if blob.type == 'blue' and other_blob.type == 'blue': # check if other blob is blue | |
pass | |
else: | |
blob + other_blob # Else merge/eat the other blob. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment