Pygame Tutorial - Creating Space Invaders

Tech With Tim · 833K views · 2020 · 1:56:03

Open on YouTube

Game programming & enginesPython, Ruby & scripting languagesEducation & learningtutorial

Why Stack Overflow recommends it

Stack Overflow users link it because many questions come from people following this tutorial and hitting errors, so askers cite it as the source of their code.

Recommended by 3 people across 3 different months, 2020 to 2022.

What it is

A nearly two-hour Python tutorial from Tech With Tim that builds a complete Space Invaders style shooter in pygame. It covers installing and using the pygame module, loading assets, moving ships, firing lasers, collisions and basic game design, with downloadable assets and code.

What people said

Recommends Haadi Khan · 2020-08 · Stack Overflow · on “Pygame NameError: free variable 'lost' referenced before assignment in enclosing scope”

I have recently been working on a Space Invaders game in Pygame via Tech with Tim YouTube tutorial and have encounter a name error on line 111: NameError: free variable 'lost' referenced before assignment in enclosing scope. Link to the tutorial [this video] Code: import pygame import os import time import random import math import sys WIDTH, HEIGHT = 750, 750 screen = pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("Space Invaders") #Game Variables pygame.font.init() pygame.init() #Ships RED_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_red_small.png')) GREEN_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_green_small.png')) BLUE_SPACE_SHIP

Recommends Axhul · 2020-09 · Stack Overflow · on “Getting a type Surface error with argument 1. How would I fix it?”

I'm using Python 3.7.4 and Python 1.9.6. I have a class which is the main one for the subclass's. I'm experiencing an error: the line with the error --> window.blit(self.ship_img, (self.x, self.y)) TypeError: argument 1 must be pygame.Surface, not None . Code: class Ship: def __init__(self, x, y, health=100): self.x = x self.y = y self.health = health self.ship_img = None self.laser_img = None self.lasers = [] self.cool_down_counter = 0 def draw(self, window): window.blit(self.ship_img, (self.x, self.y)) def get_width(self): return self.ship_img.get_width() def get_height(self): return self.ship_img.get_height() The reason it has to be None because it has several images of the same class(mea

Recommends TheBaum · 2022-02 · Stack Overflow · on “forloop while loop integration”

In general you can nest as many for-loops as you want. for target in range(20): # some code for i in range(10): # other code But I guess that's not what your aiming for. my goal is to If all the targets are down create a new set of targets creating a loop if possible. So first of all you can remove a target with target_group.remove(target) . Calling len(target_group) returns the size of the group. To check if all targets are destroyed you can use if len(target_group) == 0: target_group = pygame.sprite.Group() for target in range(20): new_target = Target("target.png", random.randrange(0,screen_width),random.randrange(0,screen_height)) target_group.add(new_target) # main loop of your game whil

Cites James Chen · 2020-12 · Stack Overflow · on “AttributeError: module 'pygame.font' has no attribute 'Sysfont',”

I just started Python and i thought to learn it using a tutorial, [this video] I'm following the tutorial step by step, however, it shows that i have an issue. Despite that I have word for word Can someone help me? This is the part [LINK] This is the tutorial's [LINK] Note: I have the pygame.font.init() included

Cites Federico Baù · 2021-01 · Stack Overflow · on “Pygame | it shows a black screen everytime i try to run it and then crashes, even though it was working before”

First of all, there are so many mistake on the code, anyway here, this should at least show you something: import pygame pygame.init() space_width = 55 space_height = 40 win = pygame.display.set_mode((800, 600)) pygame.display.set_caption("hi") playerimg = pygame.image.load('001-spaceship.jpg') ply = pygame.transform.rotate(pygame.transform.scale(playerimg, (space_width, space_height)), 90) def draw_window(plyred): win.fill((255, 0, 0)) win.blit(ply, (plyred.x, plyred.y)) pygame.display.update() def main(): plyred = pygame.Rect(100, 100, space_width, space_height) run = True while run: win.blit(playerimg, (0, 0)) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False brea

Cites Iris S. · 2021-06 · Stack Overflow · on “Why am I getting the error 'ValueError: list.remove(x): x not in list' when creating a pygame?”

I'm having a different error this time when following Tech with Tim's Pygame tutorial ( [this video] ) about creating Space Invaders with Pygame. I'm 1hr36m into the video. There's something that's going wrong with the lasers. These are the versions, code and errors. What am I doing wrong? Pycharm 2020.3.5., pygame 2.0.1 (SDL 2.0.14, Python 3.8.10) Errors Traceback (most recent call last): File "C:/PycharmProjects/Pygames/Space Invaders/main.py", line 222, in <module> main() File "C:/PycharmProjects/Pygames/Space Invaders/main.py", line 219, in main player.move_lasers(-laser_vel, enemies) File "C:/PycharmProjects/Pygames/Space Invaders/main.py", line 110, in move_lasers self.lasers.remove(laser)

Cites realbob988 · 2021-07 · Stack Overflow · on “AttributeError: 'pygame.Surface' object has no attribute 'pygame'”

I am following a Python Programming tutorial entitled 'Python Crash Course, a hands on, project-based introduction to programming', and I have arrived at the section that taught me how to make an alien invasion game. here is the link to the book: [LINK] when I ran the code (not that the game is not finished) an error occurred. here is my code: import sys import pygame class Ship: "a class to manage ship" def __init__(self, ai_game): "initialize the ship and set its starting position." self.screen = ai_game.screen self.screen_rect = ai_game.screen.pygame.freetype.get_rect() #load the ship image and get its rect. self.image = pygame.image.load('images/pixel_ship_yellow.bmp') self.rect = self.i

Cites Datta · 2022-05 · Stack Overflow · on “TypeError while rewriting pygame tutorial code”

Code from YT tutorial that I am using as a basis for game with additional elements that are not included in tutorial: # Import import os import time import pygame import random # Get font for text pygame.font.init() # Set directory os.chdir(r'E:\Data\python_data\game\Space Invaders') WIDTH, HEIGHT = 850, 850 WIN = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Invasion from Space") # LOADING IMAGES # Enemy ships RED_SHIP = pygame.image.load(os.path.join('assets', 'pixel_ship_red_small.png')) GREEN_SHIP = pygame.image.load(os.path.join('assets', 'pixel_ship_green_small.png')) BLUE_SHIP = pygame.image.load(os.path.join('assets', 'pixel_ship_blue_small.png')) # Player ship