Python Pygame:电池越大,速度越慢

Python Pygame:电池越大,速度越慢,python,pygame,Python,Pygame,我目前正在制作一个简化版的 在游戏中,你的手机越大,你应该越慢。 这应该是相称的。然而,在我的代码中,你可以看到我只是设置了一系列范围,使细胞以一定的速度运行。是否有办法实现成比例的速度/大小而不是设定范围 这是我的密码 import pygame, sys, random from pygame.locals import * # set up pygame pygame.init() mainClock = pygame.time.Clock() # set up the window

我目前正在制作一个简化版的

在游戏中,你的手机越大,你应该越慢。
这应该是相称的。然而,在我的代码中,你可以看到我只是设置了一系列范围,使细胞以一定的速度运行。是否有办法实现成比例的速度/大小而不是设定范围

这是我的密码

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)
import pygame,sys,random
从pygame.locals导入*
#设置pygame
pygame.init()
mainClock=pygame.time.Clock()
#开窗
窗宽=800
窗高=600
thesurface=pygame.display.set_模式((窗口宽度,窗口高度),0,32)
pygame.display.set_标题(“”)
#bg=pygame.image.load(“bg.png”)
basicFont=pygame.font.SysFont('calibri',36)
#设置颜色
黑色=(0,0,0)
绿色=(0,255,0)
白色=(255,255,255)
蓝色=(0,0255)
尺寸=10
playercolor=黑色
#建立细胞和食物数据结构
foodCounter=0
新食品=35
食品尺寸=10
cell=pygame.draw.circle(表面,玩家颜色,(60250),40)
食品=[]
对于范围(20)内的i:
foods.append(pygame.Rect(random.randint(0,windowwidth-FOODSIZE),random.randint(0,windowheight-FOODSIZE),FOODSIZE,FOODSIZE))
#设置运动变量
moveLeft=False
moveRight=False
moveUp=False
向下移动=错误
移动速度=10
分数=0
#运行游戏循环
尽管如此:
#检查事件
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
#更改键盘变量
如果event.key==K_LEFT或event.key==ord('a'):
moveRight=False
moveLeft=True
如果event.key==K_RIGHT或event.key==ord('d'):
moveLeft=False
moveRight=True
如果event.key==K_UP或event.key==ord('w'):
向下移动=错误
向上移动=真
如果event.key==K_DOWN或event.key==ord('s'):
moveUp=False
向下移动=真
如果event.key==ord('x'):
cell.top=random.randint(0,windowheight-cell.windowheight)
cell.left=random.randint(0,windowwidth-cell.windowwidth)
#分裂细胞
如果event.key==K_空间:
pygame.draw.circle(表面,玩家颜色,(cell.centerx,cell.centery),整数(大小/2))
pygame.draw.circle(表面,玩家颜色,(单元格中心X+大小,单元格中心Y+大小),整数(大小/2))
如果event.type==KEYUP:
如果event.key==K_转义:
pygame.quit()
sys.exit()
如果event.key==K_LEFT:
moveLeft=False
如果event.key==K_RIGHT:
moveRight=False
如果event.key==K_UP:
moveUp=False
如果event.key==K_DOWN:
向下移动=错误
如果event.type==MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0],event.pos[1],FOODSIZE,FOODSIZE))
foodCounter+=1
如果foodCounter>=新食品:
#添加新食物
foodCounter=0
foods.append(pygame.Rect(random.randint(0,windowwidth-FOODSIZE),random.randint(0,windowheight-FOODSIZE),FOODSIZE,FOODSIZE))
#电池越大,速度就越慢。
如果100>分数>50:
移动速度=9
elif 150>分数>100:
移动速度=8
elif 250>分数>150:
移动速度=6
elif 400>分数>250:
移动速度=5
elif 600>分数>400:
移动速度=3
elif 800>分数>600:
移动速度=2
elif评分>800分:
移动速度=1
#移动单元格
如果向下移动且cell.bottom<窗口高度:
cell.top+=移动速度
如果moveUp和cell.top>0:
cell.top-=移动速度
如果moveLeft和cell.left>0:
cell.left-=移动速度
如果moveRight和cell.right

谢谢你的帮助

关于分数的一系列值如何,并使用divmod计算该lsit的适当指数

示例:

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

分数的一系列值如何,并使用divmod计算此lsit的适当索引

示例:

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

签出
多项式插值
签出
多项式插值
我不确定如何在该代码中实现,只需将
如果
/
elif
的块替换为上述内容,并将
速度范围
定义为模块级变量。我不确定如何在该代码中实现代码只需将
if
/
elif
块替换为上述内容,并将
速度范围定义为模块级变量。