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#
There are many reasons that could be the culprit. A laptop keyboard may behave weirdly when there are sticky keys or hardware issues. It may also act weird when it is under virus or malware attack. Also, when the keyboard drivers are outdated, you'll experience problems with the keyboard.
As keyboards get used and wear down over time, some of the keys can begin to stick during a keystroke. Sometimes it's a physical sticking, where the keys don't release back to their normal position after you press them, causing the keyboard to repeat those keys.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With