Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygame begginer game #57

Open
iAmxEz opened this issue Oct 17, 2018 · 0 comments
Open

pygame begginer game #57

iAmxEz opened this issue Oct 17, 2018 · 0 comments

Comments

@iAmxEz
Copy link

iAmxEz commented Oct 17, 2018

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()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant