细胞物理学

2024-06-02 06:03:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个简化版的agar.io。在

目前,细胞被放置在网格背景上,就像在游戏中一样。细胞吃食物,但只有圆圈内较小的正方形部分才真正吃到食物(在我的程序中),当你足够大的时候这一点很明显。另外,当你按下空格键时,它会将单元格分成两个较小的部分,几秒钟后这些部分又会合并回来。这将需要一个KEY_UPK_SPACE事件,但我不确定如何实现它。而且,一旦你的质量是34,你可以按下w来拍摄你自己的一小部分,一个质量在14左右的更小的细胞。在

我试图在细胞达到一定质量后用一堆if语句来减慢它的速度。在游戏中,它会自然减速。Here和{a3}是描述游戏中使用的数学的源代码。在

下面是我的代码:

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)
size = 10
playercolor = BLUE
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 35
FOODSIZE = 10
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

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.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
    thesurface.blit(bg, (0, 0))

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

    # check if the player has intersected with any food squares.
    for food in foods[:]:
        if player.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])

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

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

再一次,这里是我想解决的问题。在

  • 我想当空格键被按下时,单元格分成两部分。只有当单元格大小大于30时才会发生这种情况。在
  • 我希望当你按下W键时,细胞吐出一小部分,吐出的部分是一组15质量。原来的牢房要小15个。在最初的情况下,多打几次球都是不可能的,直到多出20次。在

编辑:我试过做分裂的事情:

^{pr2}$

在输入上面的代码,运行程序并按空格键之后,什么都没有发生。程序就像我从来没按过一样。在


Tags: orthekeyeventfalseifrandompygame
1条回答
网友
1楼 · 发布于 2024-06-02 06:03:16

您需要在调用后将.draw调用移动到blit您的背景,否则它将覆盖您的玩家圈子。我在这里使用了一个布尔标志,您可以使用它与计时器一起在您希望关闭分割时进行切换:

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)
size = 10
playercolor = BLUE
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 35
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

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 == K_SPACE and size >= 30: # 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
    thesurface.blit(bg, (0, 0))

    # draw the player onto the surface
    if not splitting: # XXX check the split flag and draw accordingly...
        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
            score+=1

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

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

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

当然,您也需要将其转换为几个函数和/或类。在

相关问题 更多 >