ethanvoo / newagario

I changed a bit of Viliami's Agar.io work. Please could you help me to solve a problem in the code. It is on line 183 to 188. Thanks.
0 stars 0 forks source link

Per your request [lines 183 to 188] #2

Open cotilloe opened 4 years ago

cotilloe commented 4 years ago

Change the following:

Line 74/75... def move(self): dX, dY = pygame.mouse.get_pos()

... TO: def move(self): pos = pygame.mouse.get_pos() dX = pos[0] dY = pos[1]

Also, You have a ton of redundancy issues with parenthesis; Your imports should not be on the same line unless they are from the same library and you have some unused variables. I have made some adjustments to the code, posted below. The game runs, Lines 183 - 188 run but once I reach a large size, it quickly expands the cell to the point that Python crashes.

Updated Code:

`import math import pygame import random

inp = input('Type in your name: ')

pygame.init() 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 = (1600, 850) 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 FileNotFoundError: 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)

def move(self):
    # dX, dY = pygame.mouse.get_pos()
    pos = pygame.mouse.get_pos()
    dX = pos[0]
    dY = pos[1]
    rotation = math.atan2(dY - (float(screen_height) / 2), dX - (float(screen_width) / 2)) * 180 / math.pi
    speed = 5
    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(0, 1980) self.y = random.randint(0, 1980) self.mass = 10 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, inp) spawn_cells(4000)

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)) drawText("1. G #1", (screen_width - 157, 20 + 25)) drawText("2. G #2", (screen_width - 157, 20 + 25 2)) drawText("3. ISIS", (screen_width - 157, 20 + 25 3)) drawText("4. ur mom", (screen_width - 157, 20 + 25 4)) drawText("5. w = pro team", (screen_width - 157, 20 + 25 5)) drawText("6. jumbo", (screen_width - 157, 20 + 25 6)) drawText("7. [voz]plz team", (screen_width - 157, 20 + 25 7)) drawText("8. G #3", (screen_width - 157, 20 + 25 8)) drawText("9. doge", (screen_width - 157, 20 + 25 9)) if blob.mass <= 500: drawText("10. G #4", (screen_width - 157, 20 + 25 10)) else: drawText("10. Viliami", (screen_width - 157, 20 + 25 10), (210, 0, 0))

while True: clock.tick(3000) 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()

for some reason the code below doesn't work

# start
if blob.mass >= 500:
    spawn_cells(500)
    print('Spawned')
# end
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()

`

cotilloe commented 4 years ago

Sorry for above formatting... I have uploaded the file here...

examptwo.zip