0

I have been trying to create movement for squares in pygame without controlling it, but I can't seem to do it. My goal was to make the red squares "bounce" the walls. I have cut out the collision in my game to prevent complications.

My code is:

import pygame
r=(255,0,0)
b=(0,0,255)
sort =(0,0,0)
class Block(pygame.sprite.Sprite):
    def __init__(self, color=b, width= 40, height= 40):
        super(Block, self).__init__()
        self.image = pygame.Surface((width, height))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.origin_x=self.rect.centerx
        self.origin_y=self.rect.centery

    def set_position(self, x, y):
        self.rect.x=x-self.origin_x
        self.rect.y=y-self.origin_y
pygame.init()
pygame.display.set_caption("EsKappa!")

window_size =window_width, window_height = 400, 400
window = pygame.display.set_mode(window_size)

a=(0,201,0)


clock = pygame.time.Clock()
frames_per_second = 60

block_group = pygame.sprite.Group()

a_block= Block(b)
a_block.set_position(window_width/2 -30, window_height/2)


block1=Block()
block2=Block()
block3=Block()
block4=Block()


wall1 = Block(sort,20, 400 )
wall1.set_position(0,200)
wall2= Block(sort,400, 20 )
wall2.set_position(200,0)
wall3=Block(sort,20, 400 )
wall3.set_position(400,200)
wall4=Block(sort,400, 20 )
wall4.set_position(200,400)

block1= Block(r,50, 50 )#here draw
block1.set_position(80, 70)#here place
block2 = Block(r, 50, 40)
block2.set_position(270, 70)
block3 = Block(r,80, 20)
block3.set_position(280, 300)
block4 = Block(r, 30, 70)
block4.set_position(70, 250)

block_group.add(a_block,block1, block2, block3, block4, wall1,wall2,wall3,wall4)




h=pygame.time.get_ticks()/1000
font = pygame.font.SysFont("Times New Roman", 20)
font2 = pygame.font.SysFont("Times New Roman", 50)
text = font.render("Time", True, sort)
speed=[2,0] # here speed
#lukke funktion
running=True
while running:
    event = pygame.event.wait ()
    if event.type == pygame.QUIT or (event.type== pygame.KEYDOWN) and (event.key == pygame.K_ESCAPE): # Giver lukke funktion fra ikon og ESCAPE
        running = False
    if event.type == pygame.MOUSEMOTION :
        mouse_position = pygame.mouse.get_pos()
        a_block.set_position(mouse_position[0],mouse_position[1])
    clock.tick(frames_per_second)
    window.fill(a)

    block1.move_ip(speed) #here error is here
    screen.blit(block1) #here
    pygame.display.update()

    if targetpos[0]+target.get_width()>width or targetpos[0]<0: #here
        speed[0]=-speed[0] #here


    if pygame.sprite.collide_rect(a_block, block1) or  pygame.sprite.collide_rect(a_block, block2) or  pygame.sprite.collide_rect(a_block, block3) or  pygame.sprite.collide_rect(a_block, block4)or pygame.sprite.collide_rect(a_block, wall1) or pygame.sprite.collide_rect(a_block, wall2) or pygame.sprite.collide_rect(a_block, wall3) or pygame.sprite.collide_rect(a_block, wall4):
        time_string = "Du overlevede {} sekunder".format(pygame.time.get_ticks()/1000)
        text = font.render(time_string, True, sort)
        window.blit(text, (window_width/2-100, window_height/2-100))
        if pygame.time.get_ticks()/1000>10:
            time_string1 = "Flot!"
            text = font2.render(time_string1, True, sort)
            window.blit(text, (20, 20))
        else:
            time_string2 = "Ikke så godt :c"
            text = font2.render(time_string2, True, sort)
            window.blit(text, window.blit(text, (20, 20)))
        pygame.display.update()
        pygame.time.wait(5000)
        running = False



    block_group.draw(window)
    pygame.display.update()#opdater skærmen



pygame.quit ()

And the error is:

Traceback (most recent call last):
  File "C:\Python33\eskappa1.py", line 81, in <module>
    block1.move_ip(speed) #here error is here
AttributeError: 'Block' object has no attribute 'move_ip'
6
  • "I can't seem to do it" - so what does your code do?! Commented May 4, 2014 at 20:37
  • I had some failed attempts at doing it, so to be honest I have no idea on how to do it. If that is the answer you are looking for. Currently it is just a game which needs 4 sqaures to move. Commented May 4, 2014 at 20:44
  • Well what exactly were those attempts, and how precisely did they fail? What does your code do, and what else do you want it to do? Commented May 4, 2014 at 20:45
  • I updated the code and pointed out the place where it fails. Python says: AttributeError: 'Block' object has no attribute 'move_ip' Commented May 4, 2014 at 21:01
  • Please put the full traceback into the question, formatted as code. Commented May 4, 2014 at 21:01

1 Answer 1

1

Your Block subclasses Sprite, which per the documentation, doesn't have a move_ip method; this explains the error you are seeing.

I think what you need to do is move the Rect belonging to the Block, which does have that method. Try:

block1.rect.move_ip(speed)
Sign up to request clarification or add additional context in comments.

3 Comments

That was the solution to my error, I understand it now thank you. If you have time, and want to, I have to new errors that I hope you can use your magic on again.
@user3601885 I have rolled back your last edit - if you change the question, the answers are no longer correct. You are welcome to open a new question, but please ensure you provide the minimal example with the maximal supporting information, including full error traceback, what you want the code to do and precisely what it's currently doing.
I understand posted new question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.