Pygame:细胞物理学

编程入门 行业动态 更新时间:2024-10-24 02:28:02
本文介绍了Pygame:细胞物理学的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm making a dumbed down version of agar.io.

目前,单元格放置在网格背景上,就像在游戏中一样.细胞吃食物,但实际上只有圆圈内较小的正方形部分吃食物(在我的程序中),当你足够大时,这一点很明显.此外,当您按下空格键时,它会将单元格分成 2 个更小的部分,几秒钟后它们会合并回来.这将需要 KEY_UP 和 K_SPACE 事件,但我不确定如何实现.此外,一旦你的质量接近 34,你可以按 w 拍摄自己的一小部分,一个较小的细胞,设置质量约为 14.

Currently, the cell is placed upon a grid background like in the game. The cell eats food, but only a smaller square section inside the circle actually eats the food (in my program), which is noticeable when you are big enough. Also, when you press the spacebar, it would split the cell into 2 smaller parts, which after a few seconds would merge back. This would require a KEY_UP and K_SPACE event, but I am not sure how to implement that. Also, once you are around a mass of 34, you can press w to shoot a tiny bit of yourself, a smaller cell at around 14 set mass.

我试图用一堆 if 语句在单元达到一定质量后减慢它的速度.在游戏中,它自然会变慢.这里和这里是描述游戏中使用的数学的来源.

I attempted to slow down the cell once it reaches a certain mass with a bunch of if statements. In the game, it slows down naturally. Here and Here are sources depicting the math used in the game.

这是我的代码:

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)

再一次,这里是我想要解决的问题.

Once again, here are the issues I want to solve.

  • 我希望在按下空格键时将单元格分成 2 部分.仅当单元格大小超过 30 时才会发生这种情况.
  • 我希望当您按下 W 时,细胞会吐出自身的一小部分.吐出的部分将是一组 15 质量.原始单元格将小 15.多次击打 W 将允许吐出多个小球,直到原始细胞的质量为 20,在这种情况下,无法再吐出.

我试过做分裂的事情:

if event.key == K_SPACE: 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))

将上述代码放入后,运行程序并按空格键,没有任何反应.该程序就像我从未按下过它一样.

After putting the above code in, and running the program and pressing the spacebar, nothing happens. The program acts like I never pressed it.

推荐答案

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

You will need to move the .draw call to after the call to blit your background else it will cover your player's circle. I use a boolean flag here which you could then use with a timer to toggle whenever you want the split to turn off:

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)

你肯定也想把它变成一些函数和/或类.

You will definitely want to make this into a few functions and/or classes too.

更多推荐

Pygame:细胞物理学

本文发布于:2023-06-04 19:08:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503347.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:物理学   细胞   Pygame

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!