Python Game Programming Tutorial: Space Invaders 6

preview_player
Показать описание
Only one enemy? Let's add more!

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
Рекомендации по теме
Комментарии
Автор

wow this really helps with my school assignment. i get so much good information here and i even understand now how the for cycles work, thanks!

andrespalusalu
Автор

we can fix the bullet bug by including "if bullet.ycor() > 275:" to "if bulletstate == 'fire':" using tab

defmake_a_sound
Автор

Thanks for the tutorial, now I can add clones in my python games

programmingduntless
Автор

My code isn't working:
The error that pops up is
TypeError: unsupported overhand type(s) for +=: 'float' and 'method'
(Line 125 - x += enemy.speed)

Here is the code:
#Space Invaders - Part 1
# Add multiple enemies
# Setup screen
import turtle
import os
import math
import random

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

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

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

playerspeed = 15

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

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


for enemy in enemies:
#Create the enemy
enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)

enemy.speed = 2




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

bullet.speed = 20

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

#Move the player left and right
def move_left():
x = player.xcor()
x -= playerspeed
if x < -280:
x = - 280
player.setx(x)

def move_right():
x = player.xcor()
x += playerspeed
if x > 280:
x = + 280
player.setx(x)

def fire_bullet():
# Declare bullet state as a global if it needs changed
global bulletstate
if bulletstate == "ready":
bulletstate = "fire"
# Move the bullet 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


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

#Main game loop
while True:

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

#Move the enemy back and down
if enemy.xcor() > 280:
y = enemy.ycor()
y -= 40
enemy.speed *= -1
enemy.sety(y)

if enemy.xcor() < -280:
y = enemy.ycor()
y -= 40
enemy.speed *= -1
enemy.sety(y)

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

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

#Check for a collision between the bullet and the enemy
if isCollision(bullet, enemy):
#Reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)
#Reset the enemy
enemy.setposition(-200, 250)

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

delay = turtle.done()

playertew
Автор

It looks as if your copying from somewhere, good job by the way! This helped me alot!

shaharyarahmed
Автор

Hi, a question again, can't I make the enemies a bit tougher and make them take a hit or two before resetting them. I have been thinking about it for two days, but haven't got any idea how to do it. So I thought maybe you could help if you got the time.

sahidansari
Автор

Hello! So i have a problem, for some reason whenever i start the game my weapons bullet also starts moving with them and makes a line can u help me?

MonocledTree
Автор

when i get to the point where there are 5 enemies moving on the screen none of them move, could you help me with this if you have time???

theobenadict
Автор

Thanks for this, great tutorial, but do you know how i could make the enemies form rows like the classic Space Invaders? Thanks in advance.

natehinchliffe
Автор

i have get problem in enemy Collision if i shooting the enemy only one is in Collision help me

raz
Автор

I like it so far. I've learned a lot. However, what's kinda driving me nuts is it's klugey? Sometimes the bullet doesn't leave the muzzle, the enemies slow down, speed up, hang up.... Am I missing something?

TheJacklwilliams
Автор

I am fairly new to your channel and I wanted to ask is (I don't know if it will be corrected in later videos) everytime 1 enemy reaches the limit xcor all of them moves down even if they haven't reached it. Is there a way we can check individually if the enemy truly reached the limit xcor before going down?

Onaneehsyu
Автор

Hey liking the videos! I was experimenting a little with the code and wanted to try and make each enemy move independently. So that it doesn't move/change direction on the y-axis and x-axis unless it hits the wall. How would I go about this?

vodzzzer
Автор

hey um bro um so in this episode i wrote everything and for some reason
the enemies just phase through the border and i cant hit them all
like what code do i need to fix or what?

cekop
Автор

I am making the game step by step but when i apply the for loop for movement of multiple enemies they don't move. And even collision check is not working for them. Please help

amanrohilla
Автор

Quick question:

How do you "move" the lines a tab when you've selected them?

For example when you did that with this:
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:
y = enemy.ycor()
y -= 40
enemyspeed *= -1
enemy.sety(y)


if enemy.xcor() < -280:
y = enemy.ycor()
y -= 40
enemyspeed *= -1
enemy.sety(y)

lolind
Автор

hey man i just wanted to tell that this series is so good@ but he problem i encountered here is that my enemies are not moving once i kept the for loop. please help me!

j-vahalla-b
Автор

I am getting this error and none of my enemies are moving, The code is same
Error -

Exception has occurred: _tkinter.TclError
invalid command name ".!canvas"
File "D:\python games\<string>", line 1, in coords
File "D:\python games\bot.py", line 127, in <module>
enemy.setx(x)


The above error is in following part of code -
#Main Game Loop
while True:

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

kshitijakubal
Автор

Hi Christian, I tried adding more enemies to the game. I'm having a problem with my enemies lagging when they move, is there anyway I can move them all at once instead of one at a time?

ryancameron
Автор

hey thank you for the tutorial but i have an error that in 5:11 my enemies don't move!
here is my code
#Space invaders
#Import Packages
#set up screen
import turtle
import os
import math
import random

#Setup Screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders! V3")

#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()

#Create Player
#Player Turtle

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



#choose a number of enemies
number_of_enemies = 5
#create an empty list of the enemies
enemies = []

#add enemies to the list gradually
for i in range(number_of_enemies):
#Enemy


for enemy in enemies:
enemy = turtle.Turtle()
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)

#Enemy Movement
#Enemy Speed
enemyspeed = 2

#Create Player Bullets
#cloning
#from turtle module

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()

#speed of the bullet
bulletspeed = 20

#deine bullet state
#ready - ready to fire/shoot
#fire - bullet in firing format
bulletstate = "ready"


#player Movements
#left and right

playerspeed = 15

#moving left
def move_left():
x = player.xcor()
x -= playerspeed
if x < -280:
x = -280
player.setx(x)

#moving right
def move_right():
x = player.xcor()
x += playerspeed
if x > 280:
x = 280
player.setx(x)

def fire_bullet():
#declare bulletstate as a global var value
global bulletstate
if bulletstate == "ready":
bulletstate = "fire"
#move bullet above player
x = player.xcor()
y = player.ycor() +10
bullet.setposition(x, y)
bullet.showturtle()

def ifCollision (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



#Keyboard binding
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(fire_bullet, "space")


#main game loop
while True:
for enemy in enemies:
#move the enemy
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)

#enemy boundary check
#move enemy back and down

if enemy.xcor() > 280:
y = enemy.ycor()
y -= 40
enemyspeed *= -1
enemy.sety(y)


if enemy.xcor() < -280:
y = enemy.ycor()
y -= 40
enemyspeed *= -1
enemy.sety(y)

#Moving The Bullet
if bulletstate == "fire":
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)


#Bullet YCOR Check
if bullet.ycor() > 275:
bullet.hideturtle()
bulletstate = "ready"

#Checking For Collison og bullet and enemy
if ifCollision(bullet, enemy):
#reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)
#reset the enemy
enemy.setposition(-200, 250)

if ifCollision(player, enemy):
player.hideturtle()
enemy.hideturtle()
print("Game Over")
break


delay = input("press enter to finish")

SantoshKumar-teuh
welcome to shbcf.ru