Pygame:细胞越大,速度越慢

时间:2015-06-22 05:04:40

标签: python pygame

我目前正在制作this game的愚蠢版本。

在游戏中,你的细胞越大,你应该越慢。
这应该是相称的。然而,在我的代码中,你可以看到我只是设置了一堆范围来使单元格以特定的速度运行。有没有办法实现比例速度/大小而不是设定范围?

这是我的代码。

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

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

# set up the window
windowwidth = 800
windowheight = 600
thesurface = pygame.display.set_mode((windowwidth, windowheight), 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)
size = 10
playercolor = BLACK
# set up the cell and food data structure
foodCounter = 0
NEWFOOD = 35
FOODSIZE = 10
cell = pygame.draw.circle(thesurface, playercolor, (60, 250), 40)
foods = []
for i in range(20):
    foods.append(pygame.Rect(random.randint(0, windowwidth - FOODSIZE), random.randint(0, windowheight - FOODSIZE), FOODSIZE, FOODSIZE))

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

MOVESPEED = 10

score = 0
# run the game loop
while True:
    # 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 == ord('x'):
                cell.top = random.randint(0, windowheight - cell.windowheight)
                cell.left = random.randint(0, windowwidth - cell.windowwidth)

            # split the cell
            if event.key == K_SPACE:
                pygame.draw.circle(thesurface, playercolor,(cell.centerx,cell.centery),int(size/2))
                pygame.draw.circle(thesurface, playercolor,(cell.centerx+size,cell.centery+size),int(size/2))      
        if event.type == KEYUP:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_LEFT:
                moveLeft = False
            if event.key == K_RIGHT:
                moveRight = False
            if event.key == K_UP:
                moveUp = False
            if event.key == K_DOWN:
                moveDown = False


        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, windowwidth - FOODSIZE), random.randint(0, windowheight - FOODSIZE), FOODSIZE, FOODSIZE))

# The bigger the cell, the slower it should go.

    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 cell
    if moveDown and cell.bottom < windowheight:
        cell.top += MOVESPEED
    if moveUp and cell.top > 0:
        cell.top -= MOVESPEED
    if moveLeft and cell.left > 0:
        cell.left -= MOVESPEED
    if moveRight and cell.right < windowwidth:
        cell.right += MOVESPEED

    # display background        
    thesurface.blit(bg, (0, 0))

    # draw the cell onto the surface
    cell = pygame.draw.circle(thesurface, playercolor, cell.center, size)

    # check if the cell has intersected with any food squares.
    for food in foods[:]:
        if cell.colliderect(food):
            foods.remove(food)
            size+=1
            score+=1

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

    # show the score
    printscore = basicFont.render("Score: %d" % score, True, (0,0,0))
    thesurface.blit(printscore, (10, 550))

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

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

得分的一系列值如何,并使用divmod计算这个lsit的适当索引?

示例:

>>> speeds = range(50, 800, 50)
>>> MOVESPEED, _ = divmod(130, 50)
>>> MOVESPEED
2
>>> MOVESPEED, remainder = divmod(150, 50)
>>> MOVESPEED
3