Space Invaders movement is acting really weird [duplicate] - python

I've created a Space Invaders clone in Python using the PyGame modules but I'm running into some difficulty getting them to move down together when reaching the edge of the game screen.
How would I make it so when the aliens reach the edge of the game screen they all simultaneously change direction and drop down a level?
import pygame
import random
class spaceInvader(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load("spaceInvader.png")
self.x = 200
self.y = 390
self.shots = []
def handle_keys(self):
key = pygame.key.get_pressed()
dist = 5
if key[pygame.K_RIGHT]:
self.x+=dist
elif key[pygame.K_LEFT]:
self.x-=dist
def draw(self, surface):
surface.blit(self.image,(self.x,self.y))
for s in self.shots:
s.draw(screen)
class Alien(pygame.sprite.Sprite):
def __init__(self,x,y,direction,alienType):
pygame.sprite.Sprite.__init__(self)
self.AlienType = alienType
self.Direction = direction
if alienType == 1:
alienImage = pygame.image.load("alien1.png")
self.Speed = 1
self.Score = 5
if alienType == 2:
alienImage = pygame.image.load("alien2.png")
self.Score = 15
self.Speed = 1
if alienType == 3:
alienImage = pygame.image.load("alien3.png")
self.Score = 10
self.Speed = 1
if alienType == 4:
alienImage = pygame.image.load("alien4.png")
self.Score = 20
self.Speed = 1
if alienType == 5:
alienImage = pygame.image.load("alien5.png")
self.Score = 25
self.Speed = 1
self.image = pygame.Surface([26, 50])
self.image.set_colorkey(black)
self.image.blit(alienImage,(0,0))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def moveAliens(self):
if self.Direction == "right":
self.rect.x += self.Speed
if self.Direction == "left":
self.rect.x -= self.Speed
pygame.init()
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode([400,400])
allAliens = pygame.sprite.Group()
spaceInvader = spaceInvader()
pygame.display.set_caption("Space Attack")
background_image = pygame.image.load("Galaxy.png").convert()
pygame.mouse.set_visible(True)
done = False
clock = pygame.time.Clock()
black =( 0, 0, 0)
white =( 255,255,255)
red = (255, 0, 0)
score = 0
enemies = []
#For X coords
spawnPositions = [50,100,150,200,250,300,350]
yCoord = 10
for n in range(5):
for i in range(len(spawnPositions)):
xCoord = spawnPositions[i]
alienType = random.randint(1,5)
alien = Alien(xCoord, yCoord,"right", alienType)
allAliens.add(alien)
yCoord = yCoord + 15
loop = 0
while done == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
for alien in (allAliens.sprites()):
if alien.rect.x < 0:
alien.rect.y = alien.rect.y + 15
alien.Direction = "right"
if alien.rect.x > 395:
alien.rect.y = alien.rect.y + 15
alien.Direction = "left"
loop =+1
for alien in (allAliens.sprites()):
alien.moveAliens()
spaceInvader.handle_keys()
screen.blit(background_image,[0,0])
spaceInvader.draw(screen)
allAliens.draw(screen)
pygame.display.flip()
clock.tick(20)
pygame.quit()
Thanks.

Your problem lies here:
for alien in (allAliens.sprites()):
if alien.rect.x < 0:
alien.rect.y = alien.rect.y + 15
alien.Direction = "right"
if alien.rect.x > 395:
alien.rect.y = alien.rect.y + 15
alien.Direction = "left"
loop =+1
I assume the aliens are currently individually dropping down?
You need to change this so that when one alien triggers these if statements, all alien's y and direction are appropriately set, not just the one hitting the side.

# Space Invader
import turtle
import os
import random
import math
# Set up the screen.
wn = turtle.Screen()
wn.setup(700,700)
wn.bgcolor("lightblue")
wn.title("Space Invaders")
# Draw border
border = turtle.Turtle()
border.speed("fastest")
border.color("white")
border.penup()
border.setposition(-300,-270)
border.pendown()
border.pensize(3)
for side in range(4):
border.forward(560)
border.left(90)
border.hideturtle()
# Set the score to 0.
score = 0
# Draw the score.
score_pen = turtle.Turtle()
score_pen.speed("fastest")
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290,260)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align="left", font = ("Arial",14,"normal"))
score_pen.hideturtle()
# Create the player turtle
player = turtle.Turtle()
player.color("black")
player.speed("fastest")
player.shape("triangle")
player.penup()
player.setposition(0,-265)
player.setheading(90)
playerspeed = 15
# Create enemy
#enemy = turtle.Turtle()
#enemy.shape("circle")
#enemy.color("red")
#enemy.penup()
#enemy.speed(2)
#enemy.setposition(-200,250)
#enemyspeed = 2
# Choose number of enemies.
number_of_enemies = 5
# Create an empty list of enemies.
enemies = []
# Add enemies to list.
for i in range(number_of_enemies):
# Create enemy
enemies.append(turtle.Turtle())
for enemy in enemies:
enemy.shape("circle")
enemy.color("red")
enemy.penup()
enemy.speed(2)
x = random.randint(-200,200)
y = random.randint(100,250)
enemy.setposition(x,y)
enemyspeed = 2
# Create the player's bullet.
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed("fastest")
bullet.setheading(90)
bullet.shapesize(.5,.5)
bullet.hideturtle()
bulletspeed = 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 >250:
x = 250
player.setx(x)
def fire_bullet():
# Declare the bulletstate as a global if it needs changed.
global bulletstate
if bulletstate == "ready":
bulletstate = "fire"
# Move the bullet just above the player.
x = player.xcor()
y = player.ycor() + 10
bullet.setposition(x,y)
bullet.showturtle()
def isCollision(t1,t2):
dx = t1.xcor() - t2.xcor()
dy = t1.ycor()- t2.ycor()
distance = dx**2 + dy**2
distance1 = math.sqrt(distance)
if distance1 < 15:
return True
else:
return False
# Create keyboard bindings
wn.onkey(move_left, "Left")
wn.onkey(move_right,"Right")
wn.onkey(fire_bullet,"space")
wn.listen()
# Main game loop
while True:
for enemy in enemies:
# Move enemy
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
# Move the enemy back and down
if enemy.xcor()>250:
# Move all the enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# Change the enemy direction
enemyspeed *= -1
if enemy.xcor()< -280:
# Move all the enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# change the enemy direction
enemyspeed *= -1
# Check for the collision between bullet and the enemy.
if isCollision(bullet ,enemy):
# Reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0,-400)
# Reset the enemy
x = random.randint(-200,200)
y = random.randint(100,250)
enemy.setposition(x,y)
# Update the score.
score += 1
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align = "left",font = ("Arial",14, "normal"))
if isCollision(player,enemy):
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"
turtle.mainloop()

