appear random in a game with Pygame - python

I have a problem, in fact, I wish I could do this alone but I think I don't know how to do that, I have a game like you know hit on the mole, my problem is that:
I would like to make it appear randomly but on very precise coordinates
that's my code can you help me?
My code is not finished yet so it's possible that other things appear strangely.
import pygame
pygame.init()
display_width = 600
display_height = 480
gameDisplay = pygame.display.set_mode((display_width, display_height))
fond = pygame.image.load("background.jpg").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
Taupe1, Taupe2, Taupe3, Taupe4, Taupe5, Taupe6 = pygame.image.load("Taupe.png").convert_alpha()
Taupe = [Taupe1, Taupe2, Taupe3, Taupe4, Taupe5, Taupe6]
for i in range(6):
{
}
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
fond = pygame.image.load("background.jpg").convert()
gameDisplay.blit(fond, (0, 0))
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()

if you want them to appear only at the specific coordinates, make a list of all the coordinate. then use random.choice .
for example:
import random
lst_of_coordinates= [[1,2], [2,1], [5,5], [6,6]]
print(random.choice(lst_of_coordinates))

Related

Where do I insert set caption?

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

When the code is runned, it shows a black screen than when i exit the code fires

# no errors were shown in the consle
import pygame
pygame.init()
# display size
width = 1800
height =1000
# colors
black = (0, 0, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
aqua = (1, 255, 255)
# main display
gamedisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Race Game') # the
clock = pygame.time.Clock()
# the car image
carImg = pygame.image.load('myCar.png')
# car
def car(x, y):
gamedisplay.blit(carImg, (x, y))
x = (width * 0.45)
y = (height * 0.8)
# events
crashed1 = False
x_change = 0
while not crashed1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed1 = True
# keybinds
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 pygame.K_RIGHT:
x_change = 0
x += x_change
# update loop
print(event)
gamedisplay.fill(white)
car(x, y)
pygame.display.update()
clock.tick(120)
pygame.quit()
Try to put 4 spaces or one tab before these commands and get
import pygame
pygame.init()
# display size
width = 1800
height =1000
# colors
black = (0, 0, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
aqua = (1, 255, 255)
# main display
gamedisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Race Game') # the
clock = pygame.time.Clock()
# the car image
carImg = pygame.image.load('myCar.png')
# car
def car(x, y):
gamedisplay.blit(carImg, (x, y))
x = (width * 0.45)
y = (height * 0.8)
# events
crashed1 = False
x_change = 0
while not crashed1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed1 = True
# keybinds
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 pygame.K_RIGHT:
x_change = 0
x += x_change
# update loop
print(event)
gamedisplay.fill(white)
car(x, y)
pygame.display.update()
clock.tick(120)
pygame.quit()

Pygame - Pressing backspace to change sprite color

I'm trying to make a game, and I want to make the user be able to change color of their sprite when pressing backspace. As of now, nothing that I draw appears on screen, I have no idea why. Any detailed answer, explaining how and why you change the color of your sprite would be welcome:
This is my code:
import pygame
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
width = 800
height = 600
FPS = 100
font = pygame.font.SysFont(None, 25)
gameDisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption("Second snake game.")
clock = pygame.time.Clock()
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [width/2, height/2])
def gameloop():
exitGame = False
lead_x = width/2
lead_y = height/2
lead_x_change = 0
lead_y_change = 0
block_size = 10
velocity = 0.1
rectangle = pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
while not exitGame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
lead_y_change += -velocity
lead_x_change = 0
if event.key == pygame.K_s:
lead_y_change += velocity
lead_x_change = 0
if event.key == pygame.K_a:
lead_x_change += -velocity
lead_y_change = 0
if event.key == pygame.K_d:
lead_x_change += velocity
lead_y_change = 0
if event.key == pygame.K_BACKSPACE:
rectangle.rect(gameDisplay, black, [lead_x, lead_y, block_size, block_size])
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
lead_x_change = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
lead_y_change = 0
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.display.update()
clock.tick(FPS)
gameloop()
First, put the rendering functions in an appropriate order and put them in your while loop:
gameDisplay.fill(white) # Display fill comes first
pygame.draw.rect(gameDisplay, black, rectangle) # Then draw your objects
pygame.display.flip() # Update the screen
Then, define the pygame.Rect object properly
rectangle = pygame.Rect(lead_x, lead_y, block_size, block_size)
This is what you are probably trying to find (with my few fixes):
import pygame
pygame.init()
# Constants
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
WIDTH = 800
HEIGHT = 600
FPS = 100
FONT = pygame.font.SysFont(None, 25)
BLOCK_SIZE = 10
VELOCITY = 10
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Second snake game.")
clock = pygame.time.Clock()
def message_to_screen(msg, color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [width/2, height/2])
def gameloop():
exitGame = False
x = WIDTH / 2
y = HEIGHT / 2
rectangle = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
rectangle_color = BLACK
while not exitGame:
clock.tick(FPS)
# Pygame events
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
pygame.quit()
quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
y -= VELOCITY
if event.key == pygame.K_s:
y += VELOCITY
if event.key == pygame.K_a:
x -= VELOCITY
if event.key == pygame.K_d:
x += VELOCITY
if event.key == pygame.K_BACKSPACE:
rectangle_color = RED if rectangle_color == BLACK else BLACK
# Update rect coords
rectangle = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
# Render everything
gameDisplay.fill(WHITE)
pygame.draw.rect(gameDisplay, rectangle_color, rectangle)
pygame.display.flip()
gameloop()

How to make an image appear and disappear randomly?

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

pygame hold key to move

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.

Categories