Python pygame.draw.circle,仍然绘制一个正方形

Python pygame.draw.circle,仍然绘制一个正方形,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在制作一个版本的,我正在尝试为玩家制作圆形。我一直在尝试使用pygame.draw.circle来绘制它,但是它一直显示一个正方形。谢谢你的帮助 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 = py

我正在制作一个版本的,我正在尝试为玩家制作圆形。我一直在尝试使用
pygame.draw.circle
来绘制它,但是它一直显示一个正方形。谢谢你的帮助

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)
import pygame,sys,random
从pygame.locals导入*
#设置pygame
pygame.init()
mainClock=pygame.time.Clock()
#开窗
宽度=600
高度=800
screen=pygame.display.set_模式((宽度、高度),0,32)
pygame.display.set_标题('Agar.io'))
#设置颜色
黑色=(0,0,0)
绿色=(0,255,0)
白色=(255,255,255)
#设置播放器和食物数据结构
foodCounter=0
新食品=0
食品尺寸=20
player=pygame.draw.circle(屏幕,白色,(60250),40)
食品=[]
对于范围(20)内的i:
foods.append(pygame.Rect(random.randint(0,宽度-FOODSIZE),random.randint(0,高度-FOODSIZE),FOODSIZE,FOODSIZE))
#设置运动变量
moveLeft=False
moveRight=False
moveUp=False
向下移动=错误
移动速度=10
#运行游戏循环
尽管如此:
#检查事件
对于pygame.event.get()中的事件:
如果event.type==退出:
pygame.quit()
sys.exit()
如果event.type==KEYDOWN:
#更改键盘变量
如果event.key==K_LEFT或event.key==ord('a'):
moveRight=False
moveLeft=True
如果event.key==K_RIGHT或event.key==ord('d'):
moveLeft=False
moveRight=True
如果event.key==K_UP或event.key==ord('w'):
向下移动=错误
向上移动=真
如果event.key==K_DOWN或event.key==ord('s'):
moveUp=False
向下移动=真
如果event.type==KEYUP:
如果event.key==K_转义:
pygame.quit()
sys.exit()
如果event.key==K_LEFT或event.key==ord('a'):
moveLeft=False
如果event.key==K_RIGHT或event.key==ord('d'):
moveRight=False
如果event.key==K_UP或event.key==ord('w'):
moveUp=False
如果event.key==K_DOWN或event.key==ord('s'):
向下移动=错误
如果event.key==ord('x'):
player.top=random.randint(0,高度-player.height)
player.left=random.randint(0,宽度-player.width)
如果event.type==MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0],event.pos[1],FOODSIZE,FOODSIZE))
foodCounter+=1
如果foodCounter>=新食品:
#添加新食物
foodCounter=0
foods.append(pygame.Rect(random.randint(0,宽度-FOODSIZE),random.randint(0,高度-FOODSIZE),FOODSIZE,FOODSIZE))
#在曲面上绘制黑色背景
屏幕填充(黑色)
#移动玩家
如果向下移动且player.bottom<高度:
player.top+=移动速度
如果moveUp和player.top>0:
player.top-=移动速度
如果moveLeft和player.left>0:
player.left-=移动速度
如果moveRight和player.right<宽度:
player.right+=移动速度
#将玩家拉到水面上
pygame.draw.rect(屏幕、白色、玩家)
#检查玩家是否与任何食物广场相交。
对于食品中的食品[:]:
如果玩家.colliderect(食物):
移除(食物)
播放器宽度+=1
玩家身高+=1
#画食物
对于范围内的i(len(食品)):
pygame.draw.rect(屏幕、绿色、食物[i])
#把窗户拉到屏幕上
pygame.display.update()
主时钟滴答(40)

您眼前的问题可以通过使用
pygame.draw.circle
而不是
pygame.draw.rect
来解决:

# draw the player onto the surface
pygame.draw.circle(screen, WHITE, player.center, 40)
您正在使用以下内容创建您的
player
对象:

