How to make enemies fall at random on pygame? - python

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

Related

Enemy not showing in screen PyGame

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

K_SPACE none responsive

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

Why is my key movement not working properly?

I'm creating a basic game, but I have a problem with keyboard responding in my code. When you click on start, the cube is going all the way the direction of your pressed arrow key, but I want it to move any direction you press with arrow keys and it stops if no keys are pressed. Could someone show me what error do I have in my code and how to do it right?
Here is the code:
import pygame
import time
import sys
import pygame.event as EVENTS
from pygame.locals import *
import random
print("Welcome to the prototype of rpg game -Made by Adam Pospíchal.")
pygame.init()
sirka = 800
vyska = 800
okno = pygame.display.set_mode((sirka,vyska))
pygame.display.set_caption("RPG GAME")
#PARAMETRE OKNA#
#PARAMETRE OKNA#
#PARAMETRE KOCKY HRACA#
#PARAMETRE MENU#
#farby - RGB#
RM = 255
GM = 255
BM = 0
player_r = 0
player_g = 255
player_b = 0
player_height = 20
player_width = 20
smerx = 0
smery = 0
x_pos = 390
y_pos = 390
#farby - RGB#
x = 400
y = 400
#PARAMETRE MENU#
#TEXT#
font = pygame.font.SysFont('Arial', 40)
textsurface_title = font.render('CUBE ADVENTURE', False, (255, 0, 0))
textsurface_Controls = font.render('CONTROLS', False, (255, 0, 0))
textsurface = font.render('START', False, (255, 0, 0))
textsurface2 = font.render('CONTROLS', False, (255, 0, 0))
textsurface3 = font.render('EXIT', False, (255, 0, 0))
#TEXT#
#MAIN MENU#
obdlznik_start = pygame.Rect(300,200,200,100)
obdlznik_controls = pygame.Rect(300,350,200,100)
obdlznik_exit = pygame.Rect(300,500,200,100)
#MAIN MENU
def game():
x_pos = 360
y_pos = 360
smerx = 0
smery = 0
player_r = 0
player_g = 255
player_b = 0
player_height = 20
player_width = 20
sirka = 800
vyska = 800
while True:
for event in pygame.event.get():
if event.type == QUIT:
koniec()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
smerx = 1
if event.key == pygame.K_LEFT:
smerx = -1
if event.key == pygame.K_DOWN:
smery = 1
if event.key == pygame.K_UP:
smery = -1
if event.key == pygame.K_ESCAPE:
koniec()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
smerx = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
smery = 0
#BORDER
if x_pos + player_width > 800:
x_pos = 800
if x_pos < 0:
x_pos = 0
if y_pos + player_height > 800:
y_pos = 800
if y_pos < 0:
y_pos = 0
#BORDER
#PLAYER CUBE
okno.fill((0,0,0))
x_pos = x_pos + smerx
y_pos = y_pos + smery
obdlznik_player = pygame.Rect(x_pos,y_pos,player_height,player_width)
pygame.draw.rect(okno,(player_r,player_g,player_b),obdlznik_player)
pygame.display.update()
#PLAYER CUBE
#HEALTHBAR
#HEALTHBAR
def koniec(): #EXIT GAME
pygame.quit()
sys.exit()
def menu_controls(): #CONTROLS MENU
while True:
for event in pygame.event.get():
if event.type == QUIT:
koniec()
#FARBY KOCKY KURZORA
RR = 255
GR = 0
BR = 0
#FARBY KOCKY KURZORA
mouse_x,mouse_y = pygame.mouse.get_pos() #POZÍCIA MYŠI
mouse_rect = pygame.Rect(mouse_x-10,mouse_y-10,20,20) #KOCKA MYŠI
obdlznik_spat = pygame.Rect(10,10,40,40)
obdlznik_spat_2 = pygame.Rect(30,25,10,10)
spat_collision = mouse_rect.colliderect(obdlznik_spat)
if spat_collision:
RR = 0
GR = 0
BR = 255
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu_main()
pygame.mouse.set_visible(0) #VIDITEĽNOSŤ MYŠI
okno.fill((0,0,0))
okno.blit(textsurface_Controls,(300,50))#NADPIS
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_spat)
pygame.draw.polygon(okno, (255,0,0), ((15,30), (30,15), (30,45)))
pygame.draw.rect(okno,(255,0,0),obdlznik_spat_2)
pygame.draw.rect(okno,(RR,GR,BR),mouse_rect) #KRESLENIE KOCKY KURZORA MYSI
pygame.display.update() #UPDATE OBRAZOVKY
def menu_main():
while True:
for event in pygame.event.get():
if event.type == QUIT:
koniec()
#FARBY KOCKY KURZORA
RR = 255
GR = 0
BR = 0
#FARBY KOCKY KURZORA
mouse_x,mouse_y = pygame.mouse.get_pos() #POZÍCIA MYŠI
pygame.mouse.set_visible(0) #VIDITEĽNOSŤ MYŠI
mouse_rect = pygame.Rect(mouse_x-10,mouse_y-10,20,20) #KOCKA MYŠI
s_collision = mouse_rect.colliderect(obdlznik_start)
c_collision = mouse_rect.colliderect(obdlznik_controls)
e_collision = mouse_rect.colliderect(obdlznik_exit)
if s_collision:
RR = 0
GR = 255
BR = 0
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
game()
if c_collision:
RR = 0
GR = 0
BR = 255
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
menu_controls()
if e_collision:
RR = 255
GR = 255
BR = 255
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
koniec()
okno.fill((0,0,0))
okno.blit(textsurface_title,(250,50))#NADPIS
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_start)
okno.blit(textsurface,(350,230))
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_controls)
okno.blit(textsurface2,(315,370))
pygame.draw.rect(okno,(RM,GM,BM),obdlznik_exit)
okno.blit(textsurface3,(360,520))
pygame.draw.rect(okno,(RR,GR,BR),mouse_rect) #KRESLENIE KOCKY KURZORA MYSI
pygame.display.update() #UPDATE OBRAZOVKY
menu_main()
#MAIN MENU#
#HERNY CYKLUS - PRIKAZY V HRE#
The problem is that you are using two event loops inside game(). You must add all the events in the same event loop.
So,
for event in pygame.event.get():
if event.type == QUIT:
koniec()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
smerx = 1
if event.key == pygame.K_LEFT:
smerx = -1
if event.key == pygame.K_DOWN:
smery = 1
if event.key == pygame.K_UP:
smery = -1
if event.key == pygame.K_ESCAPE:
koniec()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
smerx = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
smery = 0
this ^ will become just
for event in pygame.event.get():
if event.type == QUIT:
koniec()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
smerx = 1
if event.key == pygame.K_LEFT:
smerx = -1
if event.key == pygame.K_DOWN:
smery = 1
if event.key == pygame.K_UP:
smery = -1
if event.key == pygame.K_ESCAPE:
koniec()
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
smerx = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
smery = 0
So basically, I did a mistake in my code by using (2x for) in the game(). Next time when I will use (for), I will check if the commands below it are in the same cycle. Thanks for answer :)

