Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 皮加梅运动_Python_Pygame - Fatal编程技术网

Python 皮加梅运动

Python 皮加梅运动,python,pygame,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).__

我一直在尝试在
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 ()
导入pygame
r=(255,0,0)
b=(0,0255)
排序=(0,0,0)
类块(pygame.sprite.sprite):
def uuu init uuuuu(自,颜色=b,宽度=40,高度=40):
超级(块,自).\uuu初始化
self.image=pygame.Surface((宽度、高度))
self.image.fill(颜色)
self.rect=self.image.get_rect()
self.origin\u x=self.rect.centerx
self.origin\u y=self.rect.centery
def设置位置(自身、x、y):
self.rect.x=x-self.origin\u x
self.rect.y=y-self.origin\u y
pygame.init()
pygame.display.set_标题(“EsKappa!”)
窗口大小=窗口宽度,窗口高度=400400
window=pygame.display.set_模式(窗口大小)
a=(0201,0)
clock=pygame.time.clock()
每秒帧数=60
block_group=pygame.sprite.group()
a_块=块(b)
a_块。设置_位置(窗宽/2-30,窗高/2)
block1=Block()
block2=Block()
block3=Block()
block4=Block()
墙1=块(排序,20400)
墙1.设置位置(0200)
墙2=块(排序,400,20)
墙2.设置_位置(200,0)
墙3=块(排序,20400)
墙3.设置_位置(400200)
墙4=块(排序,400,20)
墙4.设置位置(200400)
块1=块(r,50,50)#这里画
块1.设置位置(80,70)#此处放置
块2=块(r,50,40)
块2.设置位置(270,70)
块3=块(r,80,20)
块3.设置_位置(280300)
块4=块(r,30,70)
块4.设置位置(70250)
块组。添加(块、块1、块2、块3、块4、墙1、墙2、墙3、墙4)
h=pygame.time.get_ticks()/1000
font=pygame.font.SysFont(“新罗马时代”,20)
font2=pygame.font.SysFont(“新罗马时代”,50)
text=font.render(“时间”、True、排序)
速度=[2,0]#这里是速度
#卢卡芬克蒂翁酒店
运行=真
运行时:
event=pygame.event.wait()
如果event.type==pygame.QUIT或(event.type==pygame.KEYDOWN)和(event.key==pygame.K_ESCAPE):#给定者lukke funktion fra ikon og ESCAPE
运行=错误
如果event.type==pygame.MOUSEMOTION:
mouse\u position=pygame.mouse.get\u pos()
a_块。设置_位置(鼠标位置[0],鼠标位置[1])
时钟滴答声(每秒帧数)
窗口填充(a)
块1.移动ip(速度)#此处有错误
screen.blit(block1)#这里
pygame.display.update()
如果targetpos[0]+target.get_width()>width或targetpos[0]10:
time\u string1=“Flot!”
text=font2.render(time_string1,True,sort)
窗口(文本,(20,20))
其他:
time_string2=“ike sågodt:c”
text=font2.render(time_string2,True,sort)
blit(text,window.blit(text,(20,20)))
pygame.display.update()
pygame.time.wait(5000)
运行=错误
块组绘制(窗口)
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'
回溯(最近一次呼叫最后一次):
文件“C:\Python33\eskappa1.py”,第81行,在
块1.移动ip(速度)#此处有错误
AttributeError:“块”对象没有属性“移动ip”

您的
子类
精灵
,它没有
移动ip
方法;这解释了您看到的错误

我认为您需要做的是移动属于
Rect
。尝试:


“我似乎做不到”-那么你的代码是做什么的?!我尝试做这件事失败了,所以说实话,我不知道怎么做。如果这就是你想要的答案。目前这只是一个需要4平方米移动的游戏。那么这些尝试到底是什么,它们到底失败了多少?你的代码做什么,你还希望它做什么?我更新了代码并指出了失败的地方。Python说:AttributeError:'Block'对象没有属性'move_ip',请将完整的回溯放到问题中,格式为代码。这是我错误的解决方案,我现在理解了,谢谢。如果您有时间,并且愿意,我必须修改新的错误,希望您能再次使用您的魔法。@user3601885我已回滚您上次的编辑-如果您更改问题,答案将不再正确。欢迎您提出一个新问题,但请确保您向提供了最大限度的支持信息,包括完整的错误回溯、您希望代码执行的操作以及它当前正在执行的操作。我理解发布的新问题
block1.rect.move_ip(speed)