1

I was basically trying to make a game from pygame but I ran into an error in which computer sprite was moving but my sprite was not moving The codes are

import pygame
pygame.init()
win = pygame.display.set_mode((1280, 720))
pygame.display.set_caption("Game of Squares")
bluex = 100
bluey = 100
redX = 300
redY = 300
bluevel = 6
redVel = 4
run = True

def drawGame():
    win.fill((0, 225, 0))
    pygame.draw.rect(win, (0, 0, 255), (bluex, bluey, 20, 20))
    pygame.draw.rect(win, (255, 0, 0), (redX, redY, 40, 40))
    pygame.display.update()

while run:
    pygame.time.delay(100)

    if redX < bluex - 10:
        redX = redX + redVel
        drawGame()
    elif redX > bluex + 10:
        drawGame()
        redX = redX - redVel
    elif redY < bluey - 10:
        redY = redY + redVel
    elif redY > bluey + 10:
        redY = redY - redVel
    else:
        run = False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        bluex -= bluevel

    if keys[pygame.K_RIGHT]:
        bluex += bluevel

    if keys[pygame.K_UP]:
        bluey -= bluevel

    if keys[pygame.K_DOWN]:
        bluey += bluevel

    drawGame()

pygame.quit()

P.S :D- I am on macbook and using terminal as a shell And some weird stuff is coming on it while I am pressing the keys to play the game.

(base) AMITs-MacBook-Air:desktop amitkumar$ python game1.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
^[[B^[[B^[[C^[[D^[[A^[[C
genpfault
  • 51,148
  • 11
  • 85
  • 139
MAnas
  • 11
  • 1

1 Answers1

0

The strange codes you are seing are actually what terminal sends for arrow keys

See that answer for details:

Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

Hence pygame never get the expected values defined in pygames constants K_LEFT, K_RIGHT, etc.

For an easy check it's the actual source of the problem, check by using letter keys for moving you sprite around instead of arrow keys. Maybe you coudl try changing your terminal to bash as suggested in link above for mac users ?

kriss
  • 23,497
  • 17
  • 97
  • 116