Python Game Programming Tutorial: Space Invaders 12

preview_player
Показать описание
In this update, learn how to align the invaders in rows and increase the animation speed. Enjoy!

NEED HELP?

❤️❤️ SHOW SOME LOVE AND SUPPORT THE CHANNEL ❤️❤️

Click Join and Become a Channel Member Today!
Channel members can get preferential comment replies, early access to new content, members only live streams, and access to my private Discord.

Amazon Affiliate Links

Other Affiliate Links

LINKS

LEARN MORE PYTHON

LEARN MORE JAVA

#Python #Tutorial #Beginner
Рекомендации по теме
Комментарии
Автор

Bruh
All your space invader videos have been awesome.👏👏👏
Keep up the great work

Aryan-vzlv
Автор

my bullet speed was 0.2 my enemy speed was 0.2 and my player speed was 0.1 thank you for all these years teaching us how to make these fun games!

sgridluver
Автор

Trivia: I believe that in the original space invaders the enemies appeared to move because the screen position moved relative to them. That created the illusion that the enemies moved. That way it was less computationally expensive.

alexandarjelenic
Автор

Just as I thought that Python games are really slow, you uploaded this! Many thanks!

pantsik
Автор

I love this thanks for all the tutorials

jokerface
Автор

5:47 --> This works too:
x = enemy_start_x + 50 * (enemy_number%10)
y = enemy_start_y - 40 * int(enemy_number/10)

gordonspond
Автор

i cant believe you only have 25k subs, you are so good you should have move

EthanJbleethan
Автор

well, when you did put tracer(0), your game became crazy, my game give a red bull to each alien cause they were super fast and killed me T_T (i've already have the wn.update() function)

JP_GP
Автор

When I do the tracer and update thing, I can't see my bullet

briangao
Автор

Wassup man, I'm a student in a coding class and I've gotten really interested in it. I was wondering if you can possibly create a video that does into deeper depth on how to use the gifs to make your background, invaders, etc. I don't know how to download them or turn it into a piece of code, no rush whenever you have the time ;)

mister.e
Автор

Hi Tokyo,
Was just curious if there is a way to perhaps lower the volume of the sound effects as it is incredibly annoying when your playing the game at 50% volume and then when you fire the sound echoes through the entire room... Any way to fix this?
Cheers

SaazGamingChannel
Автор

Hello, Master, I am wondering about one thing, from the very beginning, when we create 30 enemies, the whole program starts to jam hard, I wonder what it is caused by, because now our computers can handle games in high quality 3D without big lags and with such a simple game everything is jammed is due to the fact that the 3d games have very well optimized games or our game is wrongly written?

krystiank
Автор

and I was wondering about how to make RTS game in Python...

SkyFly
Автор

Why does my aliens go off the screen after going down 2 rows?

import turtle
import os
import math
import random
import platform
import pygame

pygame.init()
bgsound =
pygame.mixer.music.play(-1)

if platform.system() == "Windows":
try:
import winsound
except:
print("Winsound module not available.")

# Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")

wn.tracer(0)

# Register the shapes



# Draw border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()

# Set the score to 0
score = 0

# Draw the score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 280)
scorestring = "Score: {}".format(score)
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
score_pen.hideturtle()

# Create the player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("player.gif")
player.penup()
player.speed(0)
player.setposition(0, -250)
player.setheading(90)
player.speed = 0

# Choose a number of enemies
number_of_enemies = 30
# Create an empty list of enemies
enemies = []

# Add enemies to the list
for i in range(number_of_enemies):
# Create the enemy


enemy_start_x = -225
enemy_start_y = 250
enemy_number = 0

for enemy in enemies:
enemy.color("red")
enemy.shape("invader.gif")
enemy.penup()
enemy.speed(0)
x = enemy_start_x + (50 * enemy_number)
y = enemy_start_y
enemy.setposition(x, y)
# Update the enemy number
enemy_number += 1
if enemy_number == 10:
enemy_start_y -= 50
enemy_number = 0

enemyspeed = 0.05

# Create the player's bullet
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed(0)
bullet.setheading(90)
bullet.shapesize(0.5, 0.5)
bullet.hideturtle()

bulletspeed = 1

# Define bullet state
# ready - ready to fire
# fire - bullet is firing
bulletstate = "ready"


# Move the player left and right
def move_left():
player.speed = -0.5

def move_right():
player.speed = 0.5

def move_player():
x = player.xcor()
x += player.speed
if x < -280:
x = - 280
if x > 280:
x = 280
player.setx(x)

def fire_bullet():
# Declare bulletstate as a global if it needs changed
global bulletstate
if bulletstate == "ready":
play_sound("laser.wav")
bulletstate = "fire"
# Move the bullet to the just above the player
x = player.xcor()
y = player.ycor() + 10
bullet.setposition(x, y)
bullet.showturtle()

def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(), 2)+math.pow(t1.ycor()-t2.ycor(), 2))
if distance < 15:
return True
else:
return False

def play_sound(sound_file, time = 0):
# Windows
if platform.system() == "Windows":
winsound.PlaySound(sound_file, winsound.SND_ASYNC)
# Linux
elif platform.system() == "linux":
os.system("aplay -q {}&".format(sound_file))
# Mac
else:
os.system("afplay {}&".format(sound_file))

# Repeat sound
if time > 0:
turtle.ontimer(lambda: play_sound(sound_file, time), t=int(time * 1000))

# Create keyboard bindings
wn.listen()
wn.onkeypress(move_left, "Left")
wn.onkeypress(move_right, "Right")
wn.onkeypress(fire_bullet, "space")

# Play background music
play_sound("bgm.wav", 115)

# Main game loop
while True:
wn.update()
move_player()

for enemy in enemies:
# Move the enemy
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)

# Move the enemy back and down
if enemy.xcor() > 280:
# Move all enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# Change enemy direction
enemyspeed *= -1

if enemy.xcor() < -280:
# Move all enemies down
for e in enemies:
e.ycor()
y -= 40
e.sety(y)
# Change enemy direction
enemyspeed *= -1

# Check for a collision between the bullet and the enemy
if isCollision(bullet, enemy):
play_sound("explosion.wav")
# Reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)
# Reset the enemy
enemy.setposition(0, 10000)
# Update the score
score += 10
scorestring = "Score: {}".format(score)
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))

if isCollision(player, enemy):
play_sound("explosion.wav")
player.hideturtle()
enemy.hideturtle()
print("Game Over")
break

# Move the bullet
if bulletstate == "fire":
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)

# Check to see if the bullet has gone to the top
if bullet.ycor() > 275:
bullet.hideturtle()
bulletstate = "ready"

elvaney
Автор

sir i have a new question about how to make the player stop when we are not pressing pls tell me how to do it

seabreezeluck
Автор

Hi, Christian powerful upgrade!! I created a list of enemy and every time an invaders dies I remove it from the list. What do you think? Bye!!

andreadotta
Автор

when I start after X = enemy_ start_x + (50* enemy_number ) my screen just turns black and said it wasn't defined heres the ereor ive got Traceback (most recent call last):
File invaders.py", line 124, in <module>

make_enemies()

File invaders.py", line 35, in make_enemies

x = enemy_start_x + (50 * enemy_number)

NameError: name 'enemy_start_x' is not defined

rumlord
Автор

I noticed that when the game is played for a long time the speed of everything slows down. Do you know why this happens?

pantsik
Автор

@Christian Thompson when I type in wn.update() and I run the program, the program is still very laggy and slow. Please help me.

bryanng
Автор

the enemies falls down, out of frame after some time .what should i do??

blahblahblahhhh