Skip to main content
added 1349 characters in body
Source Link
Mangata
  • 2.8k
  • 1
  • 4
  • 10
import pygame

# Starts pygame
pygame.init()

# Creates screen
# Needs two sets of () for some reason
screen = pygame.display.set_mode((800, 600))

# Sets top text of window
pygame.display.set_caption("LaBruh Invaders")

# Sets window Icon
icon = pygame.image.load('labruh.png')
pygame.display.set_icon(icon)

# player starting Pic and location
playerIMG = pygame.image.load('farmerCapy.png')
playerX = 400
playerX_change = 0

playerY = 300
playerY_change = 0

# Resizes img
playerIMG = pygame.transform.scale(playerIMG, (150, 150))


def player(x, y):
    # Blit means draw on surface
    screen.blit(playerIMG, (x, y))


# Game loop (Update every frame)
running = True

while running:
    # Changes color of screen
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        # Checks if key is pressed
        if event.type == pygame.KEYDOWN:
            # If left arrow is pressed moves left
            if event.key == pygame.K_LEFT:
                playerX_change = -5
            if event.key == pygame.K_RIGHT:
                playerX_change = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
        elif event.type == pygame.QUIT:
            running = False
            break
        else:
            continue

        playerX += playerX_change

        # Runs player funct which draws the player at X and Y cords
        player(playerX, playerY)

        # Makes screen quitable

        if playerX > 750:
            playerX = 0

        if playerX < -20:
            playerX = 740

        # Updates the screen
        pygame.display.update()
for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    elif event.type == pygame.QUIT:
        running = False
        break
    else:
        continue
import pygame

# Starts pygame
pygame.init()

# Creates screen
# Needs two sets of () for some reason
screen = pygame.display.set_mode((800, 600))

# Sets top text of window
pygame.display.set_caption("LaBruh Invaders")

# Sets window Icon
icon = pygame.image.load('labruh.png')
pygame.display.set_icon(icon)

# player starting Pic and location
playerIMG = pygame.image.load('farmerCapy.png')
playerX = 400
playerX_change = 0

playerY = 300
playerY_change = 0

# Resizes img
playerIMG = pygame.transform.scale(playerIMG, (150, 150))


def player(x, y):
    # Blit means draw on surface
    screen.blit(playerIMG, (x, y))


# Game loop (Update every frame)
running = True

while running:
    # Changes color of screen
    screen.fill((0, 0, 0))

    for event in pygame.event.get():
        # Checks if key is pressed
        if event.type == pygame.KEYDOWN:
            # If left arrow is pressed moves left
            if event.key == pygame.K_LEFT:
                playerX_change = -5
            if event.key == pygame.K_RIGHT:
                playerX_change = 5
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0
        elif event.type == pygame.QUIT:
            running = False
            break
        else:
            continue

        playerX += playerX_change

        # Runs player funct which draws the player at X and Y cords
        player(playerX, playerY)

        # Makes screen quitable

        if playerX > 750:
            playerX = 0

        if playerX < -20:
            playerX = 740

        # Updates the screen
        pygame.display.update()
A small thing was missing.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

You should discard all unexpected input. like:

for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    elif event.type == pygame.QUIT:
        running = False
        break
    else:
        continue

Thanks Vaillancourt for the reminder, I added the quit command.

You should discard all unexpected input. like:

for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    elif event.type == pygame.QUIT:
        break
    else:
        continue

Thanks Vaillancourt for the reminder, I added the quit command.

You should discard all unexpected input. like:

for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    elif event.type == pygame.QUIT:
        running = False
        break
    else:
        continue
added 127 characters in body
Source Link
Mangata
  • 2.8k
  • 1
  • 4
  • 10

You should discard all unexpected input. like:

for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    elif event.type == pygame.QUIT:
        break
    else:
        continue

Thanks Vaillancourt for the reminder, I added the quit command.

You should discard all unexpected input. like:

for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    else:
        continue

You should discard all unexpected input. like:

for event in pygame.event.get():
    # Checks if key is pressed
    if event.type == pygame.KEYDOWN:
        # If left arrow is pressed moves left
        if event.key == pygame.K_LEFT:
            playerX_change = -5
        if event.key == pygame.K_RIGHT:
            playerX_change = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            playerX_change = 0
    elif event.type == pygame.QUIT:
        break
    else:
        continue

Thanks Vaillancourt for the reminder, I added the quit command.

Source Link
Mangata
  • 2.8k
  • 1
  • 4
  • 10
Loading