Pygame: Cell Physics
I'm making a dumbed down version of agar.io. 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
Solution 1:
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.localsimport *
# 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 inrange(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 loopwhileTrue:
# check for eventsfor event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change the keyboard variablesif event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = Trueif event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = Trueif event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = Trueif event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = Trueif event.key == K_SPACE and size >= 30: # XXX if size and space set splitting to true
splitting = Trueif event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = Falseif event.key == K_RIGHT or event.key == ord('d'):
moveRight = Falseif event.key == K_UP or event.key == ord('w'):
moveUp = Falseif event.key == K_DOWN or event.key == ord('s'):
moveDown = Falseif 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 += 1if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, width - FOODSIZE), random.randint(0, height - FOODSIZE), FOODSIZE, FOODSIZE))
if100>score>50:
MOVESPEED = 9elif150>score>100:
MOVESPEED = 8elif250>score>150:
MOVESPEED = 6elif400>score>250:
MOVESPEED = 5elif600>score>400:
MOVESPEED = 3elif800>score>600:
MOVESPEED = 2elif score>800:
MOVESPEED = 1# move the playerif 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 surfaceifnot 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 foodfor i inrange(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.
Post a Comment for "Pygame: Cell Physics"