0

My code:

import pgzrun, pgzero, pygame, math, time, random, os
from pgzero.builtins import Actor, animate, keyboard
screen = pgzero.screen.Screen

# screen
WIDTH = 800
HEIGHT = 600

pygame.init()
pygame.mouse.set_visible(False)
os.chdir("c:/Users/carter.breshears/Documents/CSPVSC/Final/Images")
font = pygame.font.Font("Minecraft.ttf", 30)

# player images
playerIdle = pygame.image.load("playerIdle.png")
playerWalkImages = [pygame.image.load("playerRun1.png"), pygame.image.load("playerRun2.png"), pygame.image.load("playerRun3.png"), pygame.image.load("playerRun4.png")]

#variables
    # gameloop
gameover = False



# classes 
class Player:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.animationCount = 0
        self.movingRight = False
        self.movingLeft = False
        self.movingUp = False
        self.movingDown = False
    def main(self, screen):
        if self.animationCount + 1 >= 24:
            self.animationCount = 0
        self.animationCount += 1

        if self.movingRight or self.movingUp or self.movingDown:
            screen.blit(pygame.transform.scale(playerWalkImages[self.animationCount//6], (40,74)), (self.x, self.y))
        elif self.movingLeft:
            screen.blit(pygame.transform.scale(pygame.transform.flip(playerWalkImages[self.animationCount//6], True, False), (40,74)), (self.x, self.y))
        else:
            screen.blit(pygame.transform.scale(playerIdle, (40,74)), (self.x, self.y)) ##### This line!
        self.movingRight = False
        self.movingLeft = False
        self.movingUp = False
        self.movingDown = False
        
class PlayerBullet:
    def __init__(self, x, y, mouseX, mouseY):
        self.x = x
        self.y = y
        self.mouseX = mouseX
        self.mouseY = mouseY
        speed = 6
        self.angle = math.atan2(y - mouseY, x - mouseX)
        self.x_vel = math.cos(self.angle) * speed
        self.y_vel = math.sin(self.angle) * speed
    def main(self, screen):
        self.x -= int(self.x_vel)
        self.y -= int(self.y_vel)
        print("bang")

        pygame.draw.circle(screen, "yellow", (self.x+16, self.y+16), 5)

class InvaderEnemy:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.greenAnimationImages = [pygame.image.load("virus g.png"), pygame.image.load("virus g2.png")]
        self.yellowAnimationImages = [pygame.image.load("virus y.png"), pygame.image.load("virus y2.png")]
        self.blueAnimationImages = [pygame.image.load("virus b.png"), pygame.image.load("virus b2.png")]
        self.redAnimationImages = [pygame.image.load("virus r.png"), pygame.image.load("virus r2.png")]
        self.animationCount = 0
        self.velocity = 1
        self.lerpFactor = 0.05
        
    def main(self, screen):
        if self.animationCount + 1 == 8:
            self.animationCount = 0
        self.animationCount += 1

        spawnPos = (random.randint(-1000, 1000), random.randint(-1000, 1000))

        targetVector = pygame.math.Vector2(player.x, player.y)
        enemyVector = pygame.math.Vector2(spawnPos.x, spawnPos.y)
        newEnemyVector = pygame.math.Vector2(spawnPos.x, spawnPos.y)

        distance = enemyVector.distance_to(targetVector)
        minDistance = 25
        maxDistance = 1000

        if distance > minDistance:
            directionVector = (targetVector - enemyVector)/2
            self.minStep = max(0, distance - maxDistance)
            self.maxStep = distance - minDistance
            self.stepDistance = self.minStep + (self.maxStep - self.minStep) * self.lerpFactor
            newEnemyVector = enemyVector + directionVector * self.stepDistance

        return(newEnemyVector.x, newEnemyVector.y)

# player
player = Player(400,300)
displayScroll = [0,0]
# shooting
playerBullets = []
# invader
invaders = []
invaderEnemy = InvaderEnemy(100,100) 
invaderSpawnRate = 1

# functions
def update():
    # movement
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        displayScroll[0] -= 2
        player.movingLeft = True
        for bullet in playerBullets:
            bullet.x += 2

    if keys[pygame.K_d]:
        displayScroll[0] += 2
        player.movingRight = True
        for bullet in playerBullets:
            bullet.x -= 2

    if keys[pygame.K_w]:
        displayScroll[1] -= 2
        player.movingUp = True
        for bullet in playerBullets:
            bullet.y += 2

    if keys[pygame.K_s]:
        displayScroll[1] += 2
        player.movingDown = True
        for bullet in playerBullets:
            bullet.y -= 2

    player.main(screen)

    # shooting
    mouseX, mouseY = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                playerBullets.append(PlayerBullet(player.x, player.y, mouseX, mouseY))

    for bullet in playerBullets:
        PlayerBullet.main(screen)
    
    #enemies
    global spawnPos
    if time.perf_counter() - invaderSpawnRate > 1:
        invaders.append(InvaderEnemy(spawnPos.x, spawnPos.y))
    for invader in invaders:
        InvaderEnemy.main(screen)
    


# run
pgzrun.go()

I've tried everything I know of, Im new to this. I know that it worked before because I tested this on my home computer but whenever I downloaded the file on my school computer it stopped working so something must have changed between python versions.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Please always include the full traceback message, so that we can tell exactly what went wrong, and the path that code execution took to get to that point. You definitely have a problem with calling methods on classes rather than *instances* of those classes, but I don't think that explains the particular problem you reported. – jasonharper Dec 21 '22 at 19:56

0 Answers0