简体   繁体   中英

Pygame Movement

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'

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM