1

Hey guys I have been trying to move the ball image in a square manner on the screen with pygame. I have tried many times but it does not work. Please see my code below.

import time                                              
import pygame, sys
from pygame.locals import *
pygame.init()
clocks = pygame.time.Clock()  
surfaceObject = pygame.display.set_mode((640, 480))  
pygame.display.set_caption('Bounce')   

mousey,mousex = 0,0  
imgx = 10 
imgy = 10 
pixmove = 60
tiger = [2,2]
movement = 'down' 
background = pygame.image.load('bg.jpg').convert()
ball = pygame.image.load('ball.jpg').convert_alpha()
pygame.mixer.music.load('yeah.mp3')
while True: 
  time.sleep(1)                                        
if movement == 'down':                              
    imgx += pixmove                                  
if imgx < 640:                                      
    tiger[0] - tiger[1]                              

elif movement  == 'right':                          
    imgx += pixmove                                  
if imgx < 0:                                      
    movement = 'up'                                  


elif movement == 'up':                              
    imgy -= pixmove                                  
if imgy < 0:                                        
    movement = 'left'                                


elif movement == 'left':                            
    imgx -= pixmove                                  
if imgx < 0:                                        
    movement = 'down'                                


for event in pygame.event.get():                    
    if event.type == QUIT:                          
        pygame.quit()                                
        sys.exit()                                  

surfaceObject.blit(background,(mousex,mousey))      
surfaceObject.blit(ball,(imgx,imgy))                
pygame.mixer.music.play()
pygame.display.update()                              
clocks.tick(50)

When I run this code the ball moves in a straight way and the ball does not bounce back when it touches the end.

My goal is to rotate the ball in a square manner across the screen. I have tried changing the pixmove variable but it did not solve my problem.

Hope you guys can help me out ..Thanx in advance

sloth
  • 99,095
  • 21
  • 171
  • 219
  • @karl-henrik do you know the answer ..if anybody know just help me .. – user3792941 Jul 01 '14 at 08:26
  • Your indentation is very very broken - according to the way your code is indented above - there is nothing in your while loop. Can you correct your identation please. I don't want to try to guess what your identation should be. – Tony Suffolk 66 Jul 01 '14 at 08:40
  • @TonySuffolk66 i dont have intentation error when i tried executing the code it didnt shows any intentation error – user3792941 Jul 01 '14 at 08:47
  • 1
    I mean the code as pasted in your question - if your code runs ok then that is great, but if you look at the code that is in your question, the indentation is clearly broken - for instance your while loop is empty apart from the sleep(1) call. – Tony Suffolk 66 Jul 01 '14 at 08:53
  • @TonySuffolk66 here the intentation doesnt matters at all ..the thing is that i just need an idea on how to rotate images like in square form over the screen ..i dunno its algorith on how to rotate it ..if you know it just share ..:) – user3792941 Jul 01 '14 at 08:55
  • @ser3792941 - sorry I disagree - unless the indentation is right other people will struggle to run your code, and help you. However if all you want to do is rotate your image - look at pygame.transform http://www.pygame.org/docs/ref/transform.html#pygame.transform – Tony Suffolk 66 Jul 01 '14 at 09:12
  • @TonySuffolk66 no its not about rotate – user3792941 Jul 01 '14 at 09:14
  • In which case I have no idea what you want - sorry. – Tony Suffolk 66 Jul 01 '14 at 09:15
  • @TonySuffolk66 see i want to move the image in a rectangular form not rotating .just move the image to side and if reach the border the down then if reach down then again straight ..just like in a square manner to move the image..i know this is done by adding and substarcting pixels .but i dont have any idea about it .. – user3792941 Jul 01 '14 at 09:29
  • Do you mean you want it to bounce ? – Tony Suffolk 66 Jul 01 '14 at 09:34
  • @TonySuffolk66 exactly .. – user3792941 Jul 01 '14 at 09:34

3 Answers3

0

Your indentation is totally broken, so I can't say what's going.

However, moving the image in a square fashion is easy. Just create a Rect in which the object should move, and once the object leaves the Rect, change the direction.