Related

pygame "invalid position for blit"

I am trying to make a game but when I run the code the game makes 5 enemies appear on the screen. It says "invalid position for blit". However, I don't know where to put the blit otherwise.
I don't know what to do because I'm still learning. The code was fine before adding the copies. Not sure why that changed anything.
This is the full code:
import pygame
import random
import math
from pygame import mixer
#Imports the pygame, random, math and mixer module.
pygame.init()
#Initializes Pygame
screen = pygame.display.set_mode((800,600))
#Sets the screen to pygame looks and not normal python looks.
pygame.display.set_caption("Draft")
#Changes the title
icon = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/doctor.png')
pygame.display.set_icon(icon)
#Changing the Icon
#Adding Background Music
mixer.music.load('C:/Users/user/Desktop/Python/CodingBee/sb_indreams.mp3')
mixer.music.play(-1)
#Loads the music and plays if until the window is closed
#Colors
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
#Setting color variables to make it easier to access later in the code.
#Player
player_img = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/Doctor_Running-removebg-preview (1).png')
playerx = 20
playery = 390
playerx_change = 0
playery_change = 100
#Making the Player Variables.
#Health Kit
HealthImg = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/first-aid-kit.png')
HealthX = 20
HealthY = 405
HealthX_Change = -10
HealthY_Change = 0
Health_State = "ready"
#Making the Health_Kit Variables.
#Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
number_of_enemies = 5
#Making a list for all 5 enemies image, x value, y value, x value change, y value change. All variables are a seperate list
for i in range(number_of_enemies):
enemyImg.append(pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/zombie.png'))
enemyX.append(random.randint(0, 736))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.45)
enemyY_change.append(40)
#Score
Score_Value = 0
font = pygame.font.Font('C:/Users/user/Desktop/Python/Pygame/Space_Invaders/Mostery.ttf', 25)
#The pygame font extension requires 2 values, the font and the font size.
#.ttf is a font extension
#Making the score variable
ScoreX = 10
ScoreY = 25
#Making a variable for the fonts x and y position
#Heading
headingfont = pygame.font.Font('C:/Users/user/Desktop/Python/Pygame/Space_Invaders/Bouncy-PERSONAL_USE_ONLY.otf', 45)
HeadingX = 230
HeadingY = 10
#Game Over
game_over_font = pygame.font.Font('C:/Users/user/Desktop/Python/Pygame/Space_Invaders/Bouncy-PERSONAL_USE_ONLY.otf', 64)
#Creating Classes
class Background():
def __init__(self):
self.bgimage = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/Ground.png')
self.rectBGimg = self.bgimage.get_rect()
self.bgY1 = 485
self.bgX1 = 0
self.bgY2 = 485
self.bgX2 = self.rectBGimg.width
self.moving_speed = 7
def update(self):
self.bgX1 -= self.moving_speed
self.bgX2 -= self.moving_speed
if self.bgX1 <= -self.rectBGimg.width:
self.bgX1 = self.rectBGimg.width
if self.bgX2 <= -self.rectBGimg.width:
self.bgX2 = self.rectBGimg.width
def render(self):
screen.blit(self.bgimage, (self.bgX1, self.bgY1))
screen.blit(self.bgimage, (self.bgX2, self.bgY2))
class Player():
def draw_player():
screen.blit(player_img,(playerx,playery))
def player_jump():
global playery
playery -= playery_change
class Enemy():
def draw_enemy(enemyx,enemyy,i):
screen.blit(enemyImg[i],(enemyx,enemyy))
def move_enemy():
global enemyX
enemyX[i] += enemyX_change[i]
class Health():
def fire_Health (x,y):
global Health_State
Health_State = "fire"
screen.blit(HealthImg, ( x + 70, y + 10))
class Other():
def isCollision(enemyX, enemyY, HealthX, HealthY):
distance = math.sqrt(math.pow(enemyX - HealthX, 2) + (math.pow(enemyY - HealthY, 2)))
if distance < 27:
return True
else:
return False
def show_heading():
Heading = headingfont.render("Health Run!", True, (255,255,255))
screen.blit(Heading,( HeadingX, HeadingY))
def show_score():
Score = font.render("Score = " + str(Score_Value), True, (255,255,255))
screen.blit(Score,( ScoreX, ScoreY))
def game_over():
Game_Over = headingfont.render("GAME OVER!", True, (255,255,255))
screen.blit(Game_Over,( 200, 400))
Score_Value = 0
#def states an event. The event will not occur unless you call the event in the main game loop.
back_ground = Background()
#Main Game Loop
clock = pygame.time.Clock()
SCEEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCEEN_UPDATE,150)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
Player.player_jump()
if event.key == pygame.K_SPACE:
if Health_State == "ready":
Health_Fire_Sound = mixer.Sound('C:/Users/user/Desktop/Python/Pygame/Space_Invaders/laser.wav')
Health_Fire_Sound.play()
Health.fire_Health(HealthX , HealthY)
HealthY = playery + 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
playery += playery_change
screen.fill(blue)
back_ground.update()
back_ground.render()
#Health Kit Movement
if HealthX >= 800:
HealthX = 20
Health_State = "ready"
if Health_State == "fire":
Health.fire_Health(HealthX , HealthY)
HealthX -= HealthX_Change
#Enemy Movement and collision check
for i in range(number_of_enemies):
if enemyX[i] > 20:
for j in range(number_of_enemies):
enemyY = 2000
Other.game_over()
break
Enemy.move_enemy()
collision = Other.isCollision(enemyX[i],enemyY[i],HealthX,HealthY)
if collision:
BulletY = 480
Bullet_State = "ready"
enemyX[i] = random.randint(800,900)
enemyY[i] = 405
HealthX = 20
Health_State = "ready"
Score_Value += 1
# draw objects
Player.draw_player()
Enemy.draw_enemy(enemyX,enemyY, i)
Other.show_heading()
Other.show_score()
# update display
pygame.display.update()
clock.tick(60)
enemyX and enemyY are list of coordinates. You have 2 options:
pass items of the lists to draw_enemy:
class Enemy():
def draw_enemy(enemyx, enemyy, i):
screen.blit(enemyImg[i], (enemyx, enemyy))
Enemy.draw_enemy(enemyX[i], enemyY[i], i)
Get the items from the lists in draw_enemy:
class Enemy():
def draw_enemy(enemyx, enemyy, i):
screen.blit(enemyImg[i], (enemyx[i], enemyy[i]))
Enemy.draw_enemy(enemyX, enemyY, i)
Anyway, I suggest to create an Enemy class with attributes (see Classes). Note that the following code is not complete, it is just an example of how to implement classes and use object instances. I'll leave it up to you as an exercise to incorporate it into your code.
class Enemy():
def __init__(self, image, x, y, changeX, changeY):
self.image = image
self.x = x
self.y = y
self.changeX = changeX
self.changeY = changeY
def move(self):
self.x += self.changeX
self.y += self.changeY
def draw(self):
screen.blit(self.image, (self.x, self.y))
number_of_enemies = 5
enemies = []
enemyImg = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/zombie.png')
for i in range(number_of_enemies):
x = random.randint(0, 736)
y = random.randint(50, 150)
enemy = Enemy(enemyImg, x, y, 0.45, 0)
enemies.append(enemy)
running = True
while running:
# [...]
for enemy in enemies:
enemy.move()
# [...]
for enemy in enemies:
enemy.draw()
# [...]

