Skip to content

pygame begginer game #57

@iAmxEz

Description

@iAmxEz

on hit it takes wrong ants position
Here's the code:

import pygame
pygame.init()
clock = pygame.time.Clock()
win = pygame.display.set_mode((1000, 500))
y = 250

class Ant:
def init(self, x, side, colour):
self.x = x
self.width = 20
self.side = side
self.left = False
self.right = True
self.vel = 5
self.counter1 = 0
self.colour = colour
self.dir = dir
self.moving = True
self.counter = 0

def start(self):
    if self.side == "Left":
        self.right = True
    else:
        self.left = True
    self.counter1 = 1

def side1(self):
    if self.x < 500:
        return "left"
    else:
        return "right"

def site(self):
    if self.left:
        return "left"
    else:
        return "right"

def move(self):
    if self.moving:
        if self.counter1 == 0:
            self.start()
        if self.left:
            self.right = False
            self.x -= self.vel
        else:
            self.left = False
            self.x += self.vel

def draw(self, win):
    self.move()
    pygame.draw.circle(win, self.colour, (self.x, 250), 10)

def wall(self):
    if self.x + self.width >= 1010:
        self.moving = False
    elif self.x <= 10:
        self.moving = False

def updateWin(win):
win.blit(pygame.display.set_mode((1000, 500)), (0, 0))
text(ant1, ant2, ant3)
ant1.draw(win)
ant2.draw(win)
ant3.draw(win)
pygame.draw.rect(win, (139, 69, 19), (0, 260, 1000, 10))
pygame.display.update()

def hit(ants):
for ant4 in ants:
if ant4.moving:
for ant5 in ants:
if ant4.x < ant5.x:
if ant4.x + ant4.width >= ant5.x:
ant4.right = False
ant4.left = True
ant5.left = False
ant5.right = True
ant4.counter += 1
ant5.counter += 1

def text(ant1, ant2, ant3):
font1 = pygame.font.SysFont("comicsans", 50)
text1 = font1.render(str(ant1.counter), 1, (255, 255, 255))
text2 = font1.render(str(ant2.counter), 1, (255, 255, 255))
text2 = font1.render(str(ant3.counter), 1, (255, 255, 255))
win.blit(text1, (ant1.x, 200))
win.blit(text2, (ant2.x, 200))
win.blit(text1, (ant3.x, 200))

ant1 = Ant(40, "Left", (255, 0, 0))
ant2 = Ant(960, "Right", (100, 255, 255))
ant3 = Ant(200, "Left", (0, 200, 0))
run = True
while run:
clock.tick(15)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
ants = (ant1, ant2, ant3)
hit(ants)
ant1.site(); ant1.wall()
ant2.site(); ant2.wall()
ant3.site(); ant3.wall()
updateWin(win)
pygame.quit()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions