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
