I'm making a version of http://agar.io and i'm trying to make the circle shape for the player. I have been trying to use pygame.draw.circle
to draw it, however it keeps showing a square. Thanks for help!
import pygame, sys, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
width = 600
height = 800
screen = pygame.display.set_mode((width, height), 0, 32)
pygame.display.set_caption('Agar.io')
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 0
FOODSIZE = 20
player = pygame.draw.circle(screen, WHITE, (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
# 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))
# draw the black background onto the surface
screen.fill(BLACK)
# 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
# draw the player onto the surface
pygame.draw.rect(screen, WHITE, player)
# check if the player has intersected with any food squares.
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
player.width+=1
player.height+=1
# draw the food
for i in range(len(foods)):
pygame.draw.rect(screen, GREEN, foods[i])
# draw the window onto the screen
pygame.display.update()
mainClock.tick(40)
Your immediate problem can be solved by using
pygame.draw.circle
instead ofpygame.draw.rect
:You are creating your
player
object with:pygame.draw.circle
returns a Rect object (not a circle object, which you might have expected), and you can use its.center
attribute as the center of the circle.