Block only moves once on KEYDOWN (Python3.6 - Pygame)

My name is Jeremy and I am learning Python. I am a beginner and I just started a couple of days ago.
I am making a simple game in Python, and I would like my block/player to move continuously while the respective arrow key is held down. As of now, it only moves once when pressing down the arrow keys. Any help is greatly appreciated. Thank you!
Here is the code I've written:
import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Game')
clock = pygame.time.Clock()
blockImg = pygame.image.load('blockpic.png')
def block(x,y):
gameDisplay.blit(blockImg, (x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
y_change = 0
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key == pygame.K_RIGHT:
x_change = 10
if event.key == pygame.K_UP:
y_change = -10
if event.key == pygame.K_DOWN:
y_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
y_change = 0
x += x_change
y += y_change
gameDisplay.fill(white)
block(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Your code is not indented correctly. The line x += x_change and the five lines beneath are inside of the event loop, so they get executed once per event in the event queue. Just dedent these lines to fix the program.
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
if event.key == pygame.K_RIGHT:
x_change = 10
if event.key == pygame.K_UP:
y_change = -10
if event.key == pygame.K_DOWN:
y_change = 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
y_change = 0
# The following lines should be executed once per frame not
# once per event in the event queue.
x += x_change
y += y_change
gameDisplay.fill(white)
block(x,y)
pygame.display.update()
clock.tick(60)

How to fix infinite jump bug in pygame?

I am trying to make a game, but you can keep spamming "w" (jump) for infinite height which is really bad when you are trying to make a platformer game. This is all my code:
GRAVITY = .2
diedorgameover = False
while not diedorgameover:
for event in pygame.event.get():
if event.type == pygame.QUIT:
diedorgameover = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x_change = -5
elif event.key == pygame.K_d:
x_change = 5
elif event.key == pygame.K_s:
y_change = 5
elif event.key == pygame.K_w:
y_change = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
x_change = 0
if event.key == pygame.K_s or event.key == pygame.K_w:
y_change = 0
#adding gravity to y_value
y_change += GRAVITY
x += x_change
y += y_change
if y >= gameDisplay.get_height() - 68:
y = gameDisplay.get_height() - 68
y_change = 0
#draw everything
gameDisplay.blit(background_image,(0,0))
red(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
I would really appreciate it if you have any insight on this problem.
You can define a on_ground variable that you set to True if the player touches the ground. When the user wants to jump (presses 'w') you first check if on_ground: and then change the y_change and set on_ground to False.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
GRAVITY = .5
player_img = pygame.Surface((30, 50))
player_img.fill((40, 120, 200))
x = 100
y = 500
x_change = 0
y_change = 0
on_ground = False
diedorgameover = False
while not diedorgameover:
for event in pygame.event.get():
if event.type == pygame.QUIT:
diedorgameover = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x_change = -5
elif event.key == pygame.K_d:
x_change = 5
elif event.key == pygame.K_s:
y_change = 5
elif event.key == pygame.K_w:
# Only jump if the player is on the ground.
if on_ground:
y_change = -12
on_ground = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
x_change = 0
y_change += GRAVITY
x += x_change
y += y_change
# If the player is on the ground.
if y >= gameDisplay.get_height() - 68:
y = gameDisplay.get_height() - 68
y_change = 0
on_ground = True
gameDisplay.fill((30, 30, 30))
gameDisplay.blit(player_img, (x, y))
pygame.display.update()
clock.tick(60)
pygame.quit()
Also delete these lines, otherwise the player can stop while in the air if the key is released:
if event.key == pygame.K_s or event.key == pygame.K_w:
y_change = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_SPACE and y_change == 0: # if spacebar is pressed and y_change is 0
y_change = 18 # set y_change to 18
if y_change > 0 and player_y == 200: # if y_change is greater than 0 and player_y is 200
player_y -= y_change
y_change -= gravity
if player_y > 200:
player_y = 200
if player_y == 200 and y_change < 0:
y_change = 0

Categories

Resources