Python Forum
[PyGame] Help with blitting and events
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Help with blitting and events
#1
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
Reply


Messages In This Thread
Help with blitting and events - by elxleon42 - Nov-04-2018, 09:04 PM
RE: Help with blitting and events - by metulburr - Nov-04-2018, 11:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [pygame] Blitting armor and weapons with inventory SheeppOSU 3 2,956 Apr-29-2019, 02:31 AM
Last Post: SheeppOSU
  [PyGame] Beginner in pygame, I don't know how to use events Erepiv1 5 6,524 Jan-26-2018, 08:59 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020