2

I used a working version of a movement system that I had used before. In the working version, pressing wasd will move the player. However, I switched the player class to a sprite when adding collision and now nothing with key input will update on the screen. This being said, anything not using key input works. I copied a movement demo from another question on here and when I ran it nothing happened again? Is this a bug with pygame?

Here's what I copied:

# GitHub:
# https://github.com/Rabbid76/PyGameExamplesAndAnswers/blob/master/documentation/pygame/pygame_keys_and_keyboard_event.md
#
# Stack Overflow:
# https://stackoverflow.com/questions/63540062/pygame-unresponsive-display/63540113#63540113

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    
    rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
    rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
        
    rect.centerx = rect.centerx % window.get_width()
    rect.centery = rect.centery % window.get_height()

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()

pygame.quit()
exit()
Marat
  • 15,215
  • 2
  • 39
  • 48
  • There is no problem with this code. It works well. What is the question? It seems to be a problem with your system, not the code. – Rabbid76 Mar 31 '22 at 15:30
  • 1
    im running it on the chromeOS linux environment which uses Debian, im asking why this worked before but just randomly stopped working, i got it to work using pygame.draw.rect but i wanted to load an image so i could blit text as well, it works now with a different method, but i wanted to use pygame.sprite.Sprite which broke it in the first place after copying the code from another question i had here: https://stackoverflow.com/questions/71654721/pygame-groupcollide-and-spritecollideany-kills-all-sprites/71656703?noredirect=1#comment126666482_71656703 – Karsonthefoxx Mar 31 '22 at 15:48
  • *"... im asking why this worked before but just randomly stopped working ..."* - How are we supposed to know? We don't have access to your system, we only see the well working code from your question. – Rabbid76 Mar 31 '22 at 16:03
  • i have no idea why it stopped all i did was change the bullets and enemies to sprites with `class bulletOBJ(pygame.sprite.Sprite):` – Karsonthefoxx Mar 31 '22 at 16:10
  • Please accept that your problem is not related to your code but to your system. – Rabbid76 Mar 31 '22 at 16:27
  • The code you posted doesn’t use sprites. Post the code you have an issue with in first place :) – xxSirFartAlotxx Apr 02 '22 at 06:33
  • it was this ``` class player(pygame.sprite.Sprite): def __init__(self, pos, w, h): self.x, self.y = pos self.w = w; self.h = h self.img = pygame.Surface((self.w, self.h)) self.img.fill((0, 0, 0)) self.rect = self.img.get_rect() def render(self, screen): screen.blit(self.img, (self.x, self.y)) ``` the movement function was the same – Karsonthefoxx Apr 14 '22 at 01:45

0 Answers0