python - Pygame not checking keyevents after another happens -


i moving sprite on x-axis. moving left , right. when press both left , right @ same time stop moving properly.

i trying make when user presses both keys , lets go of one, continue moving in direction still pressed.

weirdly works while holding right , letting go of left. continues moving right.

when hold left , tap right stops moving until press right again.

i commented out ideas had make work, failed me.

i sure simple fix or logic failure on part.

i have worked couple hours on this.

thanks responses ahead of time.

import pygame import time import random import sys import math  pygame.init()  displaywidth = 1200 displayheight = 800 white = (255,255,255) black = (0,0,0)  gamedisplay = pygame.display.set_mode((displaywidth, displayheight)) pygame.display.set_caption('game 3') clock = pygame.time.clock()   class firstsquare:      def __init__(self,player_x,player_y):          self.x = player_x         self.y = player_y         self.width = 100         self.height = 100      def render(self):          pygame.draw.rect(gamedisplay, white,(self.x, self.y, self.width, self.height))  class secondsquare:      def __init__(self,cpu_x,cpu_y):          self.x = cpu_x         self.y = cpu_y         self.width = 100         self.height = 100      def render(self):          pygame.draw.rect(gamedisplay, white,(self.x, self.y, self.width, self.height))   player = firstsquare(300,300) cpu = secondsquare(100,100)   def gameloop():  ### variables##     player_x = 100     player_y = 100     x = 100     y = 100     movement_x = 0     movement_y = 0     frame_rate = 0     frame_table = 0      ingame = true        while ingame:          event in pygame.event.get():             if event.type == pygame.quit:                 ingame = false                 pygame.quit()                 sys.exit()              keypressed= pygame.key.get_pressed()  #### moving player on x-axis##              if keypressed[pygame.k_left]:                 movement_x = -5             if event.type == pygame.keyup:                 if event.key == pygame.k_left:                     movement_x = 0             if keypressed[pygame.k_right]:                 movement_x = 5             if event.type == pygame.keyup:                 if event.key == pygame.k_right:                     movement_x = 0 ### 2 keys @ once won't move player###              if keypressed[pygame.k_left] , keypressed[pygame.k_right]:                 movement_x = 0  ### pressing 1 key , letting go other continue movement  ##            if keypressed[pygame.k_left] , keypressed[pygame.k_right]: ##                if event.type == pygame.keyup: ##                    if event.key == pygame.k_left: ##                        movement_x = 5 ##                        print("left dropped") ##            if keypressed[pygame.k_right] , keypressed[pygame.k_left]: ##                if event.type == pygame.keyup: ##                    if event.key == pygame.k_right: ##                        movement_x = -5 ##                        print("right dropped")                     gamedisplay.fill(black)         player.render()         cpu.render()         player.x += movement_x         pygame.display.update()         clock.tick(60)   gameloop() pygame.quit() quit() 

i think need this:

if keypressed[pygame.k_left]:     movement_x = -5 if event.type == pygame.keyup:     if event.key == pygame.k_left , movement_x < 0:         movement_x = 0 if keypressed[pygame.k_right]:     movement_x = 5 if event.type == pygame.keyup:     if event.key == pygame.k_right , movement_x > 0:         movement_x = 0 

and it... hope helps.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -