Part 13: More sprites – Cannons and rockets together!

13_moving_rocket_walls

This is the next challenge, we shall use these cannons as something the rockets cannot collide against.

We already had a moving rocket turning 90 degrees on mouse clicks in Part 12, now we shall build on it and create cannons (circles).

Comment out the SCREEN filling and updating from “draw_updatescreen” function in the “Rocket” class. This we need to draw and update only once after the cannons are drawn, in the while loop.

def draw_updatescreen(self):
        #SCREEN.fill(BLACK)
        blitrocketrect = SCREEN.blit(self.image, (self.rect.x, self.rect.y))
        #pygame.display.update()

Comment out drawing the rocket immediately:

X = 40
Y = 40
SPRITENAME = 'rocket.png'
rocket = Rocket(SPRITENAME, X, Y)
#rocket.draw_updatescreen()

This blitting method is used since we have only one actor here. What if we have multiple actors? It would be beneficial to use a sprite group.
Let us try that though we have a single actor (rocket).

Below creating the rocket object, add the rocket to the group.

X = 40
Y = 40
SPRITENAME = 'rocket.png'
rocket = Rocket(SPRITENAME, X, Y)
rocket_group = pygame.sprite.Group()
rocket_group.add(rocket)

This is what we shall do for the cannons too, so pay attention, though it is an alternate method for the rocket.

Create a “Wall” class for the cannons. You can name the class “Cannons” too.

class Wall(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
                
        # Draw the circle
        COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
        circlerect = pygame.draw.circle(SCREEN, COLOR, (x,y), RADIUS)
        self.color = COLOR
        self.image = SCREEN.convert_alpha()
        self.rect = self.image.get_rect()
        self.circlerect = circlerect

The

SCREEN.convert_alpha()

is required, else it would not draw the circles. It converts the SCREEN such that the alpha transperancy factor is set. Hence it draws the circles on the SCREEN.

Do not add the display happening that should happen above the while loop as it was happening along with “draw_updatescreen” in Part 12

NOOFWALLS = 4
XMIN = 50
XMAX = SCREENWIDTH-100
YMIN = 50
YMAX = SCREENHEIGHT-100

wall_group=pygame.sprite.Group()
for i in range(0, NOOFWALLS):
    wall = Wall(random.randrange(XMIN, XMAX, 40), random.randrange(YMIN,YMAX, 20))
    wall_group.add(wall)

#Do it once
#wall_group.draw(SCREEN)
#pygame.display.update()

The cannons are added in the wall_group as done for the rocket. Need not draw here.

The two methods for redrawing after rotating the rocket on mouse press.

if (events.type == pygame.MOUSEBUTTONDOWN):
            rocket.rotate(ANGLE)
            #Method 1
            rocket.draw_updatescreen()
            #Method 2 - commented out
            #rocket_group.draw(SCREEN)

Now draw them in the while loop, after identifying the rocket movements.

  • Fill the SCREEN black to erase prior movements.
  • Draw the cannons in “wall_group”
  • Update the rockets position post WASD/arrow keys
  • Draw the rockets using “draw_updatescreen()”
  • Update the display
    SCREEN.fill(BLACK)
    wall_group.draw(SCREEN)
    
    rocket.update(X,Y)
    rocket.draw_updatescreen()
    #Method 2
    #rocket_group.draw(SCREEN)

    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


#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")

ANGLE = 90
BLACK = (0,0,0)

#The rocket - the actor/sprite that moves with arrows/WASD
class Rocket(pygame.sprite.Sprite):
    def __init__(self, image, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        #Horizontal
        #self.vertical = 1
    def draw_updatescreen(self):
        #Do it once
        #SCREEN.fill(BLACK)
        blitrocketrect = SCREEN.blit(self.image, (self.rect.x, self.rect.y))
        #pygame.display.update()
    def rotate(self, angle):
        self.image = pygame.transform.rotate(self.image, angle)
    def update(self, x, y):
        self.rect.x =  x
        self.rect.y =  y

#This group has only one sprite
X = 40
Y = 40
SPRITENAME = 'rocket.png'
rocket = Rocket(SPRITENAME, X, Y)

#uncomment - for drawing with sprite group
rocket_group = pygame.sprite.Group()
rocket_group.add(rocket)

#Do it once
#rocket.draw_updatescreen()
#rocket_group.draw(SCREEN)


ZEROINTENSITY = 0
MAXINTENSITY = 255
RADIUS = 20

#Getting the surface of the drawn circle and finding collisions is difficult
#hence will have to change it to an image
class Wall(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        #Not required - tried for collision
        #screensurf = pygame.Surface([SCREENWIDTH, SCREENHEIGHT])
        
        # Draw the circle
        COLOR = (random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY), random.randint(ZEROINTENSITY, MAXINTENSITY))
        circlerect = pygame.draw.circle(SCREEN, COLOR, (x,y), RADIUS)
        self.color = COLOR
        #Ignore as of now - for collision - not happening
        #self.image = pygame.Surface((x+20,y+20)).convert_alpha()

        #convert_alpha is required
        self.image = SCREEN.convert_alpha()
        self.rect = self.image.get_rect()
        self.circlerect = circlerect
        #blit - we don't need blitting here since we are using a group to draw
        #SCREEN.blit(self.image, circlerect)
        

NOOFWALLS = 4
XMIN = 50
XMAX = SCREENWIDTH-100
YMIN = 50
YMAX = SCREENHEIGHT-100

wall_group=pygame.sprite.Group()
for i in range(0, NOOFWALLS):
    wall = Wall(random.randrange(XMIN, XMAX, 40), random.randrange(YMIN,YMAX, 20))
    wall_group.add(wall)

#Do it once
#pygame.display.update()
#wall_group.draw(SCREEN)

while True:
    for events in pygame.event.get():
        if events.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if (events.type == pygame.MOUSEBUTTONDOWN):
            rocket.rotate(ANGLE)
            #Method 1
            #rocket.draw_updatescreen()
            #Method 2 - commented out
            rocket_group.draw(SCREEN)
            
    #Inside while, a single tab space from 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

    SCREEN.fill(BLACK)
    wall_group.draw(SCREEN)
    
    rocket.update(X,Y)
    #rocket.draw_updatescreen()
    #Method 2
    rocket_group.draw(SCREEN)
    
    pygame.display.update()
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