player = pygame.draw.circle(screen, WHITE, (60, 250), 40)

pygame.draw.circle
返回一个Rect对象(不是您可能期望的圆对象),您可以使用其
.center
属性作为圆的中心

这是我的agar.io代码:

import pygame,random,math

pygame.init()
place_10 = 500
place_9 = random.randint(501, 550)
place_8 = random.randint(551, 600)
place_7 = random.randint(601, 650)
place_6 = random.randint(651, 700)
place_5 = random.randint(701, 750)
place_4 = random.randint(751, 800)
place_3 = random.randint(801, 850)
place_2 = random.randint(851, 900)
place_1 = 950
colors_players = [(37,7,255),(35,183,253),(48,254,241),(19,79,251),(255,7,230),(255,7,23),(6,254,13)]
colors_cells = [(80,252,54),(36,244,255),(243,31,46),(4,39,243),(254,6,178),(255,211,7),(216,6,254),(145,255,7),(7,255,182),(255,6,86),(147,7,255)]
colors_viruses = [(66,254,71)]
screen_width, screen_height = (800,500)
surface = pygame.display.set_mode((screen_width,screen_height))
t_surface = pygame.Surface((95,25),pygame.SRCALPHA) #transparent rect for score
t_lb_surface = pygame.Surface((155,278),pygame.SRCALPHA) #transparent rect for leaderboard
t_surface.fill((50,50,50,80))
t_lb_surface.fill((50,50,50,80))
pygame.display.set_caption("Agar.io")
cell_list = list()
clock = pygame.time.Clock()
try:
    font = pygame.font.Font("Ubuntu-B.ttf",20)
    big_font = pygame.font.Font("Ubuntu-B.ttf",24)
except:
    print("Font file not found: Ubuntu-B.ttf")
    font = pygame.font.SysFont('Ubuntu',20,True)
    big_font = pygame.font.SysFont('Ubuntu',24,True)

def drawText(message,pos,color=(255,255,255)):
        surface.blit(font.render(message,1,color),pos)

def getDistance(pos1,pos2):
    px,py = pos1
    p2x,p2y = pos2
    diffX = math.fabs(px-p2x)
    diffY = math.fabs(py-p2y)

    return ((diffX**2)+(diffY**2))**(0.5)

class Camera:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.width = screen_width
        self.height = screen_height
        self.zoom = 0.5

    def centre(self,blobOrPos):
        if(isinstance(blobOrPos,Player)):
            p = blobOrPos
            self.x = (p.startX-(p.x*self.zoom))-p.startX+((screen_width/2))
            self.y = (p.startY-(p.y*self.zoom))-p.startY+((screen_height/2))
        elif(type(blobOrPos) == tuple):
            self.x,self.y = blobOrPos

class Player:
    def __init__(self,surface,name = ""):
        self.startX = self.x = random.randint(100,400)
        self.startY = self.y = random.randint(100,400)
        self.mass = 20
        self.surface = surface
        self.color = colors_players[random.randint(0,len(colors_players)-1)]
        self.name = name
        self.pieces = list()
        piece = Piece(surface,(self.x,self.y),self.color,self.mass,self.name)

    def update(self):
        self.move()
        self.collisionDetection()

    def collisionDetection(self):
        for cell in cell_list:
            if(getDistance((cell.x,cell.y),(self.x,self.y)) <= self.mass/2):
                self.mass+=0.5
                cell_list.remove(cell)
                cell = Cell(surface)
                cell_list.append(cell)

    def move(self):
        dX,dY = pygame.mouse.get_pos()
        rotation = math.atan2(dY-(float(screen_height)/2),dX-(float(screen_width)/2))*180/math.pi
        speed = 5-1
        vx = speed * (90-math.fabs(rotation))/90
        vy = 0
        if(rotation < 0):
            vy = -speed + math.fabs(vx)
        else:
            vy = speed - math.fabs(vx)
        self.x += vx
        self.y += vy

    def feed(self):
        pass

    def split(self):
        pass

    def draw(self,cam):
        col = self.color
        zoom = cam.zoom
        x = cam.x
        y = cam.y
        pygame.draw.circle(self.surface,(col[0]-int(col[0]/3),int(col[1]-col[1]/3),int(col[2]-col[2]/3)),(int(self.x*zoom+x),int(self.y*zoom+y)),int((self.mass/2+3)*zoom))
        pygame.draw.circle(self.surface,col,(int(self.x*cam.zoom+cam.x),int(self.y*cam.zoom+cam.y)),int(self.mass/2*zoom))
        if(len(self.name) > 0):
            fw, fh = font.size(self.name)
            drawText(self.name, (self.x*cam.zoom+cam.x-int(fw/2),self.y*cam.zoom+cam.y-int(fh/2)),(50,50,50))

