如何在一段时间无限循环中使对象移动?

时间:2015-06-11 11:58:30

标签: python recursion pygame

我一直试图创造一款游戏。在游戏中有一架飞机会飞入递归的背景矩形。矩形是通过无限循环创建的,所以一旦它进入循环,其余的函数就不会起作用,例如对象。我的代码在下面,问题出现在主循环中。我希望对象与递归矩形一起移动,但是当矩形开始在循环中绘制时它会冻结。 PS有助于解决这个问题,因为我已经尝试了几乎每个代码示例来修复它。谢谢

编辑:游戏的主要功能是使喷气机看起来像矩形一样递归(有两个和while循环使它同时上下移动,让人觉得喷气机正在进入屏幕。)。但是由于首先绘制了喷射,当程序进入矩形循环时,喷射器冻结并且不能移动。我希望喷射器在背景移动的同时移动。

import pygame
import random

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
Blue = (2,55,55)
black=(0,0,0)
end_it=False 

def recursive_draw(x, y, width, height):
    """ Recursive rectangle function. """
    pygame.draw.rect(screen, WHITE,
                     [x, y, width, height],
                     1)
    speed = [10,0]
    rect_change_x = 10
    rect_change_y = 10




    # Is the rectangle wide enough to draw again?
    if (width > 25):
        # Scale down
        x += width * .1
        y += height * .1
        width *= .8
        height *= .8


               # Recursively draw again
        recursive_draw(x, y, width, height)

def recursive_draw2(x, y, width, height):
    """ Recursive rectangle function. """
    pygame.draw.rect(screen, Blue,
                     [x, y, width, height],
                     1)
    speed = [10,0]
    rect_change_x = 10
    rect_change_y = 10




    # Is the rectangle wide enough to draw again?
    if (width > 25):
        x += width * .1
        y += height * .1
        width *= .8
        height *= .8



               # Recursively draw again
        recursive_draw2(x, y, width, height)
    '''
def timer(self):
    screen.fill(black)
    myfont=pygame.font.SysFont("Britannic Bold", 40)
    label2=myfont.render("Ready?", 1, (255, 0, 0))
    screen.blit(label2, (350,250))
    self =3
    nlist=[]
    for i in range (2):
        score = myfont.render(str(self),1,(255,0,0))
        screen.blit((score), (350,250))
        self = self - 1
        nlist.append(self)
        pygame.display.flip()

'''



pygame.init()
#rectanglelist = [big()] 
# Set the height and width of the screen
size = [700, 500]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("My Game")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

USEREVENT = 0

pygame.time.set_timer(USEREVENT+1, 10)
milliseconds = 0
seconds = 0
start_it = False
while (end_it==False):
    screen.fill(black)
    myfont=pygame.font.SysFont("Britannic Bold", 40)
    nlabel=myfont.render("Welcome to "+ " Jet shooter ", 1, (255, 0, 0))
    label=myfont.render("Click on the mouse to start ", 1, (255, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            end_it=True

    screen.blit(nlabel,(200, 100))
    screen.blit(label, (170,300))
    pygame.display.flip()

while (start_it==False):

    screen.fill(black)
    myfont2=pygame.font.SysFont("Britannic Bold", 40)
    label2=myfont2.render("Ready?", 1, (255, 0, 0))
    screen.blit(label2, (300,250))
    pygame.display.flip()
    pygame.time.wait(3000)
    start_it = True
fall = False   
while (fall==False):
    nlist = [3,2,1]
    for i in (nlist):


        screen.fill(black)
        n = str(i)
        myfont3=pygame.font.SysFont("Britannic Bold", 40)
        score = myfont3.render(n,1,(255,0,0))
        screen.blit((score), (350,250))
        pygame.display.flip()
        pygame.time.wait(1000)
    screen.fill(black)
    myfont4=pygame.font.SysFont("Britannic Bold", 40)
    label4=myfont3.render("GOOO!!!", 1, (255, 0, 0))
    screen.blit(label4, (300,250))
    pygame.display.flip()
    pygame.time.wait (1000)

    fall = True

b = 0
flip = 1
a = 0



time = 100
x_speed = 0
y_speed = 0

x_coord = 320
y_coord = 400

image = pygame.image.load("spaceship.gif").convert()



# -------- Main Program Loop -----------

while not done:
    for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                done = True
            # User pressed down on key
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_speed = -10
                if event.key == pygame.K_RIGHT:
                    x_speed = 10
                if event.key == pygame.K_UP:
                    y_speed = -10
                if event.key == pygame.K_DOWN:
                    y_speed = 10

            # User let go of key
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                        x_speed = 0
                if event.key == pygame.K_RIGHT:
                        x_speed = 0
                if event.key == pygame.K_UP:
                        y_speed = 0
                if event.key == pygame.K_DOWN:
                        y_speed = 0

    x_coord +=  x_speed
    y_coord +=  y_speed


    # Set the screen background
    screen.fill(BLACK)

    screen.blit(image, [x_coord,y_coord])  

    pygame.display.flip()
    clock.tick(60)






# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT



        # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT


        # Go ahead and update the screen with what we've drawn.

    # Limit to 60 frames per second


    # Be IDLE friendly. If you forget this line, the program will 'hang'
    # on exit.

    while  a == 0 :



        if flip == 1 :
             recursive_draw(35,25,625,450)
             recursive_draw2(0, 0, 700, 500)


             flip = flip + 1
             pygame.display.flip()
             clock.tick(60)




        if flip == 2 :
             recursive_draw(0, 0, 700, 500)
             recursive_draw2(35, 25, 625, 450)



             flip = flip - 1
             pygame.display.flip()
             clock.tick(60)


pygame.quit()

2 个答案:

答案 0 :(得分:0)

正如@Paul Rooney所说,你会想要使用线程。这是你将如何

import thread
.
.
.
# Instead of calling the function as you are, run it on a new thread
thread.start_new_thread(drawRectanglesFunction, ())

答案 1 :(得分:0)

据我所知,你正在以不正确的顺序绘制东西,我相信不需要循环。 (而a == 0)。另一件事是你经常翻转显示器,很难跟踪首先绘制的内容,以及之后绘制的内容。我的建议是快速重写你的程序,使它看起来像这样:

flip = 1
while not done:
    ##Catch and handle events()
    screen.fill(BLACK)
    if flip == 1 :
         recursive_draw(35,25,625,450)
         recursive_draw2(0, 0, 700, 500)
         flip = flip + 1
    elif flip == 2 :
         recursive_draw(0, 0, 700, 500)
         recursive_draw2(35, 25, 625, 450)
         flip = flip - 1
    screen.blit(image, [x_coord,y_coord])  
    pygame.display.flip()
    clock.tick(60);

我希望这会有所帮助。

(编辑:我自己做了这个并让程序运行)