Pygame - collision between two objects not working - python

I'm making trying to recreate 'Pong' and I'm struggling to figure out what is wrong with the collision. Here's the code:
ball_x += ball_x_change
collide = player_collision(player_x, ball_x, player_y, ball_y)
if collide:
ball_x -= ball_x_change
ball(ball_x, ball_y)
This is meant to move the ball from its starting position towards the player and check if the player icon and the ball has collided. If its has, it will change the direction of the ball. It then renders the ball on the screen.
Here's the whole thing:
import pygame
import math
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pong")
icon = pygame.image.load("ping-pong.png")
pygame.display.set_icon(icon)
player_icon = pygame.image.load("vertical-line.png")
player_x = 800 - (64 + 32)
player_y = 236
player_y_change = 0
cpu_icon = pygame.image.load("vertical-line.png")
cpu_x = 0 - 32
cpu_y = 236
cpu_y_change = 0
ball_icon = pygame.image.load("oval.png")
ball_x = 384
ball_y = 284
ball_x_change = 0.5
ball_y_change = 0
def player(player_x, player_y):
screen.blit(player_icon, (player_x, player_y))
def cpu(cpu_x, cpu_y):
screen.blit(cpu_icon, (cpu_x, cpu_y))
def ball(ball_x, ball_y):
screen.blit(ball_icon,(ball_x, ball_y))
def player_collision(ball_x, ball_y, player_x, player_y):
bump = math.hypot(player_x - ball_x, player_y - ball_y)
if bump < 20:
return True
else:
return False
running = True
while running:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
player_y_change += 1
if event.key == pygame.K_UP:
player_y_change -= 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or pygame.K_UP:
player_y_change = 0
player_y += player_y_change
if player_y <= 0:
player_y = 0
elif player_y >= 472:
player_y = 472
player(player_x, player_y)
cpu_y += cpu_y_change
if cpu_y <= 0:
player_y = 0
elif player_y >= 472:
player_y = 472
cpu(cpu_x, cpu_y)
ball_x += ball_x_change
collide = player_collision(player_x, ball_x, player_y, ball_y)
if collide:
ball_x -= ball_x_change
ball(ball_x, ball_y)
pygame.display.update()
Thanks for your time.
Updated the code and it still doesn't work:
import pygame
import math
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Pong")
icon = pygame.image.load("ping-pong.png")
pygame.display.set_icon(icon)
player_icon = pygame.image.load("vertical-line.png")
player_x = 672
player_y = 236
player_y_change = 0
cpu_icon = pygame.image.load("vertical-line.png")
cpu_x = 0
cpu_y = 236
cpu_y_change = 0
ball_icon = pygame.image.load("oval.png")
ball_x = 384
ball_y = 284
ball_x_change = 0.8
ball_y_change = 0
def player(player_x, player_y):
screen.blit(player_icon, (player_x, player_y))
def cpu(cpu_x, cpu_y):
screen.blit(cpu_icon, (cpu_x, cpu_y))
def ball(ball_x, ball_y):
screen.blit(ball_icon,(ball_x, ball_y))
def player_collision(ball_x, player_x, ball_y, player_y):
touch = math.hypot(player_x - ball_x, ball_y - player_y)
if touch < 10:
return True
else:
return False
running = True
while running:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
player_y_change += 1
if event.key == pygame.K_UP:
player_y_change -= 1
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN or pygame.K_UP:
player_y_change = 0
player_y += player_y_change
if player_y <= 0:
player_y = 0
elif player_y >= 472:
player_y = 472
player(player_x, player_y)
cpu_y += cpu_y_change
if cpu_y <= 0:
player_y = 0
elif player_y >= 472:
player_y = 472
cpu(cpu_x, cpu_y)
ball_x += ball_x_change
collide = player_collision(ball_x, player_x, ball_y, player_y)
if collide:
ball_x_change = -ball_x_change
ball_x += ball_x_change
ball(ball_x, ball_y)
pygame.display.update()

The function player_collision takes its arguments in (ball_x, ball_y, player_x, player_y), but when you call the function, you give the arguments in the wrong order
collide = player_collision(player_x, ball_x, player_y, ball_y)
#should be
collide = player_collision(ball_x, ball_y, player_x, player_y)
You should also do what #RFairey suggested and add ball_x_change *= -1

