Python Forum

Full Version: Help with blitting and events
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So I have an assignment that is giving me some trouble. A lot of the code is given by the professor and we have to go in and complete the assignment. What I am attempting to do is have the Bomb image populate when the mouse clicks. I can get pygame to load the bomb image, but that is without clicking the mouse. I had an idea to create a different loop specifically for the mouse click event, but was unsure how to implement that within the game loop. I am a beginner with coding and any/all insight would be appreciated.
import pygame, sys
pygame.init()
#direction constants
PA_FOLD = 0
PA_PITCH_DOWN = 1
PA_PITCH_UP = 2
PA_ROLL_LEFT = 3
PA_ROLL_RIGHT = 4
PA_FLY = 5

screen = pygame.display.set_mode((640, 480))
mouse = pygame.mouse.get_pos()
class Plane(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.PA_FOLD = 0
        self.PA_ROLL_LEFT = 1
        self.PA_ROLL_RIGHT = 2
        self.PA_FLY_STRAIGHT = 3

        self.dir = self.PA_FLY_STRAIGHT

        self.image = pygame.image.load("images/pa_fold0009.jpg")
        self.image = self.image.convert()
        tranColor = self.image.get_at((1, 1))
        self.image.set_colorkey(tranColor)
        self.rect = self.image.get_rect()
        self.rect.center = (320, 240)
        

        self.speed = 10
        self.dx = 0
        self.dy = 0
    def update(self):
        mousex, mousey = pygame.mouse.get_pos()
        self.rect.center = (mousex, mousey)
class Bomb(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/new bomb.jpg")
        self.image = self.image.convert()
        self.rect = self.image.get_rect()
        pygame.Surface = (50, 40)        #wanted to give the bomb a surface for blitting
    def draw(self, screen):
        
        self.blit = (self.image, self.rect)     #is this correct for blitting?
        
class Ocean(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/ocean.gif")
        self.rect = self.image.get_rect()
        self.dy = 5
def game():
        pygame.display.set_caption("Paper Air Force")

        background = pygame.Surface(screen.get_size())
        background.fill((0, 0, 0))
        screen.blit(background, (0, 0))
        plane = Plane()
        ocean = Ocean()
        bomb = Bomb()
        

        pygame.key.set_repeat(1,1)

        oceanSprites = pygame.sprite.Group(ocean)
        planeSprites = pygame.sprite.Group(plane)
        bombSprites = pygame.sprite.Group(bomb)

        clock = pygame.time.Clock()
        keepGoing = True
        while keepGoing:
            clock.tick(30)
            pygame.mouse.set_visible(False)
            for event in pygame.event.get():
                if event == pygame.MOUSEBUTTONDOWN:       #I've had this event below the quit event
                    
                    bombSprites.draw(screen)
                    bombSprites.update()
                    pygame.display.update()
                    return
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            keepGoing = False
                            pygame.display.quit()
                            pygame.quit()
                            sys.exit()
                
                
            oceanSprites.draw(screen)
                                               #bomb draw here but loads as soon as code is ran not with
            planeSprites.draw(screen)           mouse click
              
            oceanSprites.update()
            bombSprites.update()
            planeSprites.update()
            
            pygame.display.flip()
               
            
            #plane.sndEngine.stop()
            #return mouse cursor
        pygame.mouse.set_visible(True)
        return scoreboard.score
                    
        
            
if __name__ == "__main__":
    game()
    Bomb()                    #thought to have Bomb class init with game init
(Nov-04-2018, 09:04 PM)elxleon42 Wrote: [ -> ]What I am attempting to do is have the Bomb image populate when the mouse clicks.
This can be achieved with a flag. A flag is just a boolean variable that is determined to draw the image or not.

Originally it will be set to False.

the event would change the flag:
    for event in pygame.event.get():
        if event == pygame.MOUSEBUTTONDOWN:       #I've had this event below the quit event
            draw_image = True
then in your render section (next to oceanSprites draw)
if draw_image:
    bombSprites.draw(screen)
oceanSprites.draw(screen)
hplaneSprites.draw(screen) 
Note that only the bomb images are in the if condition. You dont want to ever draw in the event loop as it only sometimes occurs.


(Nov-04-2018, 09:04 PM)elxleon42 Wrote: [ -> ]I had an idea to create a different loop specifically for the mouse click event, but was unsure how to implement that within the game loop.
There should only ever be one game loop. Creating more loops will make your code into spaghetti.

If you want you can check out our pygame tutorials. It does go into these types of explanations. https://python-forum.io/Forum-Game-Tutorials

Quote: self.blit = (self.image, self.rect) #is this correct for blitting?
it would be screen.blit(self.image, self.rect) because you are passing screen to draw the image in the class' render method. Although i must admit i dont really use pygame.sprite.Sprite much to know the proper method they use. I usually make my own.