python - Pygame: Cell Physics -
i'm making dumbed down version of agar.io.
currently, cell placed upon grid background in game. cell eats food, smaller square section inside circle eats food (in program), noticeable when big enough. also, when press spacebar, split cell 2 smaller parts, after few seconds merge back. require key_up
, k_space
event, not sure how implement that. also, once around mass of 34, can press w shoot tiny bit of yourself, smaller cell @ around 14 set mass.
i attempted slow down cell once reaches mass bunch of if statements. in game, slows down naturally. here , here sources depicting math used in game.
here code have:
import pygame, sys, random pygame.locals import * # set pygame pygame.init() mainclock = pygame.time.clock() # set 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 colors black = (0, 0, 0) green = (0, 255, 0) white = (255, 255, 255) blue = (0, 0, 255) size = 10 playercolor = blue # set player , food data structure foodcounter = 0 newfood = 35 foodsize = 10 player = pygame.draw.circle(thesurface, playercolor, (60, 250), 40) foods = [] in range(20): foods.append(pygame.rect(random.randint(0, width - foodsize), random.randint(0, height - foodsize), foodsize, foodsize)) # set movement variables moveleft = false moveright = false moveup = false movedown = false movespeed = 10 score = 0 # run game loop while true: # check events event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() if event.type == keydown: # change 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 player if movedown , player.bottom < height: player.top += movespeed if moveup , player.top > 0: player.top -= movespeed if moveleft , player.left > 0: player.left -= movespeed if moveright , player.right < width: player.right += movespeed thesurface.blit(bg, (0, 0)) # draw player onto surface pygame.draw.circle(thesurface, playercolor, player.center, size) # check if player has intersected food squares. food in foods[:]: if player.colliderect(food): foods.remove(food) size+=1 score+=1 # draw food 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 window onto thesurface pygame.display.update() mainclock.tick(80)
once again, here issues want solve.
- i want cell split 2 parts when spacebar pressed. can happen if cell above 30 size.
- i want cell spit out fraction of when press w. part spit out set 15 mass. original cell 15 smaller. hitting w multiple times allow multiple small balls spit out, until original cell's mass 20, in case isn't viable spit out more.
edit: have tried doing splitting thing:
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 above code in, , running program , pressing spacebar, nothing happens. program acts never pressed it.
you need move .draw
call after call blit background else cover player's circle. use boolean flag here use timer toggle whenever want split turn off:
import pygame, sys, random pygame.locals import * # set pygame pygame.init() mainclock = pygame.time.clock() # set 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 colors black = (0, 0, 0) green = (0, 255, 0) white = (255, 255, 255) blue = (0, 0, 255) size = 10 playercolor = blue # set player , food data structure foodcounter = 0 newfood = 35 foodsize = 10 splitting = false player = pygame.draw.circle(thesurface, playercolor, (60, 250), 40) foods = [] in range(20): foods.append(pygame.rect(random.randint(0, width - foodsize), random.randint(0, height - foodsize), foodsize, foodsize)) # set movement variables moveleft = false moveright = false moveup = false movedown = false movespeed = 10 score = 0 # run game loop while true: # check events event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() if event.type == keydown: # change 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 , size >= 30: # xxx if size , space set splitting 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 player if movedown , player.bottom < height: player.top += movespeed if moveup , player.top > 0: player.top -= movespeed if moveleft , player.left > 0: player.left -= movespeed if moveright , player.right < width: player.right += movespeed thesurface.blit(bg, (0, 0)) # draw player onto surface if not splitting: # xxx check split flag , 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 player has intersected food squares. food in foods[:]: if player.colliderect(food): foods.remove(food) size+=1 score+=1 # draw food 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 window onto thesurface pygame.display.update() mainclock.tick(80)
you want make few functions and/or classes too.
Comments
Post a Comment