2

First time building a game in , let alone . When I launch the code the window with the desired BG comes up, and my Sprite loads where I want. However, the Sprite doesn't move despite all efforts. I've scoured this site and other for resolutions but to no avail.

Note: I am using an upgraded mid-2009 mac book with OS X 10.11.6 and I am launching the code via terminal (python boss_fight.py). Also, I downloaded[tag: pygame] via homebrew.

Other things I've tried:

  1. elif then if vs. if then if statements
  2. Adding print functions after if statements regarding key input to see if the input is registered, does not print.
  3. List item adding print functions after if statements regarding key input to see if input is registered, does not print.
  4. Updating
  5. Updating
  6. Launching with command pythonw boss_fight.py (this yields: ImportError: No module named pygame) which is weird because running the prompt python boss_fight.py runs the game.
  7. I've tried a few other things but can't remember them all

Here's the code:

import pygame  # load pygame keywords
import sys     # let  python use your file system
import os      # help python identify your OS

class Player(pygame.sprite.Sprite):
    
    # Spawn a player
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.movex = 0
        self.movey = 0
        self.frame = 0
        self.images = []
        img = pygame.image.load(os.path.join('images','hero.png'))
        self.images.append(img)
        self.image = self.images[0]
        self.rect  = self.image.get_rect()

    def control(self,x,y):
        
        # control player movement
        
        self.movex += x
        self.movey += y

    def update(self):
        
        # Update sprite position
        

        self.rect.x = self.rect.x + self.movex
        self.rect.y = self.rect.y + self.movey

        # moving left
        if self.movex < 0:
            self.frame += 1
            if self.frame > 3 * ani:
                self.frame = 0
            self.image = self.images[self.frame//ani]

        # moving right
        if self.movex > 0:
            self.frame += 1
            if self.frame > 3 * ani:
                self.frame = 0
            self.image = self.images[(self.frame//ani)+4]



# Setup

worldx = 960
worldy = 720

fps = 40        # frame rate
ani = 4        # animation cycles
clock = pygame.time.Clock()
pygame.init()
main = True

BLUE  = (25,25,200)
BLACK = (23,23,23 )
WHITE = (254,254,254)
ALPHA = (0,255,0)

world = pygame.display.set_mode([worldx,worldy])
backdrop = pygame.image.load(os.path.join('images','stage.jpeg')).convert()
backdropbox = world.get_rect()
player = Player()   # spawn player
player.rect.x = 0
player.rect.y = 0
player_list = pygame.sprite.Group()
player_list.add(player)
steps = 10      # how fast to move


# Main loop

while main:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()
            main = False

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.control(-steps,0)
                print('left')
            
            elif event.key == pygame.K_RIGHT: 
                player.control(steps,0)
                print('right')

            elif event.key == pygame.K_UP:
                print('up')

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.control(steps,0)

            elif event.key == pygame.K_RIGHT: 
                player.control(-steps,0)

            elif event.key == pygame.K_q:
                pygame.quit()
                sys.exit()
                main = False

    world.blit(backdrop, backdropbox)
    player.update()
    player_list.draw(world)
    pygame.display.update()
    clock.tick(fps)
10 Rep
  • 2,217
  • 7
  • 19
  • 33
Zesty
  • 21
  • 1
  • Try upgrading your PyGame version according to https://stackoverflow.com/a/61766668/51685 – this fixed things on my macOS 10.15 machine. – AKX Jun 26 '20 at 15:14
  • Beyond that, your `self.images` array only has one item, so comment out those lines changing `self.image`, or your program will also crash. – AKX Jun 26 '20 at 15:15
  • Upgrading pygame worked. Thanks! – Zesty Jul 06 '20 at 15:13

1 Answers1

0

The place where I think you've mistaken is where you're writing code for update

The way I do it is

def update(self):
        
        # Update sprite position
        

        self.rect.x = self.rect.x + self.movex
        self.rect.y = self.rect.y + self.movey

        # moving left
        if self.movex < 0:
            self.frame += 1
            if self.frame > 3 * ani:
                self.frame = 0
            self.image = self.images[self.frame//ani]

        # moving right
        elif self.movex > 0:
            self.frame += 1
            if self.frame > 3 * ani:
                self.frame = 0
            self.image = self.images[(self.frame//ani)+4]
        self.rect.topleft = self.rect.x, self.rect.y
SK Studio
  • 1
  • 1
  • Thanks for the feedback I will look over this further! However, I updated pygame to 2.0.0 and I now have control. Yay! – Zesty Jul 06 '20 at 15:13