Pygame蛇食物发生器

时间:2012-11-18 01:28:41

标签: generator pygame

我目前正在创建一个Snake游戏,我想创建一个食物生成器,根据我的游戏内计时器每10秒生成一个苹果。计时器从60倒计时到0(当游戏结束时)我想要一个新的苹果每10秒生成一次,即使它没有被吃掉也要保留旧苹果。我不知道如何处理这个并且可以使用一些帮助。这是我的完整计划。

编辑:这是一个初学的计算机科学学校项目,所以越基本越好。

import random
import pygame
pygame.init()

#---------------------------------------#
                                        #
# window properties                     # 
width = 640         # 
height = 480                                         
game_window=pygame.display.set_mode((width,height))
black = (  0,  0,  0)                   #                   
#---------------------------------------#

# snake's properties

outline=0                               
body_size = 9
head_size = 10
apple_size = 8                          
speed_x = 8                           
speed_y = 8                         
dir_x = 0                               
dir_y = -speed_y                               
segx = [int(width/2.)]*3                 
segy = [height, height + speed_y, height + 2*speed_y]              
segments = len(segx)
apple_counter = 0
grid_step = 8

regular_font = pygame.font.SysFont("Andina",18)
blue   = [11,  90,  220]
clock = pygame.time.Clock()
time = 60
fps = 25            
time = time + 1.0/fps
text = regular_font.render("Time from start: "+str(int(time)), 1, blue)
text2 = regular_font.render("Score: "+str(int(apple_counter)), 1, blue)

apple_x = random.randrange(0, 640, grid_step)
apple_y = random.randrange(0, 480, grid_step)  
apple_colour = (255,0,0)

def redraw_game_window():               
    game_window.fill(black)             
    for i in range(segments):           
        segment_colour = (random.randint(1,50),random.randint(100,150),random.randint(1,50))
        head_colour = (random.randint(180,220),random.randint(180,220),random.randint(1,15))
        apple_colour = (255,0,0)
        pygame.draw.circle(game_window, segment_colour, (segx[i], segy[i]), body_size, outline)
        pygame.draw.circle(game_window, head_colour, (segx[0], segy[0]), head_size, outline)
    game_window.blit(text, (530, 20))
    game_window.blit(text2, (30, 20)) 
    pygame.draw.circle(game_window, apple_colour, (apple_x, apple_y), apple_size, outline)
    pygame.display.update()

exit_flag = False
print "Use the arrows and the space bar."
print "Hit ESC to end the program."

########################################################## TIMER/CONTROLS

while exit_flag == False:
    redraw_game_window()

    clock.tick(fps)                     
    time = time - 1.00/fps
    text = regular_font.render("Time: "+str(int(time)), 1, blue)
    text2 = regular_font.render("Score: "+str(int(apple_counter)), 1, blue)


    if time < 0.1:
        print "Game Over"
        exit_flag = True


    pygame.event.get()                      
    keys = pygame.key.get_pressed()

    if time == 


    if keys[pygame.K_ESCAPE]:               
        exit_flag = True

    if keys[pygame.K_LEFT] and dir_x != speed_x:
        dir_x = -speed_x
        dir_y = 0                                             
    if keys[pygame.K_RIGHT] and dir_x != -speed_x:                
        dir_x = speed_x                      
        dir_y = 0                           
    if keys[pygame.K_UP] and dir_y != speed_x:                   
        dir_x = 0                           
        dir_y = -speed_y                    
    if keys[pygame.K_DOWN] and dir_y != -speed_x:                 
        dir_x = 0                           
        dir_y = speed_y



############################################################ SNAKE MOVEMENT

    for i in range(segments-1,0,-1):          
        segx[i]=segx[i-1]                   
        segy[i]=segy[i-1]

    segx[0] = segx[0] + dir_x              
    segy[0] = segy[0] + dir_y


############################################################ COLLISION


    for i in range(segments-1, 3, -1):
        if segments > 3:
            if segx[0] == segx[i] and segy[0] == segy[i]:
                print "You have collided into yourself, Game Over."
                exit_flag = True


############################################################# BORDERS

    if segx[0] > 640 or segx[0] < 0:
        print "Game Over, you left the borders."
        break

    if segy[0] > 480 or segy[0] < 0:
        print "Game Over, you left the borders."
        break

############################################################# APPLE DETECT

    for i in range (0 , 13):
        if segx[0] == apple_x + i  and segy[0] == apple_y + i:
            segments = segments + 1            
            segx.append(segx[-1])               
            segy.append(segy[-1])
            apple_counter = apple_counter + 1


        if segx[0] == apple_x - i  and segy[0] == apple_y - i:
            segments = segments + 1            
            segx.append(segx[-1])               
            segy.append(segy[-1]) 
            apple_counter = apple_counter + 1

#############################################################

pygame.quit()

1 个答案:

答案 0 :(得分:1)

你要么

A)使用pygame.time.set_timer每10秒调用一次函数来产生食物,并且每60秒结束一次。

B)比较get_ticks()

def new_round():     last_apple = pygame.time.get_ticks()+ 10 * 1000

  

虽然如此:       now = pygame.time.get_ticks()

if now - last_apple >= 1000:
    spawn_apple()
    last_apple = now


if now - round_start >= 60*1000:
    round_end()