Take a look at the following example:

import pygame
from itertools import cycle

pygame.init() 

screen = pygame.display.set_mode((300, 300)) 
# move inside the screen (or any other Rect)
s_r = screen.get_rect()
player = pygame.Rect((100, 100, 50, 50))
timer = pygame.time.Clock()

speed = 5
up, down, left, right = (0, -speed), (0, speed), (-speed, 0), (speed, 0)
dirs = cycle([up, right, down, left])
dir = next(dirs)

while True:

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

    # move player
    player.move_ip(dir)
    # if it's outside the screen
    if not s_r.contains(player):
        # put it back inside
        player.clamp_ip(s_r)
        # and switch to next direction
        dir = next(dirs)

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

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

enter image description here

sloth
  • 99,095
  • 21
  • 171
  • 219
  • ..this one helped me ..but can i do the same with this by loading an image ?? – user3792941 Jul 01 '14 at 10:51
  • @user3792941 Sure. Instead of `pygame.draw.rect(screen, pygame.color.Color('Grey'), player)` just do `surfaceObject.blit(ball, player)` – sloth Jul 01 '14 at 10:56
  • @user3792941 using `pygame.transform.rotate`. but please, if you have another question, please post another question. It's tiresome to answer something in the comments. – sloth Jul 01 '14 at 11:06
  • ..all my problem are over except one thing ..i just need to know how can i add two images to the pygame window and make it move in other direction. – user3792941 Jul 01 '14 at 11:39
  • @user3792941 create a second Rect `rect_2 = pygame.Rect(..)` and a second dirs iterator `dir2 = next(dirs)` and use them in the main loop the same way as the others. – sloth Jul 01 '14 at 11:49
  • @sloth..please see the edit which i have made on my code ..it didnt show 2 images.. – user3792941 Jul 01 '14 at 12:16
  • @user3792941 Please post another question instead of editing this one. If you change your code, the answers already given won't make sense. I rolled back your edit. – sloth Jul 01 '14 at 12:24
  • please see here ..i have asked another one http://stackoverflow.com/questions/24510688/bouncing-an-image-in-a-polygonal-way-with-pygame – user3792941 Jul 01 '14 at 12:47
0

Since we determined that what you actually want to do is for the ball to bounce off the edges, look here : Why isn't the ball bouncing back?

In summary, what needs to happen is that you need to keep track of the direction the ball is going (in terms of it's vertical and horizontal movement) and change them as you hit a wall.

Community
  • 1
  • 1
Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Daniel Kelley Jul 01 '14 at 14:50
  • @DanielKelley - had the OP's code been valid, I would have attempted a fuller solution. but as you can see by the comments though he denied any claim that the code is badly formatted as he provided it. – Tony Suffolk 66 Jul 01 '14 at 15:36
0

You seriously want to fix your indenting, as that leads your code to be highly ununderstandable.

If you want to move something, such as a circle, in a square formation, that is pretty easy:

import pygame, sys
from pygame.locals import *
pygame.init()

DISPLAYSURF = pygame.display.set_mode((1200, 600))

x, y = (100, 100)
dir = 'right'

while True:
        for event in pygame.event.get():
                if event.type == QUIT:
                        pygame.quit()
                        sys.exit()
        DISPLAYSURF.fill((0, 0, 0))
        pygame.draw.circle(DISPLAYSURF, (255, 255, 255), (x, y), 10)
        if dir == 'right':
                x+=14
                if x >= 1100:
                        dir = 'down'
        elif dir == 'down':
                y+=14
                if y >= 500:
                        dir = 'left'
        elif dir == 'left':
                x-=14
                if x <= 100:
                        dir = 'up'
        elif dir == 'up':
                y-=14
                if y <= 100:
                        dir = 'right'
        pygame.display.flip()

enter image description here

ZenOfPython
  • 891
  • 6
  • 15
  • do you know the answer of these ...http://stackoverflow.com/questions/24510688/bouncing-an-image-in-a-polygonal-way-with-pygame – user3792941 Jul 01 '14 at 12:48