Python Forum
[PyGame] Rectangle keeps teleporting?
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Rectangle keeps teleporting?
#1
Hi, I am trying to make this cube move, but it just teleports when I let go of an arrow key. Please help!

import pygame

pygame.init()

# screen variables
sWidth = 1000
sHeight = 700

# colours
red = (255, 0, 0)
black = (0, 0, 0)
white = (255, 255, 255)

# Main mechanics
gDisplay = pygame.display.set_mode((sWidth, sHeight))
clock = pygame.time.Clock()
pygame.display.set_caption('Zube')




def gameStart():
    # Mechanics

    # Shapes and stuff
    playerStartingx = 64
    playerStartingy = sHeight - 100
    playerWidth = 60
    playerHeight = 60

    # Game Loop Variable
    playing = True

    while playing:
        for event in pygame.event.get():
            gDisplay.fill(white)
            pygame.draw.rect(gDisplay, red, [playerStartingx, playerStartingy, playerWidth, playerHeight], 0)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    playerStartingx += -50

                elif event.key == pygame.K_RIGHT:
                    playerStartingx += 50

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    playerStartingx += 0

        pygame.display.update()
        clock.tick(60)


gameStart()
Reply


Messages In This Thread
Rectangle keeps teleporting? - by keyfive - Jun-25-2018, 08:41 PM
RE: Rectangle keeps teleporting? - by sonnekrieger7 - Jun-27-2018, 11:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 6 2,336 May-27-2023, 09:15 AM
Last Post: Fabrizio_fg
  player just randomly teleporting to the edge of a platform in pygame BliepMonster 5 2,326 Jan-26-2023, 10:12 PM
Last Post: deanhystad
  Rotating a rectangle CompleteNewb 19 13,365 Aug-28-2021, 03:23 PM
Last Post: CompleteNewb
  Make rectangle snap to other rectangle corners Pedroski55 2 4,385 Aug-13-2021, 09:59 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020