So, I've been doing a python program for movement and gravity, and everything works OK, but i doesn't set the caption and i really dont know why. I tried to insert it in the end, but doesn't work neither. Is it that i need to write it on another place, or other type of problem?
import pygame, sys
WIDTH, HEIGHT = 500, 350
pygame.init()
pygame.display.set_caption('gravity movement')
window = pygame.display.set_mode((WIDTH, HEIGHT), 32, 0)
clock = pygame.time.Clock()
player = pygame.Rect(30, 30, 32, 32)
player_speed = 5
#movement
def move(rect, x, y):
rect.x += x
rect.y += y
#Add gravity
def gravity(rect, g_force= 6):
rect.y += g_force
if rect.y + rect.h >= HEIGHT:
rect.y = HEIGHT - rect.h
x, y = 0,0
while True:
clock.tick(60)#means that for every second (at most) 60 frames should pass
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = -player_speed
if event.key == pygame.K_RIGHT:
x = player_speed
if event.key == pygame.K_UP:
y = -20
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x = 0
if event.key == pygame.K_RIGHT:
x = 0
if event.key == pygame.K_UP:
y = 0
#Draw
window.fill((0, 0, 20))
pygame.draw.rect(window, (255, 24, 10), player)
#definitive movement
move(player, x= x, y=y)
gravity(player)
pygame.display.flip()
The problem you're facing is because of setting flags to 32 over here:
window = pygame.display.set_mode((WIDTH, HEIGHT), 32, 0) #Flags = 32 , depth = 0
Just change flags to 0 and it will be alright
So it should be
window = pygame.display.set_mode((WIDTH, HEIGHT))
You can read more about flags and depth here
and so the final code should look something like this :
import pygame, sys
WIDTH, HEIGHT = 500, 350
pygame.init()
pygame.display.set_caption('gravity movement')
window = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
player = pygame.Rect(30, 30, 32, 32)
player_speed = 5
#movement
def move(rect, x, y):
rect.x += x
rect.y += y
#Add gravity
def gravity(rect, g_force= 6):
rect.y += g_force
if rect.y + rect.h >= HEIGHT:
rect.y = HEIGHT - rect.h
x, y = 0,0
while True:
clock.tick(60)#means that for every second (at most) 60 frames should pass
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x = -player_speed
if event.key == pygame.K_RIGHT:
x = player_speed
if event.key == pygame.K_UP:
y = -20
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x = 0
if event.key == pygame.K_RIGHT:
x = 0
if event.key == pygame.K_UP:
y = 0
#Draw
window.fill((0, 0, 20))
pygame.draw.rect(window, (255, 24, 10), player)
#definitive movement
move(player, x= x, y=y)
gravity(player)
pygame.display.flip()
Related
This question already has an answer here:
How to make enemies fall at random on pygame?
(1 answer)
Closed 2 years ago.
my enemy in my game is supposed to move up and down and every time it reaches the bottom of the screen it is supposed to re spawn in a different spot and go down again. Here it is explained in a video.
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 = -25
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
## if playerY > screen_height:
playerX = random.randrange(0,screen_width)
playerY = -10
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()
this is my full code and the code i used to make it move up and down was this.
playerX = random.randrange(0,screen_width)
playerY = -10
Thanks for any help!...........................................................................
What you are doing at the moment seems to be setting the Y once, you need to create a loop to constantly update the Y
The line with the condition if playerY > screen_height: is actually a comment:
## if playerY > screen_height:
so you're setting playerX every frame. It should look more like this:
if playerY > screen_height: # when out of screen
playerX = random.randrange(0,screen_width) # new random X
playerY = 0 # also reset y back to top
playerY += 10 # move
I have a problem, in fact my mole image appears but doesn't disappear. I just want one mole to appear and disappear 10 seconds later and another mole appears etc.. That's my code:
If I do a time.delay(10) my code crashes.
I have tried a million things but I'm stuck right now. Can you help me please? I don't know how to do that.
import pygame
import random
import time
pygame.init()
display_width = 600
display_height = 480
gameDisplay = pygame.display.set_mode((display_width, display_height))
fond = pygame.image.load("fond.bmp").convert()
gameDisplay.blit(fond, (0, 0))
pygame.display.set_caption('Tape Taupe')
clock = pygame.time.Clock()
BAMimg = pygame.image.load('Marteau.png')
gameIcon = pygame.image.load('Taupe.png').convert_alpha()
pygame.display.set_icon(gameIcon)
BAMimg_width = 73
perso1 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso1, (160, 55))
perso2 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso2, (320, 55))
perso3 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso3, (480, 55))
perso4 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 200))
perso5 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 200))
perso6 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 200))
perso7 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 350))
perso8 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 350))
perso9 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 350))
pygame.display.update()
def BAMImg(x, y):
gameDisplay.blit(BAMImg, (x, y))
x = (display_width * 0.45)
y = (display_height * 0.8)
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
y_change = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if 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
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
if 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
BAMimg(x, y)
x += x_change
y += y_change
Taupe = pygame.image.load("Taupe.png").convert_alpha()
coordinates = random.choice([[160, 55], [320, 55], [480, 55], [160, 200], [320, 200], [480, 200], [160, 350], [320, 350],[480, 350]])
gameDisplay.blit(Taupe, coordinates)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
You can use the pygame.time.set_timer function to add an event to the event queue after a specified time interval. The event types are actually just ints and you can define your own events in this way: CHANGE_COORDINATES = pygame.USEREVENT + 1
Then check in the event loop if this event was in the event queue and call random.choice to get the new coordinates.
There were some more problems with the code that I've fixed in the example below:
import pygame
import random
pygame.init()
display_width = 600
display_height = 480
gameDisplay = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
Taupe = pygame.Surface((50, 50))
Taupe.fill((30, 90, 230))
BAMimg = pygame.Surface((73, 73))
BAMimg.fill((230, 90, 30))
# We use this custom event for our timer.
CHANGE_COORDINATES = pygame.USEREVENT + 1
# This function had almost the same name as the BAMimg variable
# and you tried to blit the function itself not the image here.
def display_BAMImg(x, y):
gameDisplay.blit(BAMimg, (x, y))
x = display_width * 0.45
y = display_height * 0.8
def game_loop():
x = display_width * 0.45
y = display_height * 0.8
coordinates_list = [
[160, 55], [320, 55], [480, 55], [160, 200],
[320, 200], [480, 200], [160, 350],
[320, 350], [480, 350],
]
x_change = 0
y_change = 0
coordinates = random.choice(coordinates_list)
# This means after 2000 milliseconds a `CHANGE_COORDINATES`
# event will be added to the event queue.
pygame.time.set_timer(CHANGE_COORDINATES, 2000)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
elif event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
# Check if a `CHANGE_COORDINATES` event was in the
# event queue and if yes, call random.choice to get
# new coordinates.
elif event.type == CHANGE_COORDINATES:
coordinates = random.choice(coordinates_list)
# The following lines should not be in the event loop.
gameDisplay.fill((30, 30, 30)) # Clear the display each frame.
display_BAMImg(x, y)
x += x_change
y += y_change
gameDisplay.blit(Taupe, coordinates)
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
I am coding a game for a school project. I have coded it so that I use wasd to move my character. I have four different images: one facing left, one right, one forwards, and one backwards. I need to change the image being displayed as the character based on which button I press. I have a function defining each image, and I try to call that function based on which button is pressed, and then when the button goes back up, I want to display the front facing image again. All it does is every time I press, the corresponding facing image is displayed, goes away, and then the front facing image is displayed and then goes away very quickly. This is just the part that actually deals with the character:
howieImg = pygame.image.load('howie front.PNG')
howieImg_back = pygame.image.load('howie back.PNG')
howieImg_left = pygame.image.load('howie left.PNG')
howieImg_right = pygame.image.load('howie right.PNG')
def howie(x, y):
gameDisplay.blit(howieImg, (x, y))
def howie_back(x, y):
gameDisplay.blit(howieImg_back, (x, y))
def howie_left(x, y):
gameDisplay.blit(howieImg_left, (x, y))
def howie_right(x, y):
gameDisplay.blit(howieImg_right, (x, y))
while not gameExit:
startx = (display_width/2)
starty = (display_height/2)
thing_width = 25
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w and y > 0:
howie_back(x, y)
y_change = -3
if event.key == pygame.K_s and y < display_height - character_height:
howie(x, y)
y_change = 3
if event.key == pygame.K_a and x > 0:
howie_left(x, y)
x_change = -3
if event.key == pygame.K_d and x < display_width - character_width:
howie_right(x, y)
x_change = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
howie(x, y)
y_change = 0
if event.key == pygame.K_a or event.key == pygame.K_d:
howie(x, y)
x_change = 0
x += x_change
y += y_change
if x > display_width - character_width or x < 0:
x_change = 0
if y > display_height - character_height or y < 0:
y_change = 0
clock.tick(60)
gameDisplay.blit(background1Img, [0, 0])
button1()
howie(x, y)
things_collected(collected)
pygame.display.update()
I'd get rid of the functions and just store the current image in a variable (howieImg in the example below). When the user presses one of the keys, you can simply assign one of the other images to the current image variable.
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
# These are just replacement images that allowed me to test the code.
# howieImg_front = pygame.Surface((50, 50))
# howieImg_front.fill((0, 0, 255))
# howieImg_back = pygame.Surface((50, 50))
# howieImg_back.fill((255, 255, 0))
# howieImg_left = pygame.Surface((50, 50))
# howieImg_left.fill((255, 0, 0))
# howieImg_right = pygame.Surface((50, 50))
# howieImg_right.fill((0, 255, 0))
howieImg_front = pygame.image.load('howie front.PNG').convert_alpha()
howieImg_back = pygame.image.load('howie back.PNG').convert_alpha()
howieImg_left = pygame.image.load('howie left.PNG').convert_alpha()
howieImg_right = pygame.image.load('howie right.PNG').convert_alpha()
# Assign the current howie image to this variable.
howieImg = howieImg_front
x = 200
y = 300
x_change = 0
y_change = 0
clock = pygame.time.Clock()
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
howieImg = howieImg_back
y_change = -3
if event.key == pygame.K_s:
howieImg = howieImg_front
y_change = 3
if event.key == pygame.K_a:
howieImg = howieImg_left
x_change = -3
if event.key == pygame.K_d:
howieImg = howieImg_right
x_change = 3
if event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
howieImg = howieImg_front
y_change = 0
if event.key == pygame.K_a or event.key == pygame.K_d:
howieImg = howieImg_front
x_change = 0
x += x_change
y += y_change
gameDisplay.fill((30, 30, 30))
# Just blit the current `howieImg`.
gameDisplay.blit(howieImg, (x, y))
pygame.display.update()
clock.tick(60)
pygame.quit()
How would I get some mechanics for falling when in an empty space, many answers on the internet said to add gravity but I couldn't understand how they did that they just showed me a bunch of equations.
Also, how would I set an image as my background?
Here's my source code:
import pygame
pygame.init()
display_width = 2560
display_height = 1440
white = (255,255,255)
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('RGB')
clock = pygame.time.Clock()
filler = pygame.image.load('filleraftergimp.png')
def fill(x,y):
gameDisplay.blit(filler,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
y_change = 0
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
x += x_change
y += y_change
gameDisplay.fill(white)
fill(x,y)
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
To implement gravity in your game (as in a 2D platformer), you can just increase the y_change variable each frame, so that you move the object a bit faster downwards each time. Take a look at this example:
import pygame as pg
pg.init()
LIGHTBLUE = pg.Color('lightskyblue2')
DARKBLUE = pg.Color(11, 8, 69)
display = pg.display.set_mode((800, 600))
width, height = display.get_size()
clock = pg.time.Clock()
player_image = pg.Surface((30, 60))
player_image.fill(DARKBLUE)
x = width * 0.45
y = 0
x_change = 0
y_change = 0
on_ground = False
# A constant value that you add to the y_change each frame.
GRAVITY = .3
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
elif event.type == pg.KEYDOWN:
if event.key == pg.K_a:
x_change = -5
elif event.key == pg.K_d:
x_change = 5
elif event.key == pg.K_s:
y_change = 5
elif event.key == pg.K_w:
if on_ground: # Only jump if the player is on_ground.
y_change = -12
on_ground = False
elif event.type == pg.KEYUP:
if event.key == pg.K_a and x_change < 0:
x_change = 0
elif event.key == pg.K_d and x_change > 0:
x_change = 0
# Add the GRAVITY value to y_change, so that
# the object moves faster each frame.
y_change += GRAVITY
x += x_change
y += y_change
# Stop the object when it's near the bottom of the screen.
if y >= height - 130:
y = height - 130
y_change = 0
on_ground = True
# Draw everything.
display.fill(LIGHTBLUE)
pg.draw.line(display, (0, 0, 0), (0, height-70), (width, height-70))
display.blit(player_image, (x, y))
pg.display.update()
clock.tick(60)
pg.quit()
I'm trying to move the player left in the four main directions, but it only moves when pressed not held down, so I have to repeatedly press the button to move across the screen and up and down don't work.
import pygame
#Start pygame
pygame.init()
#Window/Screen/Display
display_x = 1280
display_y = 720
display = pygame.display.set_mode((display_x,display_y))
pygame.display.set_caption('Platforms')
clock = pygame.time.Clock()
#Colors
black = (0,0,0)
green = (1,166,17)
#Images
character = pygame.image.load('character.gif')
def chrctr(x,y):
display.blit(character,(x,y))
x_c = (display_x / 2)
y_c = (display_y / 2)
x_change = 0
y_change = 0
floor_1 = pygame.image.load('wood.jpg')
def floor(x,y):
display.blit(floor_1,(x,y))
x = (display_x * 0)
y = (display_y * 0.9)
not_dead=True
while not_dead:
for event in pygame.event.get():
if (event.type==pygame.QUIT):
not_dead=False
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.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_LEFT or event.key == pygame.K_RIGHT:
x_change = 5
x_c += x_change
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
y_c += y_change
display.fill(green)
pygame.draw.rect(display, black, [0, 550, 200, 50])
pygame.draw.rect(display, black, [0, 450, 200, 50])
pygame.draw.rect(display, black, [0, 350, 200, 50])
pygame.draw.rect(display, black, [0, 250, 200, 50])
pygame.draw.rect(display, black, [0, 150, 200, 50])
pygame.draw.rect(display, black, [0, 50, 200, 50])
pygame.draw.rect(display, black, [1080, 550, 200, 50])
pygame.draw.rect(display, black, [1080, 450, 200, 50])
pygame.draw.rect(display, black, [1080, 350, 200, 50])
pygame.draw.rect(display, black, [1080, 250, 200, 50])
pygame.draw.rect(display, black, [1080, 150, 200, 50])
pygame.draw.rect(display, black, [1080, 50, 200, 50])
floor(0,display_y * 0.9)
floor(236, display_y * 0.9)
floor(472, display_y * 0.9)
floor(708, display_y * 0.9)
floor(944, display_y * 0.9)
floor(1180, display_y * 0.9)
chrctr(x_c, y_c)
pygame.display.update()
clock.tick(60)
print ("Hello")
pygame.quit()
Ok so I made a simple demo where your character (hero) is centered in the screen and you can move him around with the arrow keys. Since there was a lot of refactoring, let me know what stuff I need to clear up in the comments and I will add explanations to this answer.
import pygame
import sys
class Character(object):
def __init__(self, x=0, y=0, speed=0):
self.x = x
self.y = y
self.speed = speed
self.image = pygame.image.load('hero.png')
def get_size(self):
return self.image.get_size()
def draw(self):
display.blit(self.image, (self.x, self.y))
pygame.init()
(width, height) = (800, 600)
display = pygame.display.set_mode((width, height))
pygame.display.set_caption('Platforms')
clock = pygame.time.Clock()
hero = Character(speed=5)
hero_width, hero_height = hero.get_size()
hero.x = width/2.0 - hero_width/2.0
hero.y = height/2.0 - hero_height/2.0
black = (0,0,0)
pressed_keys = {"left": False, "right": False, "up": False, "down": False}
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = True
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = True
if event.key == pygame.K_UP:
pressed_keys["up"] = True
if event.key == pygame.K_DOWN:
pressed_keys["down"] = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
pressed_keys["left"] = False
if event.key == pygame.K_RIGHT:
pressed_keys["right"] = False
if event.key == pygame.K_UP:
pressed_keys["up"] = False
if event.key == pygame.K_DOWN:
pressed_keys["down"] = False
if pressed_keys["left"]:# == True is implied here
hero.x -= hero.speed
if pressed_keys["right"]:
hero.x += hero.speed
if pressed_keys["up"]:
hero.y -= hero.speed
if pressed_keys["down"]:
hero.y += hero.speed
display.fill(black)
hero.draw()
pygame.display.update()
clock.tick(60)
EDIT:
I see that the pressed_keys dictionary caused some confusion, so I will explain that. You might want to read something like this if my explanation is unclear.
Dictionaries in python are basically like physical dictionaries in real life. Say you are looking up a word like "aardvark" (a key) and want to know what it means. You would open the dictionary book and find the definition (a value) of "aardvark." Dictionaries are simply a bunch of these key:value pairs.
When I initially created the pressed_keys variable, I defined four key:value pairs, one for each of the directions. The keys were left, right, up, and down while the corresponding values were all False. This should make sense since at the beginning of the game, we are not pressing any buttons and the player is not moving. Lines of code like pressed_keys["left"] = True update the value that matches with the key left in pressed_keys whenever a keypress occurs according to pygame.
After I get all of the events that happened in pygame (window closing, mouse movement, keyboard presses, etc.) at that particular instant of time, I now can check the status of my pressed_keys dictionary. If the value that corresponds to the dictionary key left is True, then I simply shift the hero's x-position left a little. The same is true for all of the other directions.