Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Help! Character won't move!
#1
Hi, I am a new programmer. My red cube is not moving

import pygame
import time
import random
import sys

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
    gDisplay.fill(white)
    pygame.display.update()
    clock.tick(60)

    #Shapes and stuff
    playerStartingx = 64
    playerStartingy = sHeight - 100
    player = pygame.draw.rect(gDisplay, red, (playerStartingx,playerStartingy, 60, 60), 0)
    LRC = 0
    pygame.display.update()

    #Game Loop Variable
    playing = True

    while playing:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    LRC = -50

                elif event.key == pygame.K_RIGHT:
                    LRC = 50

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


        playerStartingx += LRC

gameStart()

Quote
#2
X has already been set to 64.you need to update that every time and redraw
Quote
#3
(Jun-19-2018, 08:47 PM)metulburr Wrote: X has already been set to 64.you need to update that every time and redraw

So... what do I do?
Quote
#4
sorry i was on mobile last time.

You need to have this in the while loop to update the location every frame
Quote:
player = pygame.draw.rect(gDisplay, red, (playerStartingx,playerStartingy, 60, 60), 0)
Quote
#5
(Jun-20-2018, 01:45 AM)keyfive Wrote:
(Jun-19-2018, 08:47 PM)metulburr Wrote: X has already been set to 64.you need to update that every time and redraw

So... what do I do?

It is still not working! Sad

import pygame
import time
import random
import sys

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
    gDisplay.fill(white)
    pygame.display.update()
    clock.tick(60)

    # Shapes and stuff
    playerStartingx = 64
    playerStartingy = sHeight - 100

    LRC = 0
    pygame.display.update()

    # Game Loop Variable
    playing = True

    while playing:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    LRC = -50

                elif event.key == pygame.K_RIGHT:
                    LRC = 50

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    LRC = 0
            player = pygame.draw.rect(gDisplay, red, (playerStartingx, playerStartingy, 60, 60), 0)
        playerStartingx += LRC


gameStart()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to move the 'Human' object using keys. ashik 0 190 Apr-29-2018, 08:00 AM
Last Post: ashik
  [PyGame] move randomly sprites reutB 4 1,325 Mar-29-2017, 01:12 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)