Looks like when collision occurs, you only move the ball back one step in x:
if collide:
ball_x -= ball_x_change
To flip its direction you need to negate ball_x_change as well, so that it travels in the other direction after a bounce:
if collide:
ball_x -= ball_x_change
ball_x_change = -ball_x_change

Related

How to add game restart in this pgame code? [duplicate]

This question already has answers here:
how restarting game on pygame works
(1 answer)
Pygame restart function
(2 answers)
In game loop, how do I restart game properly using nested class or loop?
(1 answer)
Reset and restart pygame program doesn't work
(1 answer)
Closed 3 months ago.
I followed a tutorial guide on how to make this game but they never said how to put in a restart. I'm not quite sure how to do it, would anyone here know off the top of their head? Here's the code.
import pygame
import math
import random
from pygame import mixer
# Start the Program
pygame.init()
# Create the Screen
screen = pygame.display.set_mode((800, 600))
running = True
# Background
background = pygame.image.load('background.png')
# Background Sound
mixer.music.load('theme.mp3')
mixer.music.play(-1)
# Title and Icon
pygame.display.set_caption("Boot Craft")
icon = pygame.image.load('Starcraft.png')
pygame.display.set_icon(icon)
# Player
player_image = pygame.image.load('BC.png')
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
# Enemy
enemy_image = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
enemy_image.append(pygame.image.load('Zergling.png'))
enemyX.append(random.randint(0, 768))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.2)
enemyY_change.append(40)
# Bullet
# Ready - You can't see the bullet on the screen
# Fire - The bullet is currently moving
bullet_image = pygame.image.load('Bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = .5
bullet_state = "ready"
# Score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
textX = 10
textY = 10
# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
def show_score(x,y):
score = font.render('Score: ' + str(score_value), True, (255,255,255))
screen.blit(score, (x, y))
def game_over_text():
over_text = over_font.render('GAME OVER', True, (255, 255, 255))
continue_text = over_font.render('CONTINUE? Y/N', True, (255, 255, 255))
screen.blit(over_text, (200, 250))
screen.blit(continue_text, (160, 350))
def player(x, y):
screen.blit(player_image, (x, y))
def enemy(x, y, i):
screen.blit(enemy_image[i], (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_image, (x + 8, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# Game Loop - Runs Game
while running:
# RGB Color Values 0-255
screen.fill((0, 0, 0))
# Background Image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y:
running = True
# Check Key pressed.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
playerX_change = -0.2
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
playerX_change = 0.2
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_sound = mixer.Sound('laser.wav')
bullet_sound.play()
# Get the current X coordinate of the spaceship
bulletX = playerX
fire_bullet(bulletX, bulletY)
# Check Key released.
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_a or event.key == pygame.K_d:
playerX_change = 0
playerX += playerX_change
# Adds Boundary
if playerX <= 0:
playerX = 0
elif playerX >= 721:
playerX = 721
# Enemy Movement
for i in range(num_of_enemies):
# Game Over
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
game_over_text()
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.2
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 768:
enemyX_change[i] = -0.2
enemyY[i] += enemyY_change[i]
# Collision
collision = isCollision(enemyX[i],enemyY[i],bulletX,bulletY)
if collision:
explosion_sound = mixer.Sound('explode.wav')
explosion_sound.play()
bulletY = 480
bullet_state = "ready"
score_value += 1
enemyX[i] = random.randint(0, 768)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, textY)
pygame.display.update()
I'm not sure what to try, as what I googled used different ways of running the game. New to this, so genuinely confused.

Pygame displays a black screen

I did some research to see if I could fix my problem on my own but it has been fruitless so far. I've checked to see if I ran the .display command too much but I haven't, the program will run without error but the screen appears black except when I close the window out, you can catch a glimpse of what it is supposed to display. Any insight into what is wrong? Help is greatly appreciated!
import math
import random
import pygame
from pygame import mixer
# Initialize the pygame
pygame.init()
# create the screen
screen = pygame.display.set_mode((800, 600))
# Background
background = pygame.image.load('space background.png')
# Background Sound
# come back to this from bg music 'mixer.music.load('insert file name')'
# mixer.music.play(-1)
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# Player
playerImg = pygame.image.load('ship.png')
playerX = 370
playerY = 480
playerX_change = 0
# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load('enemy.png'))
enemyX.append(random.randint(0, 735))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.3)
enemyY_change.append(40)
# Bullet
# Ready - you cat see the bullet on the screen
# Fire - The bullet is currently moving
bulletImg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 1 # Bullet speed
bullet_state = "ready"
# Score
score_value = 0
font = pygame.font.Font('Minecraft.ttf', 24)
textX = 10
testY = 10 # might have to change back to 'text'
# Game Over text
over_font = pygame.font.Font('Minecraft.ttf', 64)
def show_score(x, y):
score = font.render("Score : " + str(score_value), True, (225, 225, 255))
screen.blit(score, (x, y))
def game_over_text(x, y):
over_text = over_font.render("GAME OVER", True, (225, 225, 255))
screen.blit(over_text, (200, 250))
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y, i):
screen.blit(enemyImg[i], (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
def iscollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt(math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2))
if distance < 27:
return True
else:
return False
# Game Loop
running = True
while running:
# RGB
screen.fill((0, 0, 0))
# background image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether its right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.7 # player speed
if event.key == pygame.K_RIGHT:
playerX_change = 0.7
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bullet_Sound = mixer.Sound('laser.wav')
bullet_Sound.play()
# Get the current X corrdinate of te spaceship
bulletX = playerX
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# 5 = 5 + -0.1 -> 5 = 5 - 0.1
# 5 = 5 + 0.1
# Checking for boundries of spaceship
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Enemy Movement
for i in range(num_of_enemies):
# Game over
if enemyY[i] > 440:
for j in range(num_of_enemies):
enemyY[j] = 2000
game_over_text()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 0.3 # enemy speed
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 736:
enemyX_change[i] = -0.3
enemyY[i] += enemyY_change[i]
# Collision
collision = iscollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
explosion_Sound = mixer.Sound('explosion.wav')
explosion_Sound.play()
bulletY = 480
bullet_state = "ready"
score_value += 100
enemyX[i] = random.randint(0, 736)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
show_score(textX, testY)
pygame.display.update()
All you have to do is indent the last bit of code (the 11 lines under the comment Bullet Movement) one level in. Right now, it's not under the while loop, which explains why the screen is black and why it only appears after you click the close button (which exits the loop and executes the drawing code). Hope this helps!

The pygame code and the error is mentioned below: [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Here is a source code of my space-arcade pygame. It's a quite simple one and doesn't shoots bullets. Only that we can move the player with the obstacles being moved as well. However, the limit for the spaceship (player) to move in the screen has been set from x = 0 to x = 765 and from y = 200 to y = 480. But the spaceship somehow cross the limit for y-axis i.e it can move beyond y = 0 and y = 600, but only at some specific x-axis like when x is 10 and 590 something. How can i fix this gap in the following python code:
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("space-invaders.png")
pygame.display.set_icon(icon)
background = pygame.image.load("background.png")
player_image = pygame.image.load("space-invaders.png")
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
def player(x, y):
screen.blit(player_image, (x, y))
obs_image = pygame.image.load("cave-painting (1).png")
obsX = random.randint(0, 800)
obsY = random.randint(0, 100)
obsX_change = 2
obsY_change = 0.3
def obstacle(x, y):
screen.blit(obs_image, (x, y))
running = True
while running:
screen.fill( (0, 0,0) )
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_UP:
playerY_change = -4
if event.key == pygame.K_DOWN:
playerY_change = 4
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
playerY += playerY_change
# Stopping our player beyond screen:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
elif playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
obsX += obsX_change
obsY += obsY_change
# Movement mechanics for obstacles:
if obsX <= 0:
obsX_change = 3
obsY += obsY_change
elif obsX >= 736:
obsX_change = -3
obsY += obsY_change
player(playerX, playerY)
obstacle(obsX, obsY)
pygame.display.update()```
Here are the source pictures used in the aforementioned code:
[background][1]
[cave-painting (1)][2]
[space-invaders][3]
[1]: https://i.stack.imgur.com/EZFLs.png
[2]: https://i.stack.imgur.com/CxNs4.png
[3]: https://i.stack.imgur.com/Z97n7.png
Don't use elif. You're code says (pseudocode):
If x is beyond or equal to 0 then go back to zero
If x is not beyond or equal to 0 but x is beyond or equal to 765 then go back to 765
If none of the above are true but y is beyond or equal to 200 then go back to 200
Do you see the problem? If x is equal to 0, then the y-checks never occur. Change the third elif to if to fix this issue. Here's the fixed code:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
if playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
These comparisons should be independent from each other.
By chaining then in a single if/elif/elif/... block, the checks
beyond the first only take place if the first check fails, and so on.
Given the nature of your problem, this is likely the cause.
Just replace the elif statements by plain ifs: each check will
run independently and fix the limits it should guard:
# Stopping our player beyond screen:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
elif playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
You can simplify you code. Use pygame.key.get_pressed() to move the player smoothly, rather than the keyboard events KEYDOWN and KEYDUP. pygame.key.get_pressed() returns a list with the state of each key. If a key is held down, the state for the key is True, otherwise False:
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 4
Use pygame.Rect objects and pygame.Rect.clamp() respectively pygame.Rect.clamp_ip() to limit the position of the player to a rectangular area.
border_rect = pygame.Rect(0, 200, 800, 312)
player_rect = player_image.get_rect(topleft = (playerX, playerY))
player_rect.clamp_ip(border_rect)
playerX, playerY = player_rect.topleft
Or even shorter:
keys = pygame.key.get_pressed()
playerX, playerY = player_image.get_rect(topleft = (playerX, playerY)) \
.move((keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4, \
(keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 4) \
.clamp((0, 200, 800, 312)).topleft
Example code:
running = True
while running:
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 4
border_rect = pygame.Rect(0, 200, 800, 312)
player_rect = player_image.get_rect(topleft = (playerX, playerY))
player_rect.clamp_ip(border_rect)
playerX, playerY = player_rect.topleft
obsX += obsX_change
obsY += obsY_change
# Movement mechanics for obstacles:
if obsX <= 0:
obsX_change = 3
obsY += obsY_change
elif obsX >= 736:
obsX_change = -3
obsY += obsY_change
player(playerX, playerY)
obstacle(obsX, obsY)
pygame.display.update()

The pygame code is mentioned and needs some modifications as specified: [duplicate]

This question already has answers here:
How can i shoot a bullet with space bar?
(1 answer)
How do I stop more than 1 bullet firing at once?
(1 answer)
Closed 2 years ago.
Here is a source code of my space-arcade pygame. It's a quite simple one and does shoots bullets as well but for now these bullets do not affect the obstacles coming in the screen.
Question: How can I shoot multiple bullets just after firing the first bullet and otherwise not waiting for the first bullet to pass y <= 0 to fire another bullet?
Please help me with this issue and if possible re-write this code with some adjustments made
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("space-invaders.png")
pygame.display.set_icon(icon)
background = pygame.image.load("background.png")
player_image = pygame.image.load("space-invaders.png")
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
def player(x, y):
screen.blit(player_image, (x, y))
obs_image = pygame.image.load("cave-painting (1).png")
obsX = random.randint(0, 800)
obsY = random.randint(0, 100)
obsX_change = 2
obsY_change = 0.3
def obstacle(x, y):
screen.blit(obs_image, (x, y))
bullet_image = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 0
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
def fire_bullet(x, y):
global bullet_state # Globaling it, inorder to access it inside this func.
bullet_state = "fire"
screen.blit(bullet_image, (x,y))
running = True
while running:
screen.fill( (0, 0,0) )
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_UP:
playerY_change = -4
if event.key == pygame.K_DOWN:
playerY_change = 4
if event.key == pygame.K_SPACE:
if bullet_state is "ready":
bulletX = playerX
bulletY = playerY
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
playerY += playerY_change
# Stopping our player beyond screen:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
elif playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
obsX += obsX_change
obsY += obsY_change
# Movement mechanics for obstacles:
if obsX <= 0:
obsX_change = 3
obsY += obsY_change
elif obsX >= 736:
obsX_change = -3
obsY += obsY_change
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
obstacle(obsX, obsY)
pygame.display.update()
Also Here are the source pictures used in the aforementioned code:
background
cave-painting (1)
space-invaders
bullet
You have to add a list of bullets:
bullet_list = []
When a new bullet spawns then add a new position to the head of the bullet list:
if event.key == pygame.K_SPACE:
bullet_list.insert(0, [playerX, playerY])
Move and draw the bullets in a loop. Delete the bullets which are out of the window from the list:
while running:
# [...]
for i in range(len(bullet_list)):
bullet_list[i][1] -= bulletY_change
if bullet_list[i][1] < 0:
del bullet_list[i:]
break
# [...]
for bullet in bullet_list:
screen.blit(bullet_image, bullet)
Complete example:
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("space-invaders.png")
pygame.display.set_icon(icon)
background = pygame.image.load("background.png")
player_image = pygame.image.load("space-invaders.png")
playerX = 370
playerY = 480
playerX_change = 0
playerY_change = 0
def player(x, y):
screen.blit(player_image, (x, y))
obs_image = pygame.image.load("cave-painting (1).png")
obsX = random.randint(0, 800)
obsY = random.randint(0, 100)
obsX_change = 2
obsY_change = 0.3
def obstacle(x, y):
screen.blit(obs_image, (x, y))
bullet_image = pygame.image.load("bullet.png")
bulletX_change = 0
bulletY_change = 10
bullet_list = []
def draw_bullets(bullets):
for bullet in bullets:
screen.blit(bullet_image, bullet)
running = True
while running:
clock.tick(60)
screen.fill( (0, 0,0) )
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_UP:
playerY_change = -4
if event.key == pygame.K_DOWN:
playerY_change = 4
if event.key == pygame.K_SPACE:
bullet_list.insert(0, [playerX, playerY])
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0
playerX += playerX_change
playerY += playerY_change
# Stopping our player beyond screen:
if playerX <= 0:
playerX = 0
elif playerX >= 765:
playerX = 765
elif playerY <= 200:
playerY = 200
elif playerY >= 480:
playerY = 480
obsX += obsX_change
obsY += obsY_change
# Movement mechanics for obstacles:
if obsX <= 0:
obsX_change = 3
obsY += obsY_change
elif obsX >= 736:
obsX_change = -3
obsY += obsY_change
for i in range(len(bullet_list)):
bullet_list[i][1] -= bulletY_change
if bullet_list[i][1] < 0:
del bullet_list[i:]
break
player(playerX, playerY)
obstacle(obsX, obsY)
draw_bullets(bullet_list)
pygame.display.update()

Enemy sprites not appearing in pygame until hit

I am making a space invader like game, everything works fine, but ever since I added multiple enemies using a list, their images/sprites disappeared and only reappear once hit for a second and than disappear once more.
I've checked, the image file is fine, and as far as I can see nothing unusual is happening behind the since as well, and the score adds up fine whenever an enemy is hit.
What might be the issue?
import pygame
import random
import math
# Initializing pygame
pygame.init()
# Screen creation and config
screen = pygame.display.set_mode((1024, 600))
# Background
background = pygame.image.load('court_level_bg.jpg')
# Title and Icon
pygame.display.set_caption("Space Invaders")
# Player config
player_img = pygame.image.load('Mini_Bibi.png')
playerX = 430
playerY = 480
playerX_change = 0
# Enemy config
enemy_img = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_enemies = 5
for i in range(num_enemies):
enemy_img.append(pygame.image.load('Enemy1.png'))
enemyX.append(random.randint(0, 1024))
enemyY.append(random.randint(5, 150))
enemyX_change.append(1.5)
enemyY_change.append(40)
# Bullet config
bullet_img = pygame.image.load('Weapon.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 6
bullet_state = "ready"
score = 0
# Create player
def player(x, y):
screen.blit(player_img, (x, y))
# Create enemy
def enemy(x, y, i):
screen.blit(enemy_img[i], (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bullet_img, (x + 16, y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
if distance < 27:
return True
else:
return False
# Game Loop
scrn_stat = True
while scrn_stat:
# Screen RGB color config
screen.fill((0, 0, 0))
# Background image
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
scrn_stat = False
# X axis Keystroke listening
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -4
if event.key == pygame.K_RIGHT:
playerX_change = 4
if event.key == pygame.K_SPACE:
if bullet_state is "ready":
bulletX = playerX
fire_bullet(bulletX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
# Player boundaries
if playerX <= 0:
playerX_change = 0;
elif playerX >= 945:
playerX_change = 0;
# Enemy movement
for i in range(num_enemies):
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 3
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 945:
enemyX_change[i] = -3
enemyY[i] += enemyY_change[i]
# Collision
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX[i] = random.randint(0, 945)
enemyY[i] = random.randint(50, 150)
enemy(enemyX[i], enemyY[i], i)
# Bullet Movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state is "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
player(playerX, playerY)
pygame.display.update()
enemy() has to be called for each enemy in every frame, rather than in case of a collision only. It is an Indentation issue:
for i in range(num_enemies):
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 3
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 945:
enemyX_change[i] = -3
enemyY[i] += enemyY_change[i]
# Collision
collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX[i] = random.randint(0, 945)
enemyY[i] = random.randint(50, 150)
#<--|
enemy(enemyX[i], enemyY[i], i)

Categories

Resources