Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with NPC (bot)
#1
I created a simple game but I would like to make the NPC (bots) be able to jump at random times. Here is my code:

import pygame
import random
import time

pygame.init()


win = pygame.display.set_mode((750,480))
pygame.display.set_caption("Bird Hunter")


bg = pygame.image.load('bg.jpg')

clock = pygame.time.Clock()

walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
char = pygame.image.load('standing.png')


class player(object):
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.vel = 4
        self.isJump = False
        self.left = False
        self.right = False
        self.walkCount = 0
        self.jumpCount = 10
        self.standing = True

    def draw(self, win):
        if self.walkCount + 1 >= 27:
            self.walkCount = 0

        if not(self.standing):
            if self.left:
                win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
                self.walkCount += 1
            elif self.right:
                win.blit(walkRight[self.walkCount//3], (self.x,self.y))
                self.walkCount +=1
        else:
            if self.right:
                win.blit(walkRight[0], (self.x, self.y))
            else:
                win.blit(walkLeft[0], (self.x, self.y))
                

class projectile(object):
    def __init__(self,x,y,radius,color,facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing
        self.vel = 8 * facing

    def draw(self,win):
        pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)


class alien(object):
    walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
    walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.walkCount = 0
        self.jumpCount = 10
        self.isJump = False
        self.vel = 3

    def draw(self, win):
        self.move()
        if self.walkCount + 1 >= 33:
            self.walkCount = 0
        
        if self.vel > 0:
            win.blit(self.walkRight[self.walkCount//3], (self.x,self.y))
            self.walkCount += 1
        else:
            win.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
            self.walkCount += 1
            
    def move(self):
        ran_num = random.randint(1, 10)
        if self.vel > 0:
            if self.x < self.path[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0
        

class skeleton1(object):
    walkRightS = [pygame.image.load('R1S.png'), pygame.image.load('R2S.png'), pygame.image.load('R3S.png'), pygame.image.load('R4S.png'), pygame.image.load('R5S.png'), pygame.image.load('R6S.png'), pygame.image.load('R7S.png'), pygame.image.load('R8S.png'), pygame.image.load('R9S.png')]
    walkLeftS = [pygame.image.load('L1S.png'), pygame.image.load('L2S.png'), pygame.image.load('L3S.png'), pygame.image.load('L4S.png'), pygame.image.load('L5S.png'), pygame.image.load('L6S.png'), pygame.image.load('L7S.png'), pygame.image.load('L8S.png'), pygame.image.load('L9S.png')]

    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.walkCount = 0
        self.vel = 3

    def draw(self, win):
        self.move()
        if self.walkCount + 1 >= 27:
            self.walkCount = 0
        
        if self.vel > 0:
            win.blit(self.walkRightS[self.walkCount // 3], (self.x, self.y))
            self.walkCount += 1
        else:
            win.blit(self.walkLeftS[self.walkCount // 3], (self.x,self.y))
            self.walkCount += 1
            
    def move(self):
        if self.vel > 0:
            if self.x < self.path[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.walkCount = 0


class bird1(object):
    fly_right = [pygame.image.load('B1_R1.png'), pygame.image.load('B1_R3.png'), pygame.image.load('B1_R4.png'), pygame.image.load('B1_R5.png'), pygame.image.load('B1_R6.png'), pygame.image.load('B1_R7.png')]
    fly_left = [pygame.image.load('B1_L1.png'), pygame.image.load('B1_L2.png'), pygame.image.load('B1_L3.png'), pygame.image.load('B1_L4.png'), pygame.image.load('B1_L5.png'), pygame.image.load('B1_L6.png'), pygame.image.load('B1_L7.png')]
    
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.flyCount = 0
        self.vel = 3

    def draw(self, win):
        self.fly()
        if self.flyCount + 1 >= 18:
            self.flyCount = 0

        if self.vel > 0:
            win.blit(self.fly_right[self.flyCount // 3], (self.x, self.y))
            self.flyCount += 1
        else:
            win.blit(self.fly_left[self.flyCount // 3], (self.x, self.y))
            self.flyCount += 1

    def fly(self):
        if self.vel > 0:
            if self.x < self.path[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.flyCount = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.flyCount = 0

class bird2(object):
    fly_right = [pygame.image.load('B1_R1.png'), pygame.image.load('B1_R3.png'), pygame.image.load('B1_R4.png'), pygame.image.load('B1_R5.png'), pygame.image.load('B1_R6.png'), pygame.image.load('B1_R7.png')]
    fly_left = [pygame.image.load('B1_L1.png'), pygame.image.load('B1_L2.png'), pygame.image.load('B1_L3.png'), pygame.image.load('B1_L4.png'), pygame.image.load('B1_L5.png'), pygame.image.load('B1_L6.png'), pygame.image.load('B1_L7.png')]
    
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.flyCount = 0
        self.vel = 5

    def draw(self, win):
        self.fly()
        if self.flyCount + 1 >= 18:
            self.flyCount = 0

        if self.vel > 0:
            win.blit(self.fly_right[self.flyCount // 3], (self.x, self.y))
            self.flyCount += 1
        else:
            win.blit(self.fly_left[self.flyCount // 3], (self.x, self.y))
            self.flyCount += 1

    def fly(self):
        if self.vel > 0:
            if self.x < self.path[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.flyCount = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.flyCount = 0

class bird3(object):
    fly_right = [pygame.image.load('B2_R1.png'),  pygame.image.load('B2_R2.png'), pygame.image.load('B2_R3.png'), pygame.image.load('B2_R4.png'), pygame.image.load('B2_R5.png'), pygame.image.load('B2_R6.png'), pygame.image.load('B2_R7.png')]
    fly_left = [pygame.image.load('B2_L1.png'), pygame.image.load('B2_L2.png'), pygame.image.load('B2_L3.png'), pygame.image.load('B2_L4.png'), pygame.image.load('B2_L5.png'), pygame.image.load('B2_L6.png'), pygame.image.load('B2_L7.png')]
    
    def __init__(self, x, y, width, height, end):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.path = [x, end]
        self.flyCount = 0
        self.vel = 7

    def draw(self, win):
        self.fly()
        if self.flyCount + 1 >= 21:
            self.flyCount = 0

        if self.vel > 0:
            win.blit(self.fly_right[self.flyCount // 3], (self.x, self.y))
            self.flyCount += 1
        else:
            win.blit(self.fly_left[self.flyCount // 3], (self.x, self.y))
            self.flyCount += 1

    def fly(self):
        if self.vel > 0:
            if self.x < self.path[1] + self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.flyCount = 0
        else:
            if self.x > self.path[0] - self.vel:
                self.x += self.vel
            else:
                self.vel = self.vel * -1
                self.x += self.vel
                self.flyCount = 0


def redrawGameWindow():
    win.blit(bg, (0,0))
    man.draw(win)
    alien.draw(win)
    skeleton1.draw(win)
    bird1.draw(win)
    bird2.draw(win)
    bird3.draw(win)
    for bullet in bullets:
        bullet.draw(win)
    
    pygame.display.update()
    


man = player(200, 410, 64,64)
alien = alien(100, 410, 64, 64, 700)
skeleton1 = skeleton1(50, 410, 64, 64, 700)
bird1 = bird1(25, 220, 120, 120, 730)
bird2 = bird2(25, 150, 120, 120, 730)
bird3 = bird3(25, 50, 200, 200, 730)
bullets = []


run = True
while run:
    clock.tick(27)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
    for bullet in bullets:
        if bullet.x < 700 and bullet.x > 0:
            bullet.x += bullet.vel
        else:
            bullets.pop(bullets.index(bullet))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        if man.left:
            facing = -1
        else:
            facing = 1
            
        if len(bullets) < 5:
            bullets.append(projectile(round(man.x + man.width //2), round(man.y + man.height//2), 6, (0,0,0), facing))

    if keys[pygame.K_LEFT] and man.x > man.vel:
        man.x -= man.vel
        man.left = True
        man.right = False
        man.standing = False
    elif keys[pygame.K_RIGHT] and man.x < 750 - man.width - man.vel:
        man.x += man.vel
        man.right = True
        man.left = False
        man.standing = False
    else:
        man.standing = True
        man.walkCount = 0
        
    if not(man.isJump):
        if keys[pygame.K_UP]:
            man.isJump = True
            man.right = False
            man.left = False
            man.walkCount = 0
    else:
        if man.jumpCount >= -10:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.7 * neg
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 10
            
    redrawGameWindow()


pygame.quit()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020