Python Forum
Problem with coding for movement keys
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with coding for movement keys
#1
I am following a youtube tutorial on how to make a multiplayer game in python: https://www.youtube.com/watch?v=_fx7FQ3SP0U. I am using the online python editor, compiler, and interpreter https://repl.it/languages/python3. 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 "main.py", 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))
pygame.display.set_caption("Client")

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)
    player.draw(win)
    pygame.display.update()


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

        p.move()
        redrawWindow(win, p)

main()
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 repl.it. 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
fadfffefffedffff
9fffffffffffffff
fff7ffffffffffff
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
Colors:
default colormap: 0x20 BlackPixel: 0x0 WhitePixel: 0xffffff
Font Path:
/usr/share/fonts/X11/misc,built-ins
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 "main.py", line 26
if keys[pygame.K_LEFT]
^
SyntaxError: invalid syntax
exit status 1
Reply
#2
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 - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I added pygame.init() before width = 500 and after import pygame, because repl.it 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.
Reply
#4
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Laggy Game Movement game_slayer_99 12 4,217 Oct-05-2022, 11:34 AM
Last Post: metulburr
  [PyGame] Particle movement mystery XavierPlatinum 10 2,871 Jul-09-2022, 04:28 AM
Last Post: deanhystad
  [PyGame] Isometric Movement on Tiled Map Josselin 0 2,321 Nov-02-2021, 06:56 AM
Last Post: Josselin
  [PyGame] object's movement to left leave shadow on the screen Zhaleh 3 3,081 Aug-02-2020, 09:59 PM
Last Post: nilamo
  Using classes for room movement (text game) Lawr3y 3 6,483 Aug-20-2019, 12:40 AM
Last Post: Lawr3y
  Pygame Movement X/Y coord TheHumbleIdiot 2 3,475 Mar-19-2019, 02:21 PM
Last Post: TheHumbleIdiot
  Movement after KEYUP, only after pause func esteel 2 3,248 Aug-22-2018, 03:03 PM
Last Post: Windspar
  Coding problem in a Pygame Sghet 4 7,958 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 11,442 May-13-2018, 06:33 PM
Last Post: mrmn

Forum Jump:

User Panel Messages

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