0

I currently am working on a 'Flappy Bird' remake in Pygame using Python 3.2. I thought it would be good for practice, and relativly simple. However, it is proving to be hard. Currently, I am having a problem when drawing a rectangle at different heights but keeping the rectangle at the height it is set to.

Here is my Pipe class

class Pipe:
    def __init__(self,x):
        self.drawn = True
        self.randh = random.randint(30,350)
        self.rect = Rect((x,0),(30,self.randh))

    def update(self):
        self.rect.move_ip(-2,0)

    def draw(self,screen):
        self.drawn = True
        pygame.draw.rect(screen,(0,130,30),self.rect)

My while Loop is as follows:

while True:
    for event in pygame.event.get():
        movey = +0.8
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                movey = -2


    x += movex
    y += movey


    screen.blit(background,(0,0))
    screen.blit(bird,(x,y))

    Pipe1 = Pipe(scrollx)

    if Pipe1.drawn == True:
        Pipe1.update()
    else:
        Pipe1 = Pipe(scrollx)
        Pipe1.draw(screen)

    scrollx -= 0.3

    pygame.display.update()

I have being wrestling with this code for over a week, and I really appreciate any help you can give.

4

2 回答 2

1

我没有遵循这部分的逻辑:

Pipe1 = Pipe(scrollx)

if Pipe1.drawn == True:
    Pipe1.update()
else:
    Pipe1 = Pipe(scrollx)
    Pipe1.draw(screen)

drawn属性True在构造函数中设置为,那么您希望何时else触发条件?请记住,您每帧都在重新创建此管道。

你试过像画鸟一样画管子吗?

编辑:给你的循环建议:

PIPE_TIME_INTERVAL = 2

pipes = []    # Keep the pipes in a list.
next_pipe_time = 0

while True:
    [... existing code to handle events and draw the bird ...]

    for pipe in pipes:
        pipe.move(10)     # You'll have to write this `move` function.
        if pipe.x < 0:    # If the pipe has moved out of the screen...
            pipes.pop(0)  # Remove it from the list.

    if current_time >= next_pipe_time:   # Find a way to get the current time/frame.
        pipes.append(Pipe())  # Create new pipe.
        next_pipe_time += PIPE_TIME_INTERVAL  # Schedule next pipe creation.
于 2014-02-14T18:08:01.333 回答
0

您在每个循环上都创建一个新Pipe的,但永远不要挂在旧的上,所以每次都会得到一个新的随机高度。移动这一行:

Pipe1 = Pipe(scrollx)

while循环之外。更好的是,有一个管道列表,您可以添加新管道并轻松更新它们。你从来没有设置self.drawn = FalsePipe任何一个范围内。

此外,您正在movey为每个事件重置,请尝试:

movey = 0.8 # no need for plus 
for event in pygame.event.get():
于 2014-02-14T18:07:29.630 回答