Entel Entel - 1 year ago 42
Python Question

Why isn't the rectangle moving?

This is my code:

import pygame
from pygame.locals import *

#define the player
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.Surface((50, 25))
self.surf.fill((255,255,255))
self.rect = self.surf.get_rect()

def update(self, pressed_keys):
"""
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
"""
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)

#keep player on the screen
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > 400:
self.rect.right = 400

#initialization
pygame.init()

#create the screen object
screen = pygame.display.set_mode((400, 600))

#instantiate the player
player = Player()

running = True

#main loop
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False

#draw the player to the screen
screen.blit(player.surf, (200, 550))

pressed_keys = pygame.key.get_pressed()

player.update(pressed_keys)

#update the display
pygame.display.flip()


Everything shows up, but I can't move the rectangle with my arrow keys without any error showed, help.. Thanks in advance! I also add a print to check if the pressed_keys work and it print without moving the rectangle.

Answer Source

good to see that you are using pygame:D

In Python, it is important to write "good" code, which means the code should follow all syntactic rules, properly indented, and most importantly, clear and readable.

Here are some guide lines in fixing your code (following PEP 8 guidelines):

  1. Always indent your code by 4 spaces or a tab

  2. A comment ("#") should always be followed by a space, ie: "# <= there's a space there"

  3. There should never be too many empty consecutive new lines

  4. Leave a space after a comma in a list/tuple/set, ie. (10, 10, 10)


And here are some pygame guide lines:

  1. You should not use pygame.key.get_pressed() explicitly to check for keypress at every loop, it will make your code slower, and possibly, lag

  2. Check for keypress in for event in pygame.event.get()

  3. Set a frame rate so it doesn't consume too much ram and to prevent from lagging, do so by using clock.tick, minimum frame rate should be 30

  4. Always fill your screen with a color, even if it's black, screen.fill((0, 0, 0))

  5. refill your screen after every loop. screen.fill((0, 0, 0))


Here is what you been waiting for, how to fix your problem, it's quite simple actually. screen.blit(player.surf, (200, 550)) you're bliting it at your location have set (200, 550). Instead, what you probably want is to change it to screen.blit(player.surf, player.rect), which will blit the player to it's location ("rect").


Here's a complete modified version of your code following all my suggestions:

import pygame
from pygame.locals import *


# define the player
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        self.surf = pygame.Surface((50, 25))
        self.surf.fill((255, 255, 255))
        self.rect = self.surf.get_rect()

    def update(self, pressed_keys):
        if pressed_keys == K_LEFT:
            self.rect.move_ip(-5, 0)
        if pressed_keys == K_RIGHT:
            self.rect.move_ip(5, 0)
        # keep player on the screen
        if self.rect.left < 0:
            self.rect.left = 0
        elif self.rect.right > 400:
            self.rect.right = 400

# initialization
pygame.init()

# create the screen object
screen = pygame.display.set_mode((400, 600))
screen.fill((0, 0, 0))

# instantiate the player
player = Player()

# instantiate the ticker
clock = pygame.time.Clock()

# mainloop
running = True
while running:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            elif event.key == K_LEFT:
                player.update(event.key)
            elif event.key == K_RIGHT:
                player.update(event.key)
        elif event.type == QUIT:
            running = False

    screen.fill((0, 0, 0))
    # draw the player to the screen
    screen.blit(player.surf, player.rect)
    # update the display
    pygame.display.flip()
    clock.tick(30)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download