Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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,我正在做一个平台游戏,试图让我的敌人来回移动一段距离。基本上,我需要找到一种方法,将dis增加到一个特定的数字,将其减少到零,然后再次增加到相同的数字。它需要无限期地继续这样做。现在它增加到10,但随后保持不变。任何帮助都将不胜感激。(注意:此代码只是删除了所有“self”的测试版本。) speed=1 dis=0 尽管如此: 如果速度>0: dis+=1 如果dis>10: 速度=-1 dis-=1 如果dis=10: #设定速度 如果dis你为什么不试着打破它?根据当前距离,设置速度。然后根

我正在做一个平台游戏,试图让我的敌人来回移动一段距离。基本上,我需要找到一种方法,将dis增加到一个特定的数字,将其减少到零,然后再次增加到相同的数字。它需要无限期地继续这样做。现在它增加到10,但随后保持不变。任何帮助都将不胜感激。(注意:此代码只是删除了所有“self”的测试版本。)

speed=1
dis=0
尽管如此:
如果速度>0:
dis+=1
如果dis>10:
速度=-1
dis-=1
如果dis<0:
速度=1
打印(dis)

你为什么不试着把它拆散呢?根据当前距离,设置速度。然后根据当前速度,增加或减少距离

speed  = 1
dis = 0

while True:
    if dis >= 10:
        # Set speed
    if dis <= 0:
        # Set speed

    # Based on speed, increment or decrement distance
speed=1
dis=0
尽管如此:
如果dis>=10:
#设定速度

如果dis你为什么不试着打破它?根据当前距离,设置速度。然后根据当前速度,增加或减少距离

speed  = 1
dis = 0

while True:
    if dis >= 10:
        # Set speed
    if dis <= 0:
        # Set speed

    # Based on speed, increment or decrement distance
speed=1
dis=0
尽管如此:
如果dis>=10:
#设定速度
如果我发现了:)

speed=1
dis=0
尽管如此:
如果速度>=0:
dis+=1
如果dis>=10:
速度=-1
如果速度我想出来了:)

speed=1
dis=0
尽管如此:
如果速度>=0:
dis+=1
如果dis>=10:
速度=-1

如果速度,您可以检查对象是否在该区域之外,然后反转速度。s有许多属性,如
left
right
,在这里很有用

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray13')
BLUE = pg.Color('dodgerblue1')

rect = pg.Rect(300, 200, 20, 20)
distance = 150
speed = 1

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    # Check if the rect is outside of the specified
    # area and if it's moving to the left or right.
    if (rect.right > 300+distance and speed > 0
            or rect.left < 300 and speed < 0):
        speed *= -1  # Invert the speed.
    # Add the speed to the rect's x attribute to move it.
    rect.x += speed

    # Draw everything.
    screen.fill(BG_COLOR)
    pg.draw.rect(screen, BLUE, rect)
    pg.display.flip()
    clock.tick(60)

pg.quit()
将pygame导入为pg
第init页()
屏幕=pg.display.set_模式((640480))
时钟=pg.time.clock()
BG_COLOR=pg.COLOR('gray13')
蓝色=pg.Color('dodgerblue1')
rect=pg.rect(300,200,20,20)
距离=150
速度=1
完成=错误
虽然没有这样做:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
完成=正确
#检查rect是否在指定的范围之外
#区域以及它是否向左或向右移动。
如果(右直>300+距离和速度>0
或左直<300且速度<0):
速度*=-1#反转速度。
#将速度添加到矩形的x属性以移动它。
矩形x+=速度
#画所有的东西。
屏幕填充(背景颜色)
pg.draw.rect(屏幕、蓝色、rect)
pg.display.flip()
时钟滴答(60)
第页退出

您只需检查物体是否在该区域之外,然后反转速度即可。s有许多属性,如
left
right
,在这里很有用

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray13')
BLUE = pg.Color('dodgerblue1')

rect = pg.Rect(300, 200, 20, 20)
distance = 150
speed = 1

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    # Check if the rect is outside of the specified
    # area and if it's moving to the left or right.
    if (rect.right > 300+distance and speed > 0
            or rect.left < 300 and speed < 0):
        speed *= -1  # Invert the speed.
    # Add the speed to the rect's x attribute to move it.
    rect.x += speed

    # Draw everything.
    screen.fill(BG_COLOR)
    pg.draw.rect(screen, BLUE, rect)
    pg.display.flip()
    clock.tick(60)

pg.quit()
将pygame导入为pg
第init页()
屏幕=pg.display.set_模式((640480))
时钟=pg.time.clock()
BG_COLOR=pg.COLOR('gray13')
蓝色=pg.Color('dodgerblue1')
rect=pg.rect(300,200,20,20)
距离=150
速度=1
完成=错误
虽然没有这样做:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
完成=正确
#检查rect是否在指定的范围之外
#区域以及它是否向左或向右移动。
如果(右直>300+距离和速度>0
或左直<300且速度<0):
速度*=-1#反转速度。
#将速度添加到矩形的x属性以移动它。
矩形x+=速度
#画所有的东西。
屏幕填充(背景颜色)
pg.draw.rect(屏幕、蓝色、rect)
pg.display.flip()
时钟滴答(60)
第页退出
您可以使用

speed  = 1
dis = 0

while True:
    dis += speed

    if dis >= 10:
        speed = -1
    elif dis <= 0:
        speed = 1
你可以用

speed  = 1
dis = 0

while True:
    dis += speed

    if dis >= 10:
        speed = -1
    elif dis <= 0:
        speed = 1

使用
print()
查看变量中的值以及执行代码的哪一部分。这有助于发现错误。可能您必须添加变量,如
增加速度=True
减少速度=False
状态=“增加”
状态=“减少”
,以更好地控制它。使用
打印()
查看变量中的值,并查看执行代码的哪一部分。这有助于发现错误。可能您必须添加变量,如
增加速度=真
减少速度=假
状态=“增加”
状态=“减少”
以更好地控制它。您可以添加代码来代替
#Set speed
#Based
以使其更有用:)您可以添加代码来代替
#Set speed
#Based
以使其更有用:)如果速度
,您可以使用一个
dis speed
,而不是两个
。如果spee将<0,则得到
dis+=-1
,如果速度将>0,则得到
dis+=1
,如果速度
,则可以使用一个
dis+=speed
,而不是两个
。如果spee将<0,那么您将得到
dis+=1
,如果spee将>0,那么您将得到
dis+=1
迄今为止最优雅的答案。这太棒了!非常感谢。迄今为止最优雅的答案。这太棒了!非常感谢。