Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # https://www.reddit.com/r/learnpython/comments/4uy0rn/new_to_python_having_problems_with_pygame_and/
- #
- import pygame
- # --- constants - Uppercase ---
- # Colors used in the game
- BLACK = ( 0, 0, 0)
- WHITE = ( 255, 255, 255)
- GREEN = ( 0, 255, 0)
- RED = ( 255, 0, 0)
- # Size of the window / open window
- SIZE = (700, 500)
- # --- classes --- CamelCase ---
- class Bullet(pygame.sprite.Sprite):
- def __init__(self, position):
- super().__init__()
- self.image = pygame.Surface([4,10])
- self.image.fill(WHITE)
- self.rect = self.image.get_rect()
- self.rect.center = position
- def update(self):
- self.rect.y -= 5
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.image.load("player1.png").convert()
- # Make color transparent
- self.image.set_colorkey(BLACK)
- self.rect = self.image.get_rect()
- def draw(self, screen):
- screen.blit(self.image, self.rect)
- def move(self, position):
- self.rect.center = position
- # --- main ---
- # Setup
- pygame.init()
- screen = pygame.display.set_mode(SIZE)
- screen_rect = screen.get_rect() # to center background
- # Window Title
- pygame.display.set_caption("Andrew's Cool Game")
- background_image = pygame.image.load("space1.jpg").convert()
- background_image_rect = background_image.get_rect(center=screen_rect.center) # to center background
- #click_sound = pygame.mixer.Sound("laser5.ogg")
- # Hide mouse cursor
- pygame.mouse.set_visible(False)
- # Bullet List
- bullet_list = pygame.sprite.Group()
- # Player
- player = Player()
- # ---------------- MAIN PROGRAM LOOP ----------------
- # Loop until user closes program
- done = False
- # Manages how fast screen updates
- clock = pygame.time.Clock()
- while done == False: # User did something
- # ----- PROCESSING -----
- for event in pygame.event.get(): # If user clicks close
- if event.type == pygame.QUIT:
- done = True
- # I like to use ESC to close example
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_ESCAPE:
- done = True
- # Moving player
- elif event.type == pygame.MOUSEMOTION:
- player.move(event.pos)
- # Play sound when mouse button is clicked
- elif event.type == pygame.MOUSEBUTTONDOWN:
- #click_sound.play()
- print ("Testing", event.pos)
- bullet = Bullet(event.pos)
- print(bullet.rect)
- bullet_list.add(bullet)
- # ----- PROCESSING END -----
- # ----- GAME LOGIC -----
- for bullet in bullet_list:
- if bullet.rect.y < -10:
- bullet_list.remove(bullet)
- bullet_list.update()
- # ----- GAME LOGIC END -----
- # ----- DRAW -----
- screen.blit(background_image, background_image_rect) # to center background
- player.draw(screen)
- bullet_list.draw(screen)
- # Update the screen with DRAW
- pygame.display.flip()
- # ----- DRAW END -----
- # Limit fps
- clock.tick(60)
- # --- The End ---
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement