0

I have to move the rectangular object straight through the pygame window. I have tried some code with pygame. The code is

import pygame
from itertools import cycle

pygame.init() 
screen = pygame.display.set_mode((300, 300)) 
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))

timer = pygame.time.Clock()

movement = "straight"

x = 0

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise

    if movement == 'straight':
      x += 50

    screen.fill(pygame.color.Color('Black'))
    pygame.draw.rect(screen, pygame.color.Color('Grey'), player)

    pygame.display.flip()
    timer.tick(25)

Here the image didnt moves. What I need is that the image must be moved in a straight way.

furas
  • 134,197
  • 12
  • 106
  • 148
user3830347
  • 1
  • 1
  • 1
  • 4
  • Could you fix your indenting? Firstly, you change `x`, but you never use it. Look at this: http://www.pygame.org/docs/ref/rect.html. Also, each time through the loop, you need to redraw the rectangle. – Matt Shank Jul 11 '14 at 17:30
  • @MattShank, I have rolled back your edit. As python is a language where indentation matters so much any modification of it should be done by the OP. You were correct in requesting clarification from them just not in doing so yourself. Indentation could be part of their issues and "correcting" it for them could invalidate the Question itself. I'm not proposing that is true in this case but when it comes to python and other languages where whitespace means something, don't mess with indents. – indivisible Jul 11 '14 at 17:48
  • @indivisible, Understood. – Matt Shank Jul 11 '14 at 17:55
  • @user3830347 next time use button `{}` on code to keep indentions in question. – furas Jul 11 '14 at 17:59

3 Answers3

1

x is adding, but that does not affect player, which actually affects the drawing of the rectangle.

import pygame
from itertools import cycle

pygame.init() 
screen = pygame.display.set_mode((300, 300)) 
s_r = screen.get_rect()


timer = pygame.time.Clock()

movement = "straight"

x = 0
player = pygame.Rect((x, 100, 50, 50))

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            raise

    if movement == 'straight':
      x += 10
      player = pygame.Rect((x, 100, 50, 50))

    if x >= 300:
      x = 0





    screen.fill(pygame.color.Color('Black'))
    pygame.draw.rect(screen, pygame.color.Color('Grey'), player)

    pygame.display.flip()
    timer.tick(25)
ZenOfPython
  • 891
  • 6
  • 15
  • this is not a valid answer..please post the answer to make the rectangle move with pygame ..am just a beginer..so i dont know much ..if you know how to move it straight ..please help me – user3830347 Jul 11 '14 at 17:31
  • it was helpful ..but when it moves straight it goes out of screen ..how can i start from the middle again ..like doing the same process again and again after striking the screen .. – user3830347 Jul 11 '14 at 17:37
  • You need to compare `x` to the width of your window. If `x` is greater than the width of your window, you can reset `x` back to 0 or 100, or wherever you want it to start. – Matt Shank Jul 11 '14 at 17:39
  • You can use `player.x += 10`. You don't have to create `Rect()` everytime. – furas Jul 11 '14 at 18:02
0

You need to adjust the player rectangle each time you change x. From http://www.pygame.org/docs/ref/rect.html, you can see that the first two arguments are "left" and "top". So, if you want to the rectangle to move from left to right, you'll want something like this:

player = pygame.Rect((100 + x, 100, 50, 50))
pygame.draw.rect(screen, pygame.color.Color('Grey'), player)
Matt Shank
  • 158
  • 1
  • 1
  • 12
0
import pygame

BLACK = pygame.color.Color('Black')
GREY  = pygame.color.Color('Grey')

pygame.init() 

screen = pygame.display.set_mode((300, 300)) 
screen_rect = screen.get_rect()

timer = pygame.time.Clock()

movement = "straight"

player = pygame.Rect(0, 100, 50, 50) # four aguments in place of tuple (,,,)

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if movement == 'straight':
        player.x += 10
        if player.x >= 300: # check only when `player.x` was changed 
            player.x = 0

    screen.fill(BLACK)
    pygame.draw.rect(screen, GREY, player)

    pygame.display.flip()

    timer.tick(25)

BTW:

  • don't use raise to exit program.
  • use readable variable - not s_r but screen_rect
  • you don't need x - you have player.x
  • you can create rectangle only once
  • remove repeated empty lines when you add code to question
furas
  • 134,197
  • 12
  • 106
  • 148