Part 10: Detect collision

10_collision

In this exercise, we collide the moving circle that we made in Part 8 with a wall (a rectangle). The moment, the circle touches the wall(rectangle), we shall print “Yay! Collision” on the interpreter shell as seen in the picture.


Draw a rectangle and update it on the SCREEN before the “while” loop.

#Draw a rectangle to collide with
RECTXPOS = 100
RECTYPOS = 100
RECTWIDTH = 100
RECTHEIGHT = 20

RECT = pygame.Rect(RECTXPOS,RECTYPOS, RECTWIDTH, RECTHEIGHT)
#Last parameter is zero by default
wallrect = pygame.draw.rect(SCREEN, COLOR, RECT)
pygame.display.update(wallrect)

Inside the “while” loop, since we are fill in the SCREEN “BLACK” to redraw the circle movements, we draw the rectangle and the moved circle repeatedly and check for collision with

circlerect.colliderect(wallrect)

Code:

SCREEN.fill(BLACK)
wallrect = pygame.draw.rect(SCREEN, COLOR, RECT)
circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
if(circlerect.colliderect(wallrect)):
     print("collision")
pygame.display.update()

Now for the entire code:

#Import statements are to enable the code to use the functions from the library
import pygame
import sys
import os
import random
from pygame.locals import *

#instructions to windows to center the game window in the center of
#the screen, which it might ignore
os.environ["SDL_VIDEO_CENTERED"] = "1"

#initialize pygame
pygame.init()

#Right way
SCREENWIDTH = 500
SCREENHEIGHT = 500
SCREENSIZE = [SCREENWIDTH, SCREENHEIGHT]
SCREEN = pygame.display.set_mode(SCREENSIZE)

#caption for the game
pygame.display.set_caption("My first game in pygame")

RADIUS = 20
ZEROINTENSITY = 0
MAXINTENSITY = 255

X = 40
Y = 40
CIRCLEPOS = (X, Y)
COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)

#Draw a rectangle to collide with
RECTXPOS = 100
RECTYPOS = 100
RECTWIDTH = 100
RECTHEIGHT = 20

RECT = pygame.Rect(RECTXPOS,RECTYPOS, RECTWIDTH, RECTHEIGHT)
#Last parameter is zero by default
wallrect = pygame.draw.rect(SCREEN, COLOR, RECT)

pygame.display.update(circlerect)
pygame.display.update(wallrect)

BLACK = (0,0,0)

while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        #Here for any commands inside the for loop
        #if (events.type == pygame.KEYDOWN) and (events.key == pygame.K_SPACE):
        if (events.type == pygame.MOUSEBUTTONDOWN):
            COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
            #note- we have skipped the last parameter and by default, 0 is taken
            circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
            pygame.display.update(circlerect)
    #beware of the positioning of this line. It should be inside the while
    #for all the commands that need to be executed inside the while

    user_input = pygame.key.get_pressed()
    
    if(user_input[pygame.K_UP]):
        Y=Y-1
        if(YSCREENHEIGHT):Y=0
    elif(user_input[pygame.K_LEFT]):
        X=X-1
        if(XSCREENWIDTH):X=0
    CIRCLEPOS = (X,Y)
    #exits the borders reposition

    SCREEN.fill(BLACK)
    wallrect = pygame.draw.rect(SCREEN, COLOR, RECT)
    circlerect = pygame.draw.circle(SCREEN, COLOR, CIRCLEPOS, RADIUS)
    if(circlerect.colliderect(wallrect)):
        print("collision")
    pygame.display.update()

The next challenge, if the squares are at different positions on the screen, detect collision for all.
Up to it? Let’s start!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s