class Piece:
    def __init__(self,surface,pos,color,mass,name,transition=False):
        self.x,self.y = pos
        self.mass = mass
        self.splitting = transition
        self.surface = surface
        self.name = name

    def draw(self):
        pass

    def update(self):
        if(self.splitting):
            pass

class Cell:
    def __init__(self,surface):
        self.x = random.randint(20,1980)
        self.y = random.randint(20,1980)
        self.mass = random.randint(6, 8)
        self.surface = surface
        self.color = colors_cells[random.randint(0,len(colors_cells)-1)]

    def draw(self,cam):
        pygame.draw.circle(self.surface,self.color,(int((self.x*cam.zoom+cam.x)),int(self.y*cam.zoom+cam.y)),int(self.mass*cam.zoom))

def spawn_cells(numOfCells):

    for i in range(numOfCells):
        cell = Cell(surface)
        cell_list.append(cell)

def draw_grid():
    for i in range(0,2001,25):
        pygame.draw.line(surface,(230,240,240),(0+camera.x,i*camera.zoom+camera.y),(2001*camera.zoom+camera.x,i*camera.zoom+camera.y),3)
        pygame.draw.line(surface,(230,240,240),(i*camera.zoom+camera.x,0+camera.y),(i*camera.zoom+camera.x,2001*camera.zoom+camera.y),3)

camera = Camera()
blob = Player(surface,"person x")
spawn_cells(2000)

def draw_HUD():
    w,h = font.size("Score: "+str(int(blob.mass*2))+" ")
    surface.blit(pygame.transform.scale(t_surface,(w,h)),(8,screen_height-30))
    surface.blit(t_lb_surface,(screen_width-160,15))
    drawText("Score: " + str(int(blob.mass*2)),(10,screen_height-30))
    surface.blit(big_font.render("Leaderboard",0,(255,255,255)),(screen_width-157,20))

    if blob.mass >= place_1:
       drawText("1. person x", (screen_width-157,20+25))

    else:
        drawText("1. team",(screen_width-157,20+25))

    if blob.mass >= place_2 and blob.mass < place_1:
        drawText("2. person x", (screen_width-157,20+25*2))

    else:
        drawText("2. help",(screen_width-157,20+25*2))

    if blob.mass >= place_3 and blob.mass < place_2:
        drawText("3. person x", (screen_width-157,20+25*3))

    else:
        drawText("3. ISIS",(screen_width-157,20+25*3))

    if blob.mass >= place_4 and blob.mass < place_3:
        drawText("4. person x", (screen_width-157,20+25*4))

    else:
        drawText("4. ur mom",(screen_width-157,20+25*4))

    if blob.mass >= place_5 and blob.mass < place_4:
        drawText("5. person x", (screen_width-157,20+25*5))

    else:
        drawText("5. w = pro team",(screen_width-157,20+25*5))

    if blob.mass >= place_6 and blob.mass < place_5:
        drawText("6. person x", (screen_width-157,20+25*6))

    else:
        drawText("6. jumbo",(screen_width-157,20+25*6))

    if blob.mass >= place_7 and blob.mass < place_6:
        drawText("7. person x", (screen_width-157,20+25*7))

    else:
        drawText("7. [cg]team",(screen_width-157,20+25*7))

    if blob.mass >= place_8 and blob.mass < place_7:
        drawText("8. person x", (screen_width-157,20+25*8))

    else:
        drawText("8. jack",(screen_width-157,20+25*8))

    if blob.mass >= place_9 and blob.mass < place_8:
        drawText("9. person x", (screen_width-157,20+25*9))

    else:
        drawText("9. doge",(screen_width-157,20+25*9))

    if blob.mass >= place_10 and blob.mass < place_9:
        drawText("10. person x",(screen_width-157,20+25*10))

    else:
        drawText("10. happy",(screen_width-157,20+25*10),(210,0,0))