pygame platformer collision causes weird spacing issue

I am working on a platformer and have a weird issue with the collision. So far, I have a player and some platforms and the collision does work but the horizontal collision leaves some space between the player and the level and I don't understand why.
Here is the code for the player:
import pygame
from pygame.math import Vector2
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((32,64))
self.image.fill('red')
self.rect = self.image.get_rect(center = (350,250))
self.direction = Vector2(0,0)
self.speed = 8
self.gravity = 0.8
self.jump_speed = -16
self.on_ground = False
def get_input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]: self.direction.x = 1
elif keys[pygame.K_LEFT]: self.direction.x = -1
else: self.direction.x = 0
if keys[pygame.K_SPACE] and self.on_ground:
self.jump()
self.on_ground = False
def jump(self):
self.direction.y = self.jump_speed
def apply_gravity(self):
self.direction.y += self.gravity
def apply_movement(self):
# horizontal movement
self.rect.x += self.direction.x * self.speed
# vertical movement
self.rect.y += self.direction.y
def update(self):
# movement methods
self.get_input()
self.apply_gravity()
self.apply_movement()
and here the code for the level
import pygame, sys
from player import Player
basic = [
'0000000000000',
'0000000000000',
'0010011000010',
'0111000010110',
'0111000110111',
'1111111111111',
'1111111111111',]
class Level:
def __init__(self):
self.player = pygame.sprite.GroupSingle(Player())
self.tiles = pygame.sprite.Group()
self.setup_level()
def setup_level(self):
for row_index, row in enumerate(basic):
for col_index,cell in enumerate(row):
if cell == '1':
x = col_index * 100
y = row_index * 100
self.tiles.add(Tile((x,y)))
def player_collision(self):
player = self.player.sprite
player_size = player.rect.size
# vertical collision check
future_y = player.rect.top + player.direction.y
future_player_y = pygame.Rect((player.rect.left,future_y),player_size)
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(future_player_y):
if player.direction.y >= 0:
player.on_ground = True
player.rect.bottom = sprite.rect.top
player.direction.y = 0
else:
player.rect.top = sprite.rect.bottom
player.direction.y = 0
# horizontal collision check
future_x = self.player.sprite.rect.left + (self.player.sprite.direction.x * self.player.sprite.speed)
future_player_x = pygame.Rect((future_x,self.player.sprite.rect.top),player_size)
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(future_player_x):
if player.direction.x < 0:
player.rect.left = sprite.rect.right
player.rect.right += player.speed
elif player.direction.x > 0:
player.rect.right = sprite.rect.left
player.rect.right -= player.speed
def run(self):
self.tiles.draw(screen)
self.player.update()
self.player_collision()
self.player.draw(screen)
class Tile(pygame.sprite.Sprite):
def __init__(self,pos):
super().__init__()
self.image = pygame.Surface((100,100))
self.image.fill('green')
self.rect = self.image.get_rect(topleft = pos)
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((1000,640))
clock = pygame.time.Clock()
debug_font = pygame.font.Font(None,40)
level = Level()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill('black')
level.run()
pygame.display.update()
clock.tick(60)
I am using future_player_y because I had some earlier problems with animation frames and detecting whether the player was on the ground or not. I applied the same logic to the horizontal axis but I guess it wouldn't be strictly needed there.
The result looks like this and the collisions do work (kinda) but the red rectangle cannot move any closer to the tile to its left.
Just for clarity, here is a zoomed in look at the area I am talking about:
You need to separate the movement in the x and y direction.
Remove the apply_movement method:
class Player(pygame.sprite.Sprite):
# [...]
def update(self):
# movement methods
self.get_input()
self.apply_gravity()
# self.apply_movement() <--- DELETE
Apply the movement separately before the collision test:
class Level:
# [...]
def player_collision(self):
player = self.player.sprite
# vertical collision check
player.rect.y += player.direction.y
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(player.rect):
if player.direction.y >= 0:
player.on_ground = True
player.rect.bottom = sprite.rect.top
player.direction.y = 0
else:
player.rect.top = sprite.rect.bottom
player.direction.y = 0
# horizontal collision check
player.rect.x += player.direction.x * player.speed
for sprite in self.tiles.sprites():
if sprite.rect.colliderect(player.rect):
if player.direction.x < 0:
player.rect.left = sprite.rect.right
elif player.direction.x > 0:
player.rect.right = sprite.rect.left

