2015-06-22 181 views
2

this question中,我想通過按空格鍵來弄清楚如何分割細胞。我現在想知道如何讓它們自己合併在一起,假設在30秒之後。新合併的細胞將其他兩個細胞的質量合併在一起。Pygame:細胞合併回來

import pygame, sys, random 
from pygame.locals import * 

# set up pygame 
pygame.init() 
mainClock = pygame.time.Clock() 

# set up the window 
width = 800 
height = 600 
thesurface = pygame.display.set_mode((width, height), 0, 32) 
pygame.display.set_caption('') 

bg = pygame.image.load("bg.png") 


basicFont = pygame.font.SysFont('calibri', 36) 

# set up the colors 
BLACK = (0, 0, 0) 
GREEN = (0, 255, 0) 
WHITE = (255, 255, 255) 
BLUE = (0, 0, 255) 
playercolor = BLUE 
# set up the player and food data structure 
foodCounter = 0 
NEWFOOD = 20 
FOODSIZE = 10 
splitting = False 
player = pygame.draw.circle(thesurface, playercolor, (60, 250), 40) 
foods = [] 
for i in range(20): 
    foods.append(pygame.Rect(random.randint(0, width - FOODSIZE), random.randint(0, height - FOODSIZE), FOODSIZE, FOODSIZE)) 

# set up movement variables 
moveLeft = False 
moveRight = False 
moveUp = False 
moveDown = False 

MOVESPEED = 10 
size = 10 
score = size 


# run the game loop 
while True: 
    thesurface.blit(bg, (0, 0)) 
    # check for events 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     if event.type == KEYDOWN: 
      # change the keyboard variables 
      if event.key == K_LEFT or event.key == ord('a'): 
       moveRight = False 
       moveLeft = True 
      if event.key == K_RIGHT or event.key == ord('d'): 
       moveLeft = False 
       moveRight = True 
      if event.key == K_UP or event.key == ord('w'): 
       moveDown = False 
       moveUp = True 
      if event.key == K_DOWN or event.key == ord('s'): 
       moveUp = False 
       moveDown = True 
      if event.key == K_SPACE and size >= 32: # XXX if size and space set splitting to true 
       splitting = True 
     if event.type == KEYUP: 
      if event.key == K_ESCAPE: 
       pygame.quit() 
       sys.exit() 
      if event.key == K_LEFT or event.key == ord('a'): 
       moveLeft = False 
      if event.key == K_RIGHT or event.key == ord('d'): 
       moveRight = False 
      if event.key == K_UP or event.key == ord('w'): 
       moveUp = False 
      if event.key == K_DOWN or event.key == ord('s'): 
       moveDown = False 
      if event.key == ord('x'): 
       player.top = random.randint(0, height - player.height) 
       player.left = random.randint(0, width - player.width) 

     if event.type == MOUSEBUTTONUP: 
      foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE)) 

    foodCounter += 1 
    if foodCounter >= NEWFOOD: 
     # add new food 
     foodCounter = 0 
     foods.append(pygame.Rect(random.randint(0, width - FOODSIZE), random.randint(0, height - FOODSIZE), FOODSIZE, FOODSIZE)) 
    if 100>score>50: 
     MOVESPEED = 9 
    elif 150>score>100: 
     MOVESPEED = 8 
    elif 250>score>150: 
     MOVESPEED = 6 
    elif 400>score>250: 
     MOVESPEED = 5 
    elif 600>score>400: 
     MOVESPEED = 3 
    elif 800>score>600: 
     MOVESPEED = 2 
    elif score>800: 
     MOVESPEED = 1 
    # move the player 
    if moveDown and player.bottom < height: 
     player.top += MOVESPEED 
    if moveUp and player.top > 0: 
     player.top -= MOVESPEED 
    if moveLeft and player.left > 0: 
     player.left -= MOVESPEED 
    if moveRight and player.right < width: 
     player.right += MOVESPEED 

    # draw the player onto the surface 
    if not splitting: 
     pygame.draw.circle(thesurface, playercolor, player.center, size) 
    else: 
     pygame.draw.circle(thesurface, playercolor,(player.centerx,player.centery),int(size/2)) 
     pygame.draw.circle(thesurface, playercolor,(player.centerx+size,player.centery+size),int(size/2)) 
    # check if the player has intersected with any food squares. 
    for food in foods[:]: 
     if player.colliderect(food): 
      foods.remove(food) 
      size+=1 

    # draw the food 
    for i in range(len(foods)): 
     pygame.draw.rect(thesurface, GREEN, foods[i]) 

    printscore = basicFont.render("Score: %d" % size, True, (0,0,0)) 
    thesurface.blit(printscore, (10, 550)) 

    pygame.display.update() 
    # draw the window onto the thesurface 
    pygame.display.update() 
    mainClock.tick(80) 

我覺得這涉及刪除兩個圓圈和添加一個,但我不知道如何與它前進,因爲該方案現在似乎脆弱。

非常感謝幫助!

回答

1

Agar.io type game?

嘗試創建一個新的計時器線程。

import time,threading 
go = False 
def timer(): # pause for 30 seconds, then change "go" to true. 
    global go 
    time.sleep(30) 
    go = True 

然後使用threading.Thread(target=timer).start()啓動計時器。之後,檢查每個遊戲的打勾,看看go是否爲真,這意味着30秒計時器已啓動。對不起,如果這是令人困惑。我無法很好地解釋。