Pygame运动

时间:2014-05-04 20:33:57

标签: python pygame

我一直试图在不控制它的情况下为pygame中的方块创建移动,但我似乎无法做到这一点。我的目标是让红色方块“反弹”墙壁。我已经在比赛中减少了碰撞以防止并发症。

我的代码是:

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 ()

错误是:

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'

1 个答案:

答案 0 :(得分:1)

您的Block子类Spriteper the documentation,没有move_ip方法;这解释了你所看到的错误。

我认为您需要做的是移动属于Rect的{​​{1}} does have that method。尝试:

Block