Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Problem with coding for movement keys
I am following a youtube tutorial on how to make a multiplayer game in python: I am using the online python editor, compiler, and interpreter Whenever I run the code, I get this error: Python 3.6.1 (default, Dec 2015, 13:05:11)
[GCC 4.8.2] on linux
File "", line 26
if keys[pygame.K_LEFT]
SyntaxError: invalid syntax

This is my code:
import pygame

width = 500
height = 500
win = pygame.display.set_mode((width, height))

clientNumber = 0

class Player():
    def __init__(self, x, y, width, height, color):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.color = color
        self.rect = (x, y, width, height)
        self.vel = 3

    def draw(self, win):
      pygame.draw.rect(win, self.color, self.rect)

    def move(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT]
            self.x -= self.vel

        if keys[pygame.K_RIGHT]
            self.x += self.vel

        if keys[pygame.K_UP]
            self.y -+ self.vel

        if keys[pygame.K_DOWN]
            self.y += self.vel

        self.rect = (self.x, self.y, self.width, self.height)    

def redrawWindow(win, player):
    win.fill(255, 255, 255)

def main():
    run = True
    p = Player(50, 50, 100, 100, (0, 255, 0))
    while run:
        for event in pygame.event.get():
          if event.type == pygame.QUIT:
            run = False

        redrawWindow(win, p)

Can anybody tell me what's wrong with it?
I'm using Ubuntu, not Windows. I don't know if that helps.

I found a pygame tutorial on It said to make an account, click new repl, and choose pygame from language list: I pasted in the same code I had before: this is what I got:

Python 3.6.7
 bash -c polygott-x11-vnc q && DISPLAY=:0 run-project
nohup: redirecting stderr to stdout
Keyboard Control:
auto repeat: off key click percent: 0 LED mask: 00000000
XKB indicators:
00: Caps Lock: off 01: Num Lock: off 02: Scroll Lock: off
03: Compose: off 04: Kana: off 05: Sleep: off
06: Suspend: off 07: Mute: off 08: Misc: off
09: Mail: off 10: Charging: off 11: Shift Lock: off
12: Group 2: off 13: Mouse Keys: off
auto repeat delay: 660 repeat rate: 25
auto repeating keys: 00ffffffdffffbbf
bell percent: 50 bell pitch: 400 bell duration: 100
Pointer Control:
acceleration: 2/1 threshold: 4
Screen Saver:
prefer blanking: yes allow exposures: yes
timeout: 600 cycle: 600
default colormap: 0x20 BlackPixel: 0x0 WhitePixel: 0xffffff
Font Path:
DPMS (Energy Star):
Display is not capable of DPMS
nohup: ignoring input and appending output to 'nohup.out'
nohup: appending output to 'nohup.out'
File "", line 26
if keys[pygame.K_LEFT]
SyntaxError: invalid syntax
exit status 1
You need colons (:) at the end of your if statements. You need a colon before any indented block of code in Python.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

I added pygame.init() before width = 500 and after import pygame, because said to. but it kept saying this:

Python 3.6.7
 bash -c polygott-x11-vnc q && DISPLAY=:0 run-project
nohup: redirecting stderr to stdout

PLease help - I'm super confused.

It still didn't work, I'll just take more courses until I figure this out.
That's not an error. It's just a message letting you know what it's doing.

It seems like the issue is with trying to start python. Is there a reason you can't install it on your computer?

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Using classes for room movement (text game) Lawr3y 3 819 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Pygame Movement X/Y coord TheHumbleIdiot 2 716 Mar-19-2019, 02:21 PM
Last Post: TheHumbleIdiot
  Movement after KEYUP, only after pause func esteel 2 891 Aug-22-2018, 03:03 PM
Last Post: Windspar
  Coding problem in a Pygame Sghet 4 3,248 Aug-13-2018, 05:39 PM
Last Post: MTGReen
  [PyGame] How to stop the sprite's movement when it touches the edges of the screen? mrmn 5 3,994 May-13-2018, 06:33 PM
Last Post: mrmn

Forum Jump:

Users browsing this thread: 1 Guest(s)