while(True):
    clock.tick(70)
    for e in pygame.event.get():
        if(e.type == pygame.KEYDOWN):
            if(e.key == pygame.K_ESCAPE):
                pygame.quit()
                quit()
            if(e.key == pygame.K_SPACE):
                blob.split()
            if(e.key == pygame.K_w):
                blob.feed()
        if(e.type == pygame.QUIT):
            pygame.quit()
            quit()
    blob.update()
    camera.zoom = 100/(blob.mass)+0.3
    camera.centre(blob)
    surface.fill((242,251,255))
    #surface.fill((0,0,0))
    draw_grid()
    for c in cell_list:
        c.draw(camera)
    blob.draw(camera)
    draw_HUD()
    pygame.display.flip()
导入pygame、random、math
pygame.init()
位置10=500
place_9=random.randint(501550)
place_8=random.randint(551600)
place_7=random.randint(601650)
place_6=random.randint(651700)
place_5=random.randint(701750)
place_4=random.randint(751800)
place_3=random.randint(801850)
place_2=random.randint(851900)
位置1=950
颜色玩家=[(37,7255),(35183253),(48254241),(19,79251),(255,7230),(255,7,23),(6254,13)]
颜色单元格=[(80252,54),(36244255),(243,31,46),(4,39243),(254,6178),(255211,7),(216,6254),(145255,7),(7255182),(255,6,86),(147,7255)]
颜色\u病毒=[(66254,71)]
屏幕宽度,屏幕高度=(800500)
surface=pygame.display.set_模式((屏幕宽度、屏幕高度))
t#u surface=pygame.surface((95,25),pygame.SRCALPHA)#分数的透明矩形
t_lb_surface=pygame.surface((155278),pygame.SRCALPHA)#排行榜的透明矩形
t_表面填充((50,50,50,80))
t_lb_表面填充((50,50,50,80))
pygame.display.set_标题(“Agar.io”)
单元格列表=列表()
clock=pygame.time.clock()
尝试:
font=pygame.font.font(“Ubuntu-B.ttf”,20)
big_font=pygame.font.font(“Ubuntu-B.ttf”,24)
除:
打印(“未找到字体文件:Ubuntu-B.ttf”)
font=pygame.font.SysFont('Ubuntu',20,True)
big_font=pygame.font.SysFont('Ubuntu',24,True)
def drawText(消息、位置、颜色=(255255)):
blit(字体渲染(消息,1,颜色),pos)
def getDistance(位置1、位置2):
px,py=pos1
p2x,p2y=pos2
diffX=数学晶圆厂(px-p2x)
diffY=数学晶圆厂(py-p2y)
返回((diffX**2)+(diffY**2))**(0.5)
类摄像机:
定义初始化(自):
self.x=0
self.y=0
self.width=屏幕宽度
self.height=屏幕高度
self.zoom=0.5
def中心(自身、blobOrPos):
如果(isinstance(blobOrPos,玩家)):
p=blobOrPos
self.x=(p.startX-(p.x*self.zoom))-p.startX