Lee
Lee

Reputation: 1

How to make an image move using OOP

I'm new to pygame, and I'm trying to get the image (Player1) to move in OOP. I initially used procedural and it worked but when I put it into OOP format, the image no longer works.

This is the code I used when doing procedural programming, which I got by adapting code I found from https://www.pygame.org/docs/ref/key.html#comment_pygame_key_name

#Moving player
import pygame
pygame.init()

ScreenSize = 1200, 600
Screen = pygame.display.set_mode(ScreenSize)
Screen.fill([255, 255, 255])

XPos = 0
YPos = 0
Plyr = []
PlyrRct = []

Background = pygame.image.load('carpet.jpg')
Player = pygame.image.load('player.jpg')

PlayerTemp = Player.subsurface(1, 0, 64, 64)
PlayerRectTemp = PlayerTemp.get_rect()
Plyr.append(PlayerTemp)
PlyrRct.append(PlayerRectTemp)

End = False
while not End:
    Key = pygame.key.get_pressed()
    if Key[pygame.K_a]:
        XPos -= 4
    if Key[pygame.K_d]:
        XPos += 4
    if Key[pygame.K_w]:
        YPos -= 4
    if Key[pygame.K_s]:
        YPos += 4

    Background = pygame.image.load('carpet.jpg')
    Screen.blit(Background, (0, 0))

    PlyrRct[0].left = XPos
    PlyrRct[0].top = YPos
    Screen.blit(Plyr[0], PlyrRct[0])

    for event in pygame.event.get():
        if event.type==pygame.QUIT:sys.exit()
    pygame.display.flip()

This is currently my oop program

# Player Move test

import pygame
pygame.init()

class LoadImages(object):

    def __init__(self, Image, Top, Left, Width, Height):
        self.Image = pygame.image.load(Image)
        self.Top = Top
        self.Bottom = Top + Height
        self.Left = Left
        self.Right = Left + Width

class Player(LoadImages):

    def __init__(self, Image, Top, Left, Width, Height):
        LoadImages.__init__(self, Image, Top, Left, Width, Height)

    def Move(self, A, D, W, S):
        #self.CheckCollision()
        if A:
            self.Left -= 4
            self.Right -= 4
        if D:
            self.Left += 4
            self.Right += 4
        if W:
            self.Top += 4
            self.Bottom += 4
        if S:
            self.Top -= 4
            self.Bottom -= 4

    #def CheckCollision(self):
        #print ()

class Item(LoadImages):

    def __init__(self, Image, Top, Left, Width, Height):
        LoadImages.__init__(self, Image, Top, Left, Width, Height)

    #def Examine(self):
        #print ()

Player1 = Player("player.jpg", 0, 0, 300, 300)
Item1 = Item("player.jpg", 0, 0, 300, 300)
Background = pygame.image.load("carpet.jpg")

ScreenSize = 1200, 600
Screen = pygame.display.set_mode(ScreenSize)
Screen.fill([255, 255, 255])

End = False
while not End:
    Screen.blit(Background, (0, 0))
    Screen.blit(Player1.Image, (Player1.Top, Player1.Left))
    pygame.display.update()

    Key = pygame.key.get_pressed()
    Player1.Move(Key[pygame.K_a], Key[pygame.K_d], Key[pygame.K_w], Key[pygame.K_s])

I'm expecting the image (Player1) to move on the screen but it won't move anywhere. The images in the program are just being used for testing purposes and will be replaced with a .png

Upvotes: 0

Views: 173

Answers (1)

sloth
sloth

Reputation: 101072

You're missing the event processing in your second example. If you don't call pygame.event.get() (or poll()), your window won't respond (depending on your OS/window manager).

So simple add

for e in pygame.event.get():
    if e.type == pygame.QUIT:
        End = True

to your while loop.

Also, since you're using pygame, take a look at its Sprite class, which you can use for OOP with pygame. Somewhat related to your question is this one. Maybe you'll find it useful.

Upvotes: 1

Related Questions