hey so im pretty new to python and im trying to make this game. its a 2d top down action game. i added an inventory, yet i have to hold e for it to keep open, how do i make it so keydown inputs only once and the inventory keeps open?
i apologize if the code looks messy.
the code i need help for:
win.fill(green)
win.blit(bg, (0, 0))
win.blit(tree, (xtree, ytree))
win.blit(tree1, (xtree1, ytree1))
win.blit(tree2, (xtree2, ytree2))
win.blit(rocka, (xrock, yrock))
win.blit(rockb, (xrock1, yrock1))
win.blit(rockc, (xrock2, yrock2))
win.blit(i, (200, 530))
win.blit(top, (100, 2))
def openinventory():
global w
win.blit(border, (10, 10))
win.blit(border, (770, 10))
win.blit(border1, (10, 270))
win.blit(border1, (770, 280))
win.blit(border2, (10, 590))
win.blit(border2, (10, 10))
win.blit(border2, (10, 250))
win.blit(border2, (15, 270))
win.blit(islot, (20, 20))
win.blit(islot, (20, 100))
win.blit(islot, (20, 175))
win.blit(islot, (90, 20))
win.blit(islot, (90, 100))
win.blit(islot, (90, 175))
win.blit(islot, (160, 20))
win.blit(islot, (160, 100))
win.blit(islot, (160, 175))
win.blit(i1, (240, 20))
win.blit(border, (230, 10))
win.blit(i2, (14, 270))
if woodmat == 1:
win.blit(woodmat3, w)
if w > (10, 10) and w > (200, 0):
w = (20, 455)
if woodmat == 2:
win.blit(woodmat6, w)
if w > (10, 10) and w > (200, 0):
w = (20, 455)
if woodmat == 3 or woodmat > 3:
win.blit(woodmat9, w)
if w > (10, 10) and w > (200, 0):
w = (20, 455)
if rockmat == 1:
win.blit(rockmat2, (100, 455))
if rockmat == 2 or rockmat > 2:
win.blit(rockmat4, (100, 455))
if rockmat == 3 or rockmat > 3:
win.blit(rockmat6, (100, 455))
# here it is:
if keys[pygame.K_e]:
openinventory()
heres the full code:
import pygame
pygame.init()
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 150, 15)
blue = (0, 0, 255)
def game_loop():
global w
w = 20, 455
w_width = 800
w_height = 600
xtree = 390
ytree = 80
xtree1 = 90
ytree1 = 280
xtree2 = 590
ytree2 = 140
xrock = 215
yrock = 185
xrock1 = 690
yrock1 = 340
xrock2 = 400
yrock2 = 300
woodmat = 0
rockmat = 0
x = 400
y = 400
pygame.display.set_caption("Dragon Quest 2")
win = pygame.display.set_mode((w_width, w_height))
icon = pygame.image.load("icon.gif")
pygame.display.set_icon(icon)
clock = pygame.time.Clock()
man = pygame.image.load("man.gif")
manright = pygame.image.load("manleft.gif")
manleft = pygame.image.load("manright.gif")
manup = pygame.image.load("manback.gif")
tree = pygame.image.load("tree2.gif")
tree1 = pygame.image.load("tree1.gif")
tree2 = pygame.image.load("tree2.gif")
rocka = pygame.image.load("rock.gif")
rockb = pygame.image.load("rock.gif")
rockc = pygame.image.load("rock.gif")
border = pygame.image.load("iborder.gif")
border1 = pygame.image.load("iborder1.gif")
border2 = pygame.image.load("iborder2.gif")
bg = pygame.image.load("bg.png")
islot = pygame.image.load("islot.gif")
i1 = pygame.image.load("inventory.gif")
i2 = pygame.image.load("idown.png")
i = pygame.image.load("bar.png")
top = pygame.image.load("top.png")
woodmat3 = pygame.image.load("woodmat3.png")
woodmat6 = pygame.image.load("woodmat6.png")
woodmat9 = pygame.image.load("woodmat9.png")
rockmat2 = pygame.image.load("rockmat2.png")
rockmat4 = pygame.image.load("rockmat4.png")
rockmat6 = pygame.image.load("rockmat6.png")
gameexit = False
while not gameexit:
win.fill(green)
win.blit(bg, (0, 0))
win.blit(tree, (xtree, ytree))
win.blit(tree1, (xtree1, ytree1))
win.blit(tree2, (xtree2, ytree2))
win.blit(rocka, (xrock, yrock))
win.blit(rockb, (xrock1, yrock1))
win.blit(rockc, (xrock2, yrock2))
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameexit = True
win.blit(man, (x, y))
keys = pygame.key.get_pressed()
movespeed = 5
if keys[pygame.K_LEFT] and x > 5:
x -= movespeed
win.blit(manleft, (x + 10, y + 2))
if keys[pygame.K_RIGHT]:
x += movespeed
win.blit(manright, (x, y))
if keys[pygame.K_UP]:
y -= movespeed
win.blit(manup, (x + 5, y + 5))
if keys[pygame.K_DOWN]:
y += movespeed
win.blit(man, (x, y - 5))
if x == 400 and y == 140 and keys[pygame.K_SPACE]:
xtree = 1000
if xtree == 1000:
woodmat += 1
if x == 100 and y == 350 and keys[pygame.K_SPACE]:
xtree1 = 1000
if xtree1 == 1000:
woodmat += 1
if x == 600 and y == 200 and keys[pygame.K_SPACE]:
xtree2 = 1000
if xtree2 == 1000:
woodmat += 1
if x == 225 and y == 200 and keys[pygame.K_SPACE]:
xrock = 1000
if xrock == 1000:
rockmat += 1
if x == 700 and y == 365 and keys[pygame.K_SPACE]:
xrock1 = 1000
if xrock1 == 1000:
rockmat += 1
if x == 410 and y == 325 and keys[pygame.K_SPACE]:
xrock2 = 1000
if xrock2 == 1000:
rockmat += 1
win.blit(i, (200, 530))
win.blit(top, (100, 2))
def openinventory():
global w
win.blit(border, (10, 10))
win.blit(border, (770, 10))
win.blit(border1, (10, 270))
win.blit(border1, (770, 280))
win.blit(border2, (10, 590))
win.blit(border2, (10, 10))
win.blit(border2, (10, 250))
win.blit(border2, (15, 270))
win.blit(islot, (20, 20))
win.blit(islot, (20, 100))
win.blit(islot, (20, 175))
win.blit(islot, (90, 20))
win.blit(islot, (90, 100))
win.blit(islot, (90, 175))
win.blit(islot, (160, 20))
win.blit(islot, (160, 100))
win.blit(islot, (160, 175))
win.blit(i1, (240, 20))
win.blit(border, (230, 10))
win.blit(i2, (14, 270))
if woodmat == 1:
win.blit(woodmat3, w)
if w > (10, 10) and w > (200, 0):
w = (20, 455)
if woodmat == 2:
win.blit(woodmat6, w)
if w > (10, 10) and w > (200, 0):
w = (20, 455)
if woodmat == 3 or woodmat > 3:
win.blit(woodmat9, w)
if w > (10, 10) and w > (200, 0):
w = (20, 455)
if rockmat == 1:
win.blit(rockmat2, (100, 455))
if rockmat == 2 or rockmat > 2:
win.blit(rockmat4, (100, 455))
if rockmat == 3 or rockmat > 3:
win.blit(rockmat6, (100, 455))
if keys[pygame.K_e]:
openinventory()
pygame.display.update()
pygame.time.delay(30)
clock.tick(60)
game_loop()
pygame.quit()
quit()
i also apologize if the answer was very simple yet i was a big idiot im pretty new
Add a global state variable door_open = False
before the main loop, set the state when e is pressed:
door_open = False
gameexit = False
while not gameexit:
# [...]
if keys[pygame.K_e]:
door_open = True
if door_open:
openinventory()
# [...]
The door_open
is ste True
and keeps its state. If you want to "close" the door you've to set door_open = False
, e.g. close the door when c is pressed:
if keys[pygame.K_c]:
door_open = False
Note, you can do this by an event (e.g. pygame.KEYDOWN
) in the event loop, too:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameexit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
door_open = True