Strange blit() question.

Strange blit() question.

Postby Donald8274 » Sat Jun 08, 2013 12:38 pm

I know there is probably a way to rid of this, however, I am trying to "unblit" something on the screen. I have it so it's constantly on the screen but when I shoot it, I would like it to delete itself in some sorts. Along the lines of this question, I will be having more than 1 of the same instance on the screen and would this method probably delete all of them at once? Thank you for any feedback.
Donald8274
 
Posts: 9
Joined: Thu Jun 06, 2013 8:00 am

Re: Strange blit() question.

Postby Donald8274 » Sat Jun 08, 2013 12:45 pm

Could I possibly add them to an array using array_blit() and then once a certain instance is defeated their instance will be deleted from the array? That would take me some time if what I think is correct because I'm not very good at utilizing arrays.
Donald8274
 
Posts: 9
Joined: Thu Jun 06, 2013 8:00 am

Re: Strange blit() question.

Postby DrakeMagi » Sat Jun 08, 2013 12:59 pm

pygame is made to redraw every scene.

so simple clear the scene.
Code: Select all
surface.fill((0,0,0)) # background color
# then rest of draw code

or have a background image.

example
Code: Select all
import os
import pygame
pygame.init()
os.environ['SDL_VIDEO_CENTERED'] = '1'

if __name__ == '__main__':
   screen = pygame.display.set_mode((800, 600))
   pygame.display.set_caption('Testing')
   clock = pygame.time.Clock()
   
   rect = pygame.Rect(20,20,40,40)
   xdir = 2
   
   running = True
   while running:
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            running = False
         elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
               running = False
      
      
      rect.move_ip((xdir,0))
      if rect.right > 780:
         xdir = -xdir
      if rect.left < 20:
         xdir = -xdir
      screen.fill((0,0,0))
      
      pygame.draw.rect(screen, (0,0,100), rect)
      
      pygame.display.flip()
      clock.tick(60)
      
   pygame.quit()
DrakeMagi
 
Posts: 82
Joined: Sun May 12, 2013 8:36 pm

Re: Strange blit() question.

Postby Donald8274 » Sat Jun 08, 2013 1:01 pm

Here's the code so maybe you can see it better.

Code: Select all
import pygame, sys
from pygame import key

pygame.init()

Screen_Width, Screen_Height = 350, 220

Px = Screen_Width/2
Py = Screen_Height-16
pbl = 0 #Player button left
pbr = 0 #Player button Right
plb = 0 #Last button pressed
Ix = 30
Iy = 30

shooting = 0
black = (0, 0, 0)
white = (255, 255, 255)

window = pygame.display.set_mode((Screen_Width, Screen_Height), 0, 32)
pygame.display.set_caption('Space Invaders')
shooter_load = pygame.image.load('Shooter.png').convert()
shooter = shooter_load.get_rect()
invader1_load = pygame.image.load('Invader1.png').convert()
invader1 = invader1_load.get_rect()

clock = pygame.time.Clock()

gameLoop = True
while gameLoop:

    for event in pygame.event.get():

        if (event.type == pygame.QUIT):

            gameLoop = False
            sys.exit()
           
        if (event.type == pygame.KEYDOWN): #Keys down
            if (Px >= 0):
                if (event.key == pygame.K_LEFT):
                    pbl = 1
                    plb = 1
                   
            if (Px <= Screen_Width - 24):
                if (event.key == pygame.K_RIGHT):
                    pbr = 1
                    plb = 2

            if (event.key == pygame.K_SPACE):
                if shooting == 0:
                    Bx = Px #Bullet x Value
                    By = Py #Bullet y Value
                    shooting = 1
               
        if (event.type == pygame.KEYUP): #Keys up
            if (event.key == pygame.K_LEFT):
                pbl = 0
                if pbr == 1: plb = 2
                else: plb = 0

            if (event.key == pygame.K_RIGHT):
                pbr = 0
                if pbl == 1: plb = 1
                else: plb = 0

    window.fill(black) #Refreshes each frame

    clock.tick(60) #60 FPS
   
   
    #Player movements

    if plb == 1: Px -= 3
    elif plb == 2: Px += 3

    #Block player from going past the boundaries

    if Px <= 0 or Px >= Screen_Width - 24:
        plb = 0

    if Px < 0: Px = 0
    if Px > Screen_Width - 24: Px = Screen_Width - 24

    if shooting == 1:
        bullet = pygame.Rect(Bx + 11, By, 2.5, 8)
        By -= 5
        pygame.draw.rect(window, (white), bullet)
        if By <= -50:
            shooting = 0
        if bullet.colliderect(invader1):
            print 'yes'
            invader1.mask.Mask.clear()
           

    window.blit(invader1_load,(Ix, Iy))
    window.blit(shooter_load,(Px, Py))
    pygame.display.flip()
Donald8274
 
Posts: 9
Joined: Thu Jun 06, 2013 8:00 am

Re: Strange blit() question.

Postby DrakeMagi » Sat Jun 08, 2013 1:41 pm

your code is a little messy but.

use invader1_alive = True
then turn it False

other problem you didn't move invader1 rect.
don't use variables and rects for position. just use rects
Code: Select all
invader1 = invader1_load.get_rect()
invader1.move_ip((30,30)) # equals invader1 = invader1.move((30,30))

Code: Select all
if invader1_alive:
    window.blit(invader1_load,invader1.topleft)
DrakeMagi
 
Posts: 82
Joined: Sun May 12, 2013 8:36 pm

Re: Strange blit() question.

Postby Donald8274 » Sat Jun 08, 2013 2:00 pm

Wouldn't that destroy them all?
Donald8274
 
Posts: 9
Joined: Thu Jun 06, 2013 8:00 am

Re: Strange blit() question.

Postby DrakeMagi » Sat Jun 08, 2013 2:22 pm

until you handle more then 1 invader.
there noway figure out how to handle this invisible code.

but there many ways to handle many sprites

A list of list # [rect, alive]
a list of dict # {'rect':rect, 'alive':alive}
a list of classes
Code: Select all
class InvaderObject(object):
    def __init__(self, rect, alive):
        self.rect = rect
        self.alive = alive

    def Blit(self, surface):
        if self.alive:
            surface.blit(Invader_image, self.rect.topleft)
DrakeMagi
 
Posts: 82
Joined: Sun May 12, 2013 8:36 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest