Reputation: 11
So I was following along a pygame tutorial and the code looks like this:
import pygame
import os
import time
import random
pygame.font.init()
player_vel = 5
WIDTH, HEIGHT = 750, 750
BLUE_BULLETS = pygame.image.load(os.path.join("c:\\pixel_laser_blue.png"))
YELLOW_BULLETS = pygame.image.load(os.path.join("c:\\pixel_laser_yellow.png"))
RED_BULLETS = pygame.image.load(os.path.join("c:\\pixel_laser_red.png"))
GREEN_BULLETS = pygame.image.load(os.path.join("c:\\pixel_laser_green.png"))
BG = pygame.transform.scale(pygame.image.load(os.path.join("c:\\background-black.png")), (WIDTH, HEIGHT))
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("space invaders")
RED_SPACESHIP = pygame.image.load(os.path.join("c:\\pixel_ship_red_small.png"))
GREEN_SPACESHIP = pygame.image.load(os.path.join("c:\\pixel_ship_green_small.png"))
BLUE_SPACESHIP = pygame.image.load(os.path.join("C:\\pixel_ship_blue_small.png"))
YELLOW_SPACESHIP = pygame.image.load(os.path.join("C:\\pixel_yellow_ship.png"))
class Ship:
def __init__(self, x, y, health=100):
self.x = x
self.y = y
self.health = health
self.ship_image = None
self.laser_image = None
self.laser = []
self.cool_down_counter = 0
def draw(self, window):
pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, 50 ,50), 1)
def main():
run = True
FPS = 60
lives = 5
level = 1
clock = pygame.time.Clock()
main_font = pygame.font.SysFont("comicsans", 50)
ship = Ship(500, 500)
keys = pygame.keys.get_pressed()
def redraw_window():
WIN.blit(BG, (0, 0))
level_font = main_font.render(f"level :{level}", 1, (0, 0, 255))
live_font = main_font.render(f"lives :{lives}", 1, (0, 0, 255))
WIN.blit(level_font, (10, 10))
WIN.blit(live_font, (WIDTH - live_font.get_width() - 10, 10))
ship.draw(WIN)
pygame.display.update()
while run:
clock.tick(FPS)
redraw_window()
if keys[pygame.K_a]:
ship.x -= player_vel
if keys[pygame.K_d]:
ship.x += player_vel
if keys[pygame.K_w]:
ship.y -= player_vel
if keys[pygame.K_s]:
ship.y += player_vel
for event in pygame.event.get:
if event.type == pygame.QUIT():
run = False
main()
I am pretty sure I did not do anything wrong this time.
but the output looks like this:
PS C:\Python\space invaders\assets\assets> & C:/Python/python.exe "c:/Python/space invaders/assets/assets/main.py"
pygame 2.0.1 (SDL 2.0.12, Python 3.9.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
what is your min len gonna be?
It shows the pygame window but the window just stays black and you can't even close it.
Sorry if the output part is a bit messy but stack overflow won't let me post it because there is too much code.
Upvotes: 0
Views: 67
Reputation: 1888
The issue is due to missing parentheses in the pygame.event.get line of your code. get is a function so it should end with (). It should be pygame.event.get() instead of pygame.event.get.
for event in pygame.event.get():
if event.type == pygame.QUIT():
run = False
Try this.
Upvotes: 0