i have tried making bullet in pygame using this tutorial https://youtu.be/8wEYDLVrAB8 but i have failed to do so here is commented code ,
. if problem arise please leave a comment on it . thank you for helping .i have already asked it once in this website but it didnt work [![ this is current state when firing bullet][1]][1] . i am running it in pycharm
import pygame
import random
#module importing
pygame.init()
pygame.display.init()
#screen creation
screen = pygame.display.set_mode((800, 600))
bacground = pygame.image.load("background.png")
#player information
playerimg = pygame.image.load("spaceship.png")
playerX_change = 0
playerX = 360
playerY = 480
#enemy information
enemyimg = pygame.image.load("enemy.png")
enemyX = random.randint(0,736)
enemyY = random.randint(50,150)
enemyX_change = 4
enemyY_change = 40
#bullet information
bulletimg = pygame.image.load("enemy.png")
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 40
bullet_state = "ready"
#all three functions
def player(x, y):
screen.blit(playerimg, (x, y))
#enemy draw function
def enemy(x, y):
screen.blit(enemyimg, (x, y))
#bullet fire function
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletimg, (x + 16, y+10))
# drawing player on screen
# game loop
running = True
while running == True:
#stuff happening in game loop
screen.fill((200, 200, 0))
screen.blit(bacground,(0,0))
player(playerX, playerY)
enemy(enemyX, enemyY)
if bullet_state == "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
#detection of the keys pressed and consequences
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
player(playerX, playerY)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
fire_bullet(bulletX,bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
#player and enemy movement system
playerX += playerX_change
enemyX += enemyX_change
#enemy and player collision detection
if enemyX <= 0:
enemyX_change = 4
enemyY += enemyY_change
if enemyX>= 736:
enemyX_change = -4
enemyY += enemyY_change
if playerX <= 0:
playerX = 0
if playerX >= 736:
playerX = 736
#display update
pygame.display.update()
It is a matter of Indentation. You have to handle the events in the event loop instead of the application loop:
while running == True:
#stuff happening in game loop
screen.fill((200, 200, 0))
screen.blit(bacground,(0,0))
player(playerX, playerY)
enemy(enemyX, enemyY)
if bullet_state == "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
#detection of the keys pressed and consequences
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# INDENTATAION
#-->|
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
fire_bullet(bulletX,bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
Related
So I've gotten movement to work for enemy and the player, but when I press a button outside of left or right, like space which will be to shoot, the player cant move and the bullet itself wont actually come out or appear at all. Its in pygame and ima be honest I have no clue whats wrong so im sorry it looks so long and weird
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("BackgroundImg.jpg")
pygame.display.set_caption("Real Hero")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
playerX_change = 0
enemyImg = pygame.image.load('Enemy.png')
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 150)
enemyX_change = 0.1
enemyY_change = 40
bulletImg = pygame.image.load('Bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
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 = -0.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.key == pygame.K_SPACE:
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.1
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.1
enemyY += enemyY_change
if bullet_state is "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
It is a matter of Indentation:
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
# INDENTATION
#-->|
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.key == pygame.K_SPACE:
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# INDENTATION
#<--|
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.1
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.1
enemyY += enemyY_change
if bullet_state is "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
So I'm trying to do a game in python, I'm new to pygame, so I was following some tutorials, the player it's showing perfectly, but the enemy isn't showing. I've tried watching the tutorial multiple times, but I still have no clue of what it's happening.
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
#Titulo E Icono
pygame.display.set_caption("Invaders")
icon = pygame.image.load('logo.jpg')
pygame.display.set_icon(icon)
#Player
playerImg = pygame.image.load('spaceship.png')
playerX = 370
playerY = 480
playerX_change = 0
enemyImage = pygame.image.load('enemy.png')
enemyX = 370
enemyY = 48
enemyX_change = 0
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
#Loop
running = True
while running:
screen.fill((59, 59, 59))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Right Or Left Checker
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.5
if event.key == pygame.K_RIGHT:
playerX_change = 0.5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <=0:
playerX=0
elif playerX >=736:
playerX = 736
player(playerX, playerY)
pygame.display.update()
You missed to call the enemy function. In addition, the events must be handled in the event loop:
running = True
while running:
screen.fill((59, 59, 59))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# INDENTATION
#-->|
#Right Or Left Checker
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.5
if event.key == pygame.K_RIGHT:
playerX_change = 0.5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <=0:
playerX=0
elif playerX >=736:
playerX = 736
player(playerX, playerY)
enemy(enemyX, enemyY) # <--- this is missing
pygame.display.update()
I am writing my first pygame script, but the K_SPACE event does not work. When the script is run, nothing happens when the space bar is pressed. I have changed K_SPACE to K_LEFT and K_LSHIFT and they work absolutely fine, so I don't think the error is in the code itself?
The Input is mid-way through the code but I wanted to include it all to ensure there were no issues above which were causing it.
Any ideas?
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('spaceship.png')
pygame.display.set_icon(icon)
background = pygame.image.load('space.jpg')
playerImg = pygame.image.load('battleship.png')
playerX = 370
playerY = 480
playerX_change = 0
enemy1Img = pygame.image.load('alien.png')
enemy1X = random.randint(0,800)
enemy1Y = random.randint(50,150)
enemy1X_change = 0.1
enemy1Y_change = 40
laserImg = pygame.image.load('laser.png')
laserX = 0
laserY = 480
laserX_change = 0
laserY_change = 0.5
laser_state = "ready"
def player(x, y):
screen.blit(playerImg, (playerX,playerY))
def enemy1(x, y):
screen.blit(enemy1Img, (enemy1X, enemy1Y))
def fire_laser(x, y):
global laser_state
laser_state = "fire"
screen.blit(laserImg, (x+30, y+10))
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 = -0.1
elif event.key == pygame.K_RIGHT:
playerX_change = 0.1
elif event.key == pygame.K_SPACE:
if laser_state == 'ready':
laserX = playerX
fire_laser(laserX, laserY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
if playerX >= 736:
playerX = 736
enemy1X += enemy1X_change
if enemy1X <= 0:
enemy1X_change = 0.1
enemy1Y += enemy1Y_change
elif enemy1X >= 736:
enemy1X_change = -0.1
enemy1Y += enemy1Y_change
if laserY <= 0:
laserY = 480
laser_state = 'ready'
if laser_state == "fire":
fire_laser(laserX,laserY)
laserY -= laserY_change
player(playerX, playerY)
enemy1(enemy1X, enemy1Y)
pygame.display.update()
I can reproduce the issue with your orignal code.
It's a matter of Indentation. You have to handle the events in the event loop not after the event loop.
Move the event handling in the event loop:
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
# INDENTATION
#-->|
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
elif event.key == pygame.K_RIGHT:
playerX_change = 0.1
elif event.key == pygame.K_SPACE:
if laser_state == 'ready':
laserX = playerX
fire_laser(laserX, laserY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
I am not getting any frames even though I have set my clock tick to 60 and it is in the right space. I also know its not my computer because I have run larger games with better frames. Also it cant be the indentation because I have it intended correctly.
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
gameclock = pygame.time.Clock()
fps = str(int(gameclock.get_fps()))
playerX = 250
playerY = 250
playerSpeed = 3
playerFrontImg = pygame.image.load("Images/PlayerImages/playerFront.png")
playerRightImg = pygame.image.load("Images/PlayerImages/playerRight.png")
playerBackImg = pygame.image.load("Images/PlayerImages/playerBack.png")
playerLeftImg = pygame.transform.flip(playerRightImg, True, False)
playercurrentImg = playerFrontImg
running = True
while running:
gameclock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
playerY -= playerSpeed
playercurrentImg = playerBackImg
if event.key == pygame.K_DOWN:
playerY += playerSpeed
playercurrentImg = playerFrontImg
if event.key == pygame.K_RIGHT:
playerX += playerSpeed
playercurrentImg = playerRightImg
if event.key == pygame.K_LEFT:
playerX -= playerSpeed
playercurrentImg = playerLeftImg
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
playerY += 0
if event.key == pygame.K_DOWN:
playerY += 0
if event.key == pygame.K_RIGHT:
playerX += 0
if event.key == pygame.K_LEFT:
playerX += 0
playerRect = playercurrentImg.get_rect(topleft = (playerX, playerY))
screen.fill((255, 255, 100))
print(fps)
screen.blit(playercurrentImg, (playerX, playerY))
pygame.display.update()
pygame.quit()
quit()
Any help would be greatly appreciated, I have been struggling with this issue for awhile now.
Is there any other tips on how I should organize my code.
You need to invoke get_fps() in the application loop:
Compute your game's framerate (in frames per second). It is computed by averaging the last ten calls to Clock.tick().
gameclock = pygame.time.Clock()
# [...]
running = True
while running:
gameclock.tick(60)
fps = str(int(gameclock.get_fps()))
# [...]
The keyboard events (see pygame.event module) occur only once when the state of a key changes. The KEYDOWN event occurs once every time a key is pressed. KEYUP occurs once every time a key is released. Use the keyboard events for a single action or a step-by-step movement.
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. Use pygame.key.get_pressed() to evaluate the current state of a button and get continuous movement:
running = True
while running:
gameclock.tick(60)
fps = str(int(gameclock.get_fps()))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
playerY -= playerSpeed
playercurrentImg = playerBackImg
if keys[pygame.K_DOWN]:
playerY += playerSpeed
playercurrentImg = playerFrontImg
if keys[pygame.K_RIGHT]:
playerX += playerSpeed
playercurrentImg = playerRightImg
if keys[pygame.K_LEFT]:
playerX -= playerSpeed
playercurrentImg = playerLeftImg
# [...]
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()