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()
Related
This question already has an answer here:
How to get if a key is pressed pygame [duplicate]
(1 answer)
Closed 1 year ago.
alright so I have been trying to start learning game development in the past couple days and I finally started working on a project. The problem is that for some reason my controls aren't working even when I made everything like it is supposed to be. Please help. (also this is the entire code because I dont know where I am wrong)
#instalize the game VERY IMPORTANT
pygame.init()
# this is the window
screen = pygame.display.set_mode((800, 600))
#title and icon
pygame.display.set_caption("Space invaders")
icon = pygame.image.load('pictures/ufo.png')
pygame.display.set_icon(icon)
#player
playerImg = pygame.image.load('pictures/player.png')
playerX = 370
playerY = 480
PlayerX_change = 0
def player(x,y):
screen.blit(playerImg, (x,y))
#game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running= False
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
#RGB
screen.fill((0, 0, 0))
playerX += PlayerX_change
player(playerX, playerY)
pygame.display.update()
It is a matter of Indentation. You cannot nest events:
unning = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running= False
# INDENTATION
#<--|
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
# [...]
Your identation is not correct. I have corrected it and attached the code.
#instalize the game VERY IMPORTANT
pygame.init()
# this is the window
screen = pygame.display.set_mode((800, 600))
#title and icon
pygame.display.set_caption("Space invaders")
icon = pygame.image.load('pictures/ufo.png')
pygame.display.set_icon(icon)
#player
playerImg = pygame.image.load('pictures/player.png')
playerX = 370
playerY = 480
PlayerX_change = 0
def player(x,y):
screen.blit(playerImg, (x,y))
#game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running= False
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
#RGB
screen.fill((0, 0, 0))
playerX += PlayerX_change
player(playerX, playerY)
pygame.display.update()
Your indentation is incorrect
Try usinng this;
if event.type == pygame.QUIT:
running= False
# if keystroke is pressed move right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.1
if event.key == pygame.K_RIGHT:
playerX_change = 0.1
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0.1
Also if you use event.KEYDOWN it not checks multiple keys
although you can use pygame.key.get_pressed()
Use:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
code:
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 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
I want enemies to randomly fall from the sky on my game and I do not know how to do this.
I was using this youtube video: Falling Objects in Pygame.
import pygame, random
pygame.init()
screen_width = 800
screen_height = 600
window = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Test')
time = pygame.time.Clock()
bg_color1 = (135, 142, 142) # MAIN BG COLOR
bg_color2 = (255, 0, 0) # red
bg_color3 = (255, 255, 0) # yellow
UFO = pygame.image.load('ufo.png')
bg_pic = pygame.image.load('Letsgo.jpg')
clock = pygame.time.Clock()
playerImg = pygame.image.load('enemy.png')
playerX = random.randrange(0, screen_width)
playerY = -50
playerX_change = 0
player_speed = 5
def player(x, y):
window.blit(playerImg, (playerX, playerY))
crashed = False
rect = UFO.get_rect()
obstacle = pygame.Rect(400, 200, 80, 80)
menu = True
playerY = playerY + player_speed
if playerY > screen_height:
playerX = random.randrange(0,screen_width)
playerY = -10
def ufo(x, y):
window.blit(UFO, (x, y))
while menu:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu = False
window.fill((0, 0, 0))
time.tick(30)
window.blit(bg_pic, (0, 0))
pygame.display.update()
x = (screen_width * 0.45)
y = (screen_height * 0.8)
x_change = 0
car_speed = 0
y_change = 0
while not crashed:
x += x_change
if x < 0:
x = 0
elif x > screen_width - UFO.get_width():
x = screen_width - UFO.get_width()
y += y_change
if y < 0:
y = 0
elif y > screen_height - UFO.get_height():
y = screen_height - UFO.get_height()
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
############SIDE TO SIDE################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
###########UP AND DOWN#################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
##
x += x_change
y += y_change
##
window.fill(bg_color1)
ufo(x, y)
player(playerX, playerY)
pygame.display.update()
clock.tick(100)
pygame.quit()
quit()
So the code that was supposed to make the image fall was this
if playerY > screen_height:
playerX = random.randrange(0,screen_width)
playerY = -10
Move the following code:
if playerY > screen_height:
playerX = random.randrange(0,screen_width)
playerY = -10
To here so it can be within the game loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
############SIDE TO SIDE################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
###########UP AND DOWN#################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
if playerY > screen_height:
playerX = random.randrange(0,screen_width)
playerY = -10
Below I have defined a pygame game loop. I am able to move the sprite in 8 directions.
#player position
def player(x,y):
screen.blit(playerImg, (x, y))
#enemy position
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))
# Game Loop
running = True
while running:
#fill values RGB - Red, Green, Blue
screen.fill(bc.goodblue)
#background immage
screen.blit(background, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#if keystroke is pressed
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
playerx_change = -movespeed
if event.key == pygame.K_RIGHT or event.key == ord('d'):
playerx_change = movespeed
if event.key == pygame.K_UP or event.key == ord('w'):
playery_change = -movespeed
if event.key == pygame.K_DOWN or event.key == ord('s'):
playery_change = movespeed
if event.key == pygame.K_SPACE and bullet_state == "ready":
bulletx = playerx
fire_bullet(bulletx, bullety)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == ord('a') or event.key == ord('d'):
playerx_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == ord('s') or event.key == ord('w'):
playery_change = 0
#player movement
playery += playery_change
playerx += playerx_change
if playerx <= 0:
playerx = 0
if playerx >= width - PImgWidth:
playerx = width - PImgWidth
if playery <=0:
playery = 0
if playery >=height - PImgHeight:
playery = height - PImgHeight
#enemy movement
enemyx += enemyx_change
if enemyx <= 0:
enemyx_change = enemyspeed
enemyy += enemyy_change
if enemyx >= width - EImgWidth:
enemyx_change = -enemyspeed
enemyy += enemyy_change
player(playerx, playery)
enemy(enemyx, enemyy)
pygame.display.update()
pygame.quit()
However, there is one small issue.
If I press the left arrow key the sprite moves to the left. If I then also press the right arrow key the sprite starts moving to the right.
If I then let go of the left arrow key the sprite stops. I don't want this to happen. If I'm moving in the right direction and I let go of the left arrow key I want to keep moving in the right direction.
I don't know an efficient way to set this up.
If you release the LEFT, RIGHT, a or d you have to check whether one of these keys is still pressed before you can set playerx_change = 0:. Use pygame.key.get_pressed() to evaluate the state of the keys:
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == ord('a') or event.key == ord('d'):
keys = pygame.key.get_pressed()
if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT] and not keys[pygame.K_a] and not keys[pygame.K_d]:
playerx_change = 0
Do the same for UP, DOWN, s respectively w
while running:
# [...]
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
playerx_change = -movespeed
if event.key == pygame.K_RIGHT or event.key == ord('d'):
playerx_change = movespeed
if event.key == pygame.K_UP or event.key == ord('w'):
playery_change = -movespeed
if event.key == pygame.K_DOWN or event.key == ord('s'):
playery_change = movespeed
if event.key == pygame.K_SPACE and bullet_state == "ready":
bulletx = playerx
fire_bullet(bulletx, bullety)
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:
keys = pygame.key.get_pressed()
if not keys[pygame.K_LEFT] and not keys[pygame.K_RIGHT] and not keys[pygame.K_a] and not keys[pygame.K_d]:
playerx_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN or event.key == pygame.K_s or event.key == pygame.K_w:
keys = pygame.key.get_pressed()
if not keys[pygame.K_UP] and not keys[pygame.K_DOWN] and not keys[pygame.K_s] and not keys[pygame.K_w]:
playery_change = 0