Python Turtle module hideturtle() isn't working

I am writing the Space Invaders game from Christian Thompson
I have problem exactly in line 188 where the player and enemy hide when a collision is detected. My player turtle is hiding but the enemy turtle doesn't "want" to hide. Here is the code:
import turtle
import os
import math
import random
import winsound
#Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
wn.bgpic("space_invaders_background.gif")
#Register the shapes
turtle.register_shape("invader.gif")
turtle.register_shape("player.gif")
#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 score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 280)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
score_pen.hideturtle()
#Game Over
game_over = turtle.Turtle()
game_over.speed(0)
game_over.color("red")
game_over.penup()
game_over.setposition(0, 0)
gameOverMsg = "Game Over!"
game_over.hideturtle()
#Create player
player = turtle.Turtle()
player.color("blue")
player.shape("player.gif")
player.penup()
player.speed(0)
player.setposition(0,-230)
player.setheading(90)
playerspeed = 15
#Choose a number of enemies
number_of_enemies = 20
#Create empty list of enemies
enemies = []
#Add enemies to list
for i in range(number_of_enemies):
# Create the enemy
enemies.append(turtle.Turtle())
for enemy in enemies:
enemy.color("red")
enemy.shape("invader.gif")
enemy.penup()
enemy.speed(0)
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
enemyspeed = number_of_enemies * 0.5
#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 = 20
def laser():
winsound.PlaySound("laser.wav", winsound.SND_ASYNC)
def explosion():
winsound.PlaySound("explosion.wav", winsound.SND_ASYNC)
#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():
if not bullet.isvisible():
laser()
#Move the bullet to the just above the player
x = player.xcor()
y = player.ycor() + 10
bullet.setposition(x, y)
bullet.showturtle()
#Checking collision between enemy and bullet
def isCollision(t1, t2) :
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 20:
return True
else:
return False
#Create keyboard bindings
turtle.listen()
turtle.onkeypress(move_left, "Left")
turtle.onkeypress(move_right, "Right")
turtle.onkeypress(fire_bullet, "space")
#Main game loop
while True:
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:
y = 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):
explosion()
# Reset the bullet
bullet.hideturtle()
bullet.setposition(0, -400)
# Reset the enemy
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
#Update the score
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
if isCollision(player, enemy):
explosion()
player.hideturtle()
enemy.hideturtle()
game_over.write(gameOverMsg, False, align="center", font=("Arial", 60, "normal"))
break
#Move the bullet
if bullet.isvisible():
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)
#Check border collision
if bullet.ycor() > 290:
bullet.hideturtle()
wn.mainloop()
I wasn't able to reproduce your problem. I've reworked your code below: I've dropped the images and sound so anyone can run it and help you debug it; I tossed your isCollision() code and substituted turtle's own distance() method; I replaced the while True: loop, which has no place in an event-driven world like turtle, with a timer event; I've streamlined the code where I could:
from turtle import Screen, Turtle
from random import randint
PLAYER_SPEED = 15
BULLET_SPEED = 20
# Choose a number of enemies
NUMBER_OF_ENEMIES = 20
SMALL_FONT = ('Arial', 14, 'normal')
LARGE_FONT = ('Arial', 60, 'normal')
# Move the player left and right
def move_left():
x = player.xcor() - PLAYER_SPEED
if x < -280:
x = -280
player.setx(x)
def move_right():
x = player.xcor() + PLAYER_SPEED
if x > 280:
x = 280
player.setx(x)
def fire_bullet():
if not bullet.isvisible():
# Move the bullet to the just above the player
x, y = player.position()
bullet.setposition(x, y + 10)
bullet.showturtle()
# Check collision between enemy and bullet
def isCollision(t1, t2):
return t1.distance(t2) < 20
# Set up the screen
wn = Screen()
wn.bgcolor('black')
wn.title("Space Invaders")
# Draw border
border_pen = Turtle(visible=False)
border_pen.speed('fastest')
border_pen.color('white')
border_pen.pensize(3)
border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()
for _ in range(4):
border_pen.fd(600)
border_pen.lt(90)
# Set the score to 0
score = 0
# Draw score
score_pen = Turtle(visible=False)
score_pen.speed('fastest')
score_pen.color('white')
score_pen.penup()
score_pen.setposition(-290, 280)
scorestring = "Score: %s" % score
score_pen.write(scorestring, False, align='left', font=SMALL_FONT)
# Game Over
game_over = Turtle(visible=False)
game_over.speed('fastest')
game_over.color('red')
game_over.penup()
gameOverMsg = "Game Over!"
# Create player
player = Turtle('turtle')
player.speed('fastest')
player.color('blue')
player.penup()
player.setposition(0, -230)
player.setheading(90)
# Create empty list of enemies
enemies = []
# Add enemies to list
for _ in range(NUMBER_OF_ENEMIES):
# Create the enemy
enemy = Turtle('turtle', visible=False)
enemy.settiltangle(270) # for non GIF version of game
enemy.speed('fastest')
enemy.color('red')
enemy.penup()
enemy.setposition(randint(-200, 200), randint(100, 250))
enemies.append(enemy)
for enemy in enemies:
enemy.showturtle()
# Create the player's bullet
bullet = Turtle('triangle', visible=False)
bullet.color('yellow')
bullet.penup()
bullet.speed('fastest')
bullet.setheading(90)
bullet.shapesize(0.5)
# Create keyboard bindings
wn.onkeypress(move_left, 'Left')
wn.onkeypress(move_right, 'Right')
wn.onkeypress(fire_bullet, 'space')
wn.listen()
enemy_speed = NUMBER_OF_ENEMIES * 0.5
# Main game loop
def move():
global score, enemy_speed
collision = False
for enemy in enemies:
# Move the enemy
enemy.forward(enemy_speed)
# Check for a collision between the bullet and the enemy
if isCollision(bullet, enemy):
# Reset the bullet
bullet.hideturtle()
# Reset the enemy
enemy.setposition(randint(-200, 200), randint(100, 250))
# Update the score
score += 10
scorestring = "Score: %s" % score
score_pen.clear()
score_pen.write(scorestring, False, align='left', font=SMALL_FONT)
if isCollision(player, enemy):
player.hideturtle()
enemy.hideturtle()
game_over.write(gameOverMsg, False, align='center', font=LARGE_FONT)
collision = True
break
if any(enemy.xcor() < -280 or enemy.xcor() > 280 for enemy in enemies):
# Move all enemies down
for enemy in enemies:
enemy.sety(enemy.ycor() - 40)
# Change enemy direction
enemy_speed *= -1
# Move the bullet
if bullet.isvisible():
bullet.forward(BULLET_SPEED)
# Check border collision
if bullet.ycor() > 290:
bullet.hideturtle()
if not collision:
wn.ontimer(move, 50)
move()
wn.mainloop()
This program is déjà vu for me with respect to this question so see my answer to it for more ideas. Also, see this answer to another space invaders game in turtle to see what you can learn from it to polish your game.

Python Turtle Space Invaders bullets not hitting invaders

I'm very new to turtle and python in general, so I apologize if my code seems messy.
I'm currently coding a turtle version of space invaders and I can't figure out why my invaders aren't dying when they hit the turtles. The code seems to be the same as all the other space invader turtle programs I can find. Any help would be greatly appreciated.
import turtle
import random
import math
#### Player ####
player1 = turtle.Turtle()
player1.shape("arrow")
player1.color("white")
player1.speed(0)
player1.penup()
player1.setheading(90)
player1.setposition(0, -175)
player1.speed(4)
def move_right():
x = player1.xcor()
x += playerspeed
if x > 210:
x = 210
player1.setx(x)
def move_left():
x = player1.xcor()
x -= playerspeed
if x < -210:
x = -210
player1.setx(x)
bulletlist = []
def fire():
if len(bulletlist) < 5:
bulletlist.append(Bullet())
#### Window ####
win = turtle.Screen()
win.bgcolor("black")
win.tracer(2)
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-225, -225)
border_pen.pensize(3)
border_pen.pendown()
for side in range(4):
border_pen.fd(450)
border_pen.lt(90)
border_pen.hideturtle()
### Invader ###
class Invader(turtle.Turtle):
def __init__(self, xcor, ycor):
turtle.Turtle.__init__(self)
self.color("red")
self.shape("turtle")
self.penup()
self.speed(0)
self.setposition(xcor, ycor)
self.setheading(270)
self.speed(1)
self.hit = 0
### Bullet ###
bulletstate = 1
class Bullet(turtle.Turtle):
def __init__(self):
turtle.Turtle.__init__(self)
self.color("yellow")
self.shape("arrow")
self.shapesize(.5, 1)
self.penup()
self.speed(0)
self.setheading(90)
self.hideturtle()
self.bulletspeed = 20
fire()
def bullet_movement():
global firedb
firedb = bulletlist[-1]
firedb.speed(0)
ybul = firedb.ycor()
ybul += 10
firedb.sety(ybul)
if ybul > 195:
firedb.hideturtle()
def fire_bullet():
print("fire!")
firedb = bulletlist[-1]
firedb.hideturtle()
x = player1.xcor()
y = player1.ycor() + 20
firedb.setpos(x, y)
firedb.showturtle()
if firedb.ycor() > 200:
firedb.hideturtle()
bulletlist.remove(firedb)
def bulletmake():
bulletfire = Bullet()
def bulletmechanics():
fire()
fire_bullet()
win.listen()
win.onkey(move_left, "Left")
win.onkey(move_right, "Right")
win.onkey(fire_bullet, "space")
enemies = []
xx = -175
xxxx = -175
for invader in range(19):
if invader <= 9:
enemies.append(Invader(xx, 200))
xx += 30
if invader >= 9:
enemies.append(Invader(xxxx, 175))
xxxx += 30
playerspeed = 7
enemyspeed = 8
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
dead = []
while True:
global firedb
firedb = bulletlist[-1]
fire()
bullet_movement()
for Invader in enemies:
bullet_movement()
# if Invader.distance(firedb.xcor()+200, firedb.ycor()+200) < 1:
if isCollision(bulletlist[-1], Invader) == True:
print("AAAAAAAAGH")
isCollision(firedb, Invader)
Invader.hit += 1
if Invader.hit == 1:
Invader.color("orange")
if Invader.hit == 2:
Invader.color("yellow")
if Invader.hit == 3:
Invader.speed(0)
Invader.setpos(1000, 1000)
dead.append(Invader)
Invader.hideturtle()
for invader in enemies:
bullet_movement()
xpos = invader.xcor()
invader.speed(0)
xpos += enemyspeed
invader.setx(xpos)
if invader.xcor() > 200:
for invader in enemies:
ypos = invader.ycor()
ypos -= 40
invader.sety(ypos)
enemyspeed *= -1
if invader.xcor() < -200:
for invader in enemies:
y = invader.ycor()
y -= 40
invader.sety(y)
enemyspeed *= -1
I belive your problem is the way you interlace bullet movements with invader movements. In your while True: loop (which shouldn't exist in an event-driven world like turtle) you call bullet_movement() at the top level, then in a for loop and again within a nested for loop. So where you see the bullet and where it is relative to invaders aren't necessarily the same.
I've reworked your code, and for example purposes, simplified it in places. Along with the above issues, I've also modified the code to use turtle tilt which is perfect for this sort of game where the turtles face in one direction but move in another. And I've completely trashed your isCollision() function as turtles already know how to compute their distance from another turtle.
from turtle import Screen, Turtle
class Invader(Turtle):
def __init__(self, xcor, ycor):
super().__init__(shape="turtle")
self.color("red")
self.penup()
self.setposition(xcor, ycor)
self.tilt(-90)
self.speed('slow')
self.hit = 0
class Bullet(Turtle):
def __init__(self):
super().__init__(shape="arrow", visible=False)
self.shapesize(.5, 1)
self.color("yellow")
self.penup()
self.setheading(90)
self.speed('fastest')
self.bulletspeed = 20
def move_right():
if player.xcor() + playerspeed < 210:
player.forward(playerspeed)
def move_left():
if player.xcor() - playerspeed > -210:
player.backward(playerspeed)
def bullet_movement():
firedb.forward(10)
if firedb.ycor() > 195:
firedb.hideturtle()
def fire_bullet():
if not firedb.isvisible():
x, y = player.position()
firedb.setposition(x, y + 20)
firedb.showturtle()
def isCollision(t1, t2):
return t1.distance(t2) < 20
def move():
global enemyspeed
if firedb.isvisible():
bullet_movement()
for invader in enemies:
if firedb.isvisible() and isCollision(firedb, invader):
invader.hit += 1
if invader.hit == 1:
invader.color("orange")
elif invader.hit == 2:
invader.color("yellow")
elif invader.hit == 3:
invader.hideturtle()
enemies.remove(invader)
firedb.hideturtle() # only one hit per bullet!
invader.forward(enemyspeed)
if invader.xcor() < -200 or invader.xcor() > 200:
for invader in enemies:
y = invader.ycor() - 40
invader.sety(y)
enemyspeed *= -1
win.update()
win.ontimer(move, 50)
#### Player ####
player = Turtle("arrow")
player.color("white")
player.penup()
player.tilt(90)
player.sety(-175)
player.speed('fast')
#### Window ####
win = Screen()
win.bgcolor("black")
win.tracer(False)
border_pen = Turtle(visible=False)
border_pen.speed('fastest')
border_pen.color("white")
border_pen.pensize(3)
border_pen.penup()
border_pen.setposition(-225, -225)
border_pen.pendown()
for _ in range(4):
border_pen.forward(450)
border_pen.left(90)
### Bullet ###
firedb = Bullet()
enemies = []
xx = -175
xxxx = -190
for invader_count in range(19):
if invader_count < 9:
enemies.append(Invader(xx, 200))
xx += 30
else:
enemies.append(Invader(xxxx, 175))
xxxx += 30
playerspeed = 7
enemyspeed = 2
win.onkey(move_left, "Left")
win.onkey(move_right, "Right")
win.onkey(fire_bullet, "space")
win.listen()
move()
win.mainloop()
Finally, be careful with Python global and turtle tracer() which are routinely misunderstood.

how to target a sprite randomly

I am trying to create a retro style 2d shooter game and am currently making a homing missile. I need to randomly select a sprite from a group (pygame.sprite.Group) and find out its x and y coords. I have already made it home in on these coords so I do not need help with that. My code is:
an Enemy class (they are all the same just with different sizes and pictures):
class EnemyShipLevel1(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("C:/Users /Me/PycharmProjects/Game folder/SpaceShip game/Enemy1.png")
self.rect = self.image.get_rect()
self.rect.y = -50
self.rect.centerx = random.randint(15, SCREEN_WIDTH-15)
self.change_y = 0
self.cooldown = 0
self.targetx = 500
self.health = 1
if self.targetx >= self.rect.centerx:
self.change_x = 2
else:
self.change_x = -2
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
if self.rect.centery < 200:
self.rect.centery += 2
self.get_target()
if self.targetx > self.rect.centerx+10:
self.change_x = 3
elif self.targetx < self.rect.centerx-10:
self.change_x = -3
else:
self.change_x = 0
self.cooldown -= 1
if self.rect.left < screen_rect.left:
self.change_x = -self.change_x
if self.rect.right > screen_rect.right:
self.change_x = -self.change_x
if self.cooldown < 0 and (self.rect.centerx-50 < self.targetx < self.rect.centerx+50) and player.trans is False:
self.cooldown = random.randint(15, 25)
laser = NormalLaser(self.rect.centery, self.rect.centerx, False, 0, 8)
all_sprite_list.add(laser)
enemy_laser_list.add(laser)
player_laser_hit_list = pygame.sprite.spritecollide(self, player_laser_list, True)
for laser in player_laser_hit_list:
if self.health < 1:
all_sprite_list.remove(self)
enemy_list.remove(self)
else:
self.health -= 1
def get_target(self):
self.targetx = player.rect.centerx
Targeting Laser class (I know you can't bend lasers):
class TargetingLaser(pygame.sprite.Sprite):
def __init__(self, start_x, start_y, dest_x, dest_y):
super().__init__()
self.floating_point_x = start_x
self.floating_point_y = start_y
self.x_diff = dest_x - start_x
self.y_diff = dest_y - start_y
self.angle = math.atan2(self.y_diff, self.x_diff)
self.velocity = 10
self.change_x = math.cos(self.angle) * self.velocity
self.change_y = math.sin(self.angle) * self.velocity
self.image = pygame.image.load("C:/Users/Minecraft/PycharmProjects/Game folder/SpaceShip game/Laser.png")
self.image = pygame.transform.rotate(self.image, -(math.degrees(self.angle)+90))
self.rect = self.image.get_rect()
self.rect.centerx = start_x
self.rect.centery = start_y
def update(self):
self.floating_point_y += self.change_y
self.floating_point_x += self.change_x
self.rect.y = int(self.floating_point_y)
self.rect.x = int(self.floating_point_x)
if self.rect.top < screen_rect.top:
player_laser_list.remove(self)
enemy_laser_list.remove(self)
all_sprite_list.remove(self)
if self.rect.bottom > screen_rect.bottom:
player_laser_list.remove(self)
all_sprite_list.remove(self)
enemy_laser_list.remove(self)
if self.rect.left < screen_rect.left:
player_laser_list.remove(self)
all_sprite_list.remove(self)
enemy_laser_list.remove(self)
if self.rect.right > screen_rect.right:
player_laser_list.remove(self)
all_sprite_list.remove(self)
enemy_laser_list.remove(self)
Game Loop:
all_sprite_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
player_list = pygame.sprite.GroupSingle()
player_laser_list = pygame.sprite.Group()
enemy_laser_list = pygame.sprite.Group()
live_list = pygame.sprite.Group()
health_bar_list = pygame.sprite.GroupSingle()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
screen_rect = screen.get_rect()
pygame.display.set_caption('Game')
clock = pygame.time.Clock()
Play = True
player = Player(SCREEN_WIDTH/2, SCREEN_HEIGHT-30)
all_sprite_list.add(player)
player_list.add(player)
for counter in range(2):
enemy = EnemyShipLevel1()
all_sprite_list.add(enemy)
enemy_list.add(enemy)
live1 = LivesIcon(5, 0, 1)
live_list.add(live1)
live1 = LivesIcon(27, 30, 2)
live_list.add(live1)
live1 = LivesIcon(50, 0, 3)
live_list.add(live1)
live1 = LivesIcon(72, 30, 4)
live_list.add(live1)
live1 = LivesIcon(95, 0, 5)
live_list.add(live1)
all_sprite_list.add(live_list)
health_bar = PlayerHealthBar(0, 690)
all_sprite_list.add(health_bar)
health_bar_list.add(health_bar)
ship = EnemyShipLevel3()
all_sprite_list.add(ship)
enemy_list.add(ship)
while Play is True:
# Shooting
if player.shooting is True:
if player.laser_cooldown < 0:
player.laser_cooldown = 5
if player.last_shot == "left":
player.last_shot = "right"
laser = NormalLaser(player.rect.centery-2, player.rect.centerx + 11, True, 0, 8)
all_sprite_list.add(laser)
player_laser_list.add(laser)
else:
player.last_shot = "left"
laser = NormalLaser(player.rect.centery-2, player.rect.centerx - 11, True, 0, 8)
all_sprite_list.add(laser)
player_laser_list.add(laser)
if player.rocket_cooldown < 0:
player.rocket_cooldown = 15
for counter in range(10):
laser = TargetingLaser(player.rect.centerx,player.rect.centery, player.rect.centerx, 0)
all_sprite_list.add(laser)
player_laser_list.add(laser)
# Shooting
# Levels
if len(enemy_list) == 0:
all_sprite_list.remove(enemy_laser_list)
all_sprite_list.remove(player_laser_list)
enemy_laser_list.empty()
player_laser_list.empty()
player.level += 1
for counter in range(player.spawn_enemies1):
enemy = EnemyShipLevel1()
all_sprite_list.add(enemy)
enemy_list.add(enemy)
for counter in range(player.spawn_enemies2):
enemy2 = EnemyShipLevel2()
all_sprite_list.add(enemy2)
enemy_list.add(enemy2)
for counter in range(player.spawn_enemies3):
ship = EnemyShipLevel3()
all_sprite_list.add(ship)
enemy_list.add(ship)
if player.level == 2:
player.spawn_enemies1 = 3
player.spawn_enemies2 = 1
if player.level == 3:
player.spawn_enemies1 = 5
player.spawn_enemies2 = 2
if player.level == 4:
player.spawn_enemies1 = 10
player.spawn_enemies2 = 5
if player.level == 5:
player.spawn_enemies1 = 12
player.spawn_enemies2 = 8
player.spawn_enemies3 = 1
# Levels
# End Game
if player.lives == 0:
Play = False
# End Game
# Keys
for event in pygame.event.get():
if event.type == pygame.QUIT:
Play = False
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_a) or (event.key == pygame.K_LEFT):
player.change_x = -5
elif (event.key == pygame.K_d) or (event.key == pygame.K_RIGHT):
player.change_x = 5
if event.key == pygame.K_SPACE:
player.shooting = True
elif event.type == pygame.KEYUP:
player.rotation = 0
player.change_x = 0
player.change_y = 0
if event.key == pygame.K_SPACE:
player.shooting = False
# Keys
all_sprite_list.update()
screen.fill(BLACK)
all_sprite_list.draw(screen)
enemy_laser_list.draw(screen)
player_laser_list.draw(screen)
pygame.display.update()
clock.tick(60)
quit()
Each time you kill all of the spawned enemies then the game spawns in more and adds them to enemy_list().
If you are asking how to randomly choose a sprite then use a random.randint(0, 30) (or the number of sprites) then do a long series of if statements. Example Code.
import random
import pygame
class Enemy:
def __init__()
random_sprite = random.randint(0, 1)
# Then put in two variables which equal to the png (I will use sprite1 and sprite2)
def sprite_render
if random_sprite == 0:
# Just blit sprite1
elif random_sprite == 1:
# Just blit sprite2
(The reason why I just commented the actions for the image stuff is because almost everyone does it in a different way and that is not the main point of this answer.)
You could assign a number attribute to each enemy.
For example, you could start off your enemy function like this:
class EnemyShipLevel1(pygame.sprite.Sprite):
number = None
def __init__(self,number):
super().__init__()
self.number = number
self.image = pygame.image.load("C:/Users /Me/PycharmProjects/Game folder/SpaceShip game/Enemy1.png")
Now when you spawn in the enemies, you would add the number attribute:
for counter in range(2): # change this number to use more than 2 enemies (I use 30 later)
enemy = EnemyShipLevel1(counter)
all_sprite_list.add(enemy)
enemy_list.add(enemy)
All you have to do is randomly choose the sprite:
choice = random.randrange(0,30) # between 0 and the number of enemies spawned
for i in enemy_list:
if i.number == choice:
# make i the target of the laser
target_x = i.rect.x
target_y = i.rect.y

Categories

Resources