Python Forum
[PyGame] Help With Pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Help With Pygame
#1
import pygame, random, math
pygame.init() 
 
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
 
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("My Game")
clock = pygame.time.Clock() 

Ship = pygame.image.load("galaga-nemco.png")
Shipright = pygame.image.load("galaga-nemco-right.png")
Shipleft = pygame.image.load("galaga-nemco-left.png")
ShipDown = pygame.image.load("galaga-nemco-down.png")
Background_Image = pygame.image.load("swagbackground.png")
LaserBeam = pygame.image.load("laserbeam.png")

current = Ship

Laser = pygame.mixer.Sound("laser5.ogg")
pygame.mixer.music.load('DST-TowerDefenseTheme.ogg')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play()

rect_change_x = 0
rect_change_y = 0
ship_x = 50
ship_y = 50

leftedge = 349
rightedge = 700
bottom = 500
top = 0
    
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        
        elif ship_y >= 500:
            ship_y = 460   
        elif ship_y <= 340:
            ship_y = 390      
        elif ship_x >= 700:
            ship_x = 640
        
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                rect_change_x = -3
                current = Shipleft
            elif event.key == pygame.K_RIGHT:
                rect_change_x = 3
                current = Shipright
            elif event.key == pygame.K_UP:
                rect_change_y = -3
            elif event.key == pygame.K_DOWN:
                current = ShipDown
                rect_change_y = 3
                
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                rect_change_x = 0
                current = Ship
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                rect_change_y = 0
                current = Ship

        elif event.type == pygame.MOUSEBUTTONDOWN:
            Laser.play()

            
    ship_x += rect_change_x
    ship_y += rect_change_y
    
    
    screen.blit(Background_Image, (0, 0))
    screen.blit(current, (ship_x, ship_y))
        
    pygame.display.flip()
    clock.tick(60)
    
pygame.quit()
My question is, how can I make LaserBeam come out of ship. (Like a laser shooting out of a space ship)
Reply
#2
you should have a laser class, that handles the laser image, and its position. As well as sound.

You should also be using pygame.Rects for your ship as well as the laser object. This would remove the need for your ship x and y as pygame rects have that within them. Then after both your ship and the laser has a rect, you just center the laser rect to the center of the ships rect, and move it based on which direction you want it to go.

This line is an example that places the starting laser exactly over top the players ship when you use pygame.Rects (NOTE: i changed variable names to make them easier to understand to people who dont really know classes and OOP usage).
In the players class under a spacebar keypress...
all_lasers.append(Laser(player.rect.center, SCREEN_RECT))
Where in the laser class it assigns "player.rect.center" (ie. The players center position) to the lasers center position as a starting position
laser.rect = loaded_image.get_rect(center=player_center)
after which the plyer.rect and laser.rect are the same, but different object. And each laser gets the updated player position and is a separate object independently moving from the other lasers.

Here is an example of doing this exact thing
https://python-forum.io/Thread-PyGame-Ad...cts-part-4

You should be using classes as every time you fire, you are creating separate independent objects with the same characteristics. Unknown variable amount of lasers on the screen at one time, and they all share the same stats, but just move at different intervals. Perfect example of requiring a class. You have sound effect, an image, different positions for each, and at some point you are either going to limit the lasers, or delay the lasers. Its better to organize that code within a laser class rather than you main game loop. IF you dont know classes i would learn a basic understanding of it.
Recommended Tutorials:
Reply
#3
Thank You Good Sir
Reply


Forum Jump:

User Panel Messages

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