0

making a game if you want to call it that, has no real objective yet. my issue is with Player.checkCollision.

my game runs but my player just runs in place now? I'm not really sure the issue. also in menu.handle after it displays my game menu it doesn't accept the second ESC key to exit game but i can hit enter to resume it.

also maybe some tips for condensing it overall.

import pygame, pyganim, random, sys, time
from time import sleep
from pygame.locals import *

pygame.init()
clock = pygame.time.Clock()
loaded = False
"""MainRun class """ 
class MainRun(object):
    def __init__(self):
        self.Main()
        self.loaded = False

    def Main(self):
        paused = False
        loaded = False
        game = GameDisplay()
        player = Player()
        menu = Menu()

        while True:
            while paused == True:
                menu.draw()
                for event in pygame.event.get():
                    if event.type == KEYDOWN:
                        if event.key == K_RETURN:
                            paused = False

            while paused == False:
                for event in pygame.event.get():
                    player.handle(event)
                    if event.type == KEYDOWN:
                        if event.key == K_RETURN:
                            loaded = True
                        if event.key == K_ESCAPE:
                            menu.handle(event)
                            GameDisplay.update(self,GameScene)
                            paused = True
                            clock.tick(0)
                if loaded == False:
                    game.load()
                    GameDisplay.update(self,GameScene)
                    clock.tick(20)
                if loaded == True:
                    player.checkCollision(game.rect)
                    print(player.rect,game.rect)
                    game.draw()
                    player.update()
                    GameDisplay.update(self,GameScene)
                    clock.tick(20)

"""GameScene class """                    
class GameScene(object):
    screen = pygame.display.set_mode((800,600))

"""GameDisplay class """ 
class GameDisplay(GameScene):
    def __init__(self):
       self.intscreen = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\Intro.png').convert()
       self.bg = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\World.png').convert()
       self.cabin = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\cabin.png').convert()
       self.rect = self.cabin.get_rect()
       self.rect.x = 210
       self.rect.y = 280

    def load(self):
        #draw Load Screen
        self.screen.fill((0,0,0))
        self.screen.blit(self.intscreen, (0,0))


    def draw(self):
        #draws game world
        self.screen.fill((0,0,0))
        self.screen.blit(self.bg, (0,0))
        self.screen.blit(self.cabin, ((self.rect.x,self.rect.y)))

    def update(self,GameScene):
        pygame.display.update()




"""Menu class """ 
class Menu(GameScene):
    def __init__(self):
        self.menu = self.menu = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\Menu.png')

    def draw(self):
        GameScene.screen.blit(self.menu,(0,0))
        pygame.display.update()
    def handle(self,event):
        for event in pygame.event.get():
            print (event)
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                if event.key == K_RETURN:
                    paused = False


"""Player class """    
class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.UP = 'up'
        self.DOWN = 'down'
        self.LEFT = 'left'
        self.RIGHT = 'right'
        self.front_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_front.gif').convert()
        self.back_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_back.gif').convert()
        self.left_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_left.gif').convert()
        self.right_standing = pygame.transform.flip(self.left_standing, True, False)
        self.running = self.moveUp = self.moveDown = self.moveLeft = self.moveRight = False
        self.quitdiag = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\QuitDiag.png')
        self.direction = self.DOWN # player starts off facing down (front)
        # load the "standing" sprites (these are single images, not animations)
        self.playerWidth, self.playerHeight = self.front_standing.get_size()
        # creating the PygAnimation objects for walking/running in all directions
        self.animTypes = 'back_run back_walk front_run front_walk left_run left_walk'.split()
        self.animObjs = {}
        for self.animType in self.animTypes:
            self.imagesAndDurations = [('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_%s.%s.gif' % (self.animType, str(num).rjust(3, '0')), 1) for num in range(6)]
            self.animObjs[self.animType] = pyganim.PygAnimation(self.imagesAndDurations)
        # create the right-facing sprites by copying and flipping the left-facing sprites
        self.animObjs['right_walk'] = self.animObjs['left_walk'].getCopy()
        self.animObjs['right_walk'].flip(True, False)
        self.animObjs['right_walk'].makeTransformsPermanent()
        self.animObjs['right_run'] = self.animObjs['left_run'].getCopy()
        self.animObjs['right_run'].flip(True, False)
        self.animObjs['right_run'].makeTransformsPermanent()
        # have the animation objects managed by a conductor.
        # With the conductor, we can call play() and stop() on all the animtion
        # objects at the same time, so that way they'll always be in sync with each
        # other.
        self.moveConductor = pyganim.PygConductor(self.animObjs)
        self.WALKRATE = 10
        self.RUNRATE = 18
        self.WINDOWWIDTH = 640
        self.WINDOWHEIGHT = 480
        self.rect = self.front_standing.get_rect()
        self.rect.x = 0
        self.rect.y = 0

    def checkCollision(self, sprite2):
        col = self.rect.colliderect(sprite2)
        if col == True:
            print('you collided')
            self.RUNRATE,self.WALKRATE = 0,0

    def update(self):
        if self.moveUp or self.moveDown or self.moveLeft or self.moveRight:
            # draw the correct walking/running sprite from the animation object
            self.moveConductor.play() # calling play() while the animation objects are already playing is okay; in that case play() is a no-op
            if self.running:
                if self.direction == self.UP:
                    self.animObjs['back_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
                elif self.direction == self.DOWN:
                    self.animObjs['front_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
                elif self.direction == self.LEFT:
                    self.animObjs['left_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
                elif self.direction == self.RIGHT:
                    self.animObjs['right_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
            else:
                # walking
                if self.direction == self.UP:
                    self.animObjs['back_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
                elif self.direction == self.DOWN:
                    self.animObjs['front_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
                elif self.direction == self.LEFT:
                    self.animObjs['left_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
                elif self.direction == self.RIGHT:
                    self.animObjs['right_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))

            # actually move the position of the player
            if self.running:
                rate = self.RUNRATE
            else:
                rate = self.WALKRATE
            if self.moveUp:
                self.rect.y -= rate
            if self.moveDown:
                self.rect.y += rate
            if self.moveLeft:
                self.rect.x -= rate
            if self.moveRight:
                self.rect.x += rate

        else:
                # standing still
                self.moveConductor.stop() # calling stop() while the animation objects are already stopped is okay; in that case stop() is a no-op
                if self.direction == self.UP:
                    GameScene.screen.blit(self.back_standing, ((self.rect.x,self.rect.y)))
                elif self.direction == self.DOWN:
                    GameScene.screen.blit(self.front_standing, ((self.rect.x,self.rect.y)))
                elif self.direction == self.LEFT:
                    GameScene.screen.blit(self.left_standing, ((self.rect.x,self.rect.y)))
                elif self.direction == self.RIGHT:
                    GameScene.screen.blit(self.right_standing, ((self.rect.x,self.rect.y)))

    def handle(self, event):
        if event.type == KEYDOWN:
            if event.key in (K_LSHIFT, K_RSHIFT):
                    # player has started running
                    self.running = True
            if event.key == K_UP:
                self.moveUp = True
                self.moveDown = False
                if not self.moveLeft and not self.moveRight:
                    # only change the direction to up if the player wasn't moving left/right
                    self.direction = self.UP
            elif event.key == K_DOWN:
                self.moveDown = True
                self.moveUp = False
                if not self.moveLeft and not self.moveRight:
                    self.direction = self.DOWN
            elif event.key == K_LEFT:
                self.moveLeft = True
                self.moveRight = False
                if not self.moveUp and not self.moveDown:
                    self.direction = self.LEFT
            elif event.key == K_RIGHT:
                self.moveRight = True
                self.moveLeft = False
                if not self.moveUp and not self.moveDown:
                    self.direction = self.RIGHT

        elif event.type == KEYUP:
            if event.key in (K_LSHIFT, K_RSHIFT):
                # player has stopped running
                self.running = False
            if event.key == K_UP:
                self.moveUp = False
                # if the player was moving in a sideways direction before, change the direction the player is facing.
                if self.moveLeft:
                    self.direction = self.LEFT
                if self.moveRight:
                    self.direction = self.RIGHT
            elif event.key == K_DOWN:
                self.moveDown = False
                if self.moveLeft:
                    self.direction = self.LEFT
                if self.moveRight:
                    self.direction = self.RIGHT
            elif event.key == K_LEFT:
                self.moveLeft = False
                if self.moveUp:
                    self.direction = self.UP
                if self.moveDown:
                    self.direction = self.DOWN
            elif event.key == K_RIGHT:
                self.moveRight = False
                if self.moveUp:
                    self.direction = self.UP
                if self.moveDown:
                    self.direction = self.DOWN

MainRun()
Doppel
  • 7
  • 3
  • use `print()` to check what values you have in variables and which part of code is executed (or learn how to use debuger). It helps to find problem. – furas Jan 05 '17 at 04:58
  • BTW: instead of `pygame.Rect.colliderect(sprite1, sprite2)` you can do `self.rect.colliderect(sprite2)` and then you can call it `player.checkCollision(game.rect)` without `player.rect` – furas Jan 05 '17 at 05:01
  • you create `class GameDisplay(GameScene):` so in all functions in `GameDisplay` you can use `self.screen` instead of `GameScene.screen` – furas Jan 05 '17 at 05:04
  • no idea how to use debugger – Doppel Jan 05 '17 at 05:06
  • you have wrong indentions - you check `K_ESCAPE` outside of `for event`. And outside `if event.type == KEYDOWN:` too. (in MainRun) – furas Jan 05 '17 at 05:06
  • you can't do `if event.type == KEYDOWN: ... elif event.type == KEYDOWN` because `elif event.type == KEYDOWN` will be never use because all `event.type == KEYDOWN` is catch by `if event.type == KEYDOWN` – furas Jan 05 '17 at 05:10
  • if you don't know how to use debugger then you have to use many `print(some_variable)` and `print("I'm in if event.type == KEYDOWN")`, etc. – furas Jan 05 '17 at 05:12
  • my issue isn't with that K_ESCAPE, that one works and it takes you to menu.handle and that's where the K_ESCAPE doesn't work – Doppel Jan 05 '17 at 05:14
  • got rid of the elif but still have the same problem – Doppel Jan 05 '17 at 05:18
  • use print() to see if you really are in this handler and what's goinig on with variables when you are in this handler. – furas Jan 05 '17 at 05:22
  • i threw in a print (' i have collided') in my check collision and so my issue is I'm colliding with the house in the game but I'm not actually near the house at all. you collided – Doppel Jan 05 '17 at 05:23
  • you are near house because your player is in position (0, 0, 32, 70) which is inside (0, 0, 215, 215). Your problem is because you keep player/object position in `self.x, self.y` but you have to keep in `self.rect.x, self.rect.y` And then you can use `self.rect` in every place wher you use `(self.x, self.y)` - ie. `blit(..., self.rect)` instead of `blit(..., (self.x, self.y))` – furas Jan 05 '17 at 05:30
  • BTW: and then you can even do `self.rect.right > self.WINDOWWIDTH` instead of `self.x > self.WINDOWWIDTH - self.playerWidth:` but you have to keep position in `self.rect.x, self.rect.y` and size in `self.rect.width, self.rect.height` (instead of `self.playerWidth, self.playerHeight`) – furas Jan 05 '17 at 05:34
  • BTW: you can even use `rect` to center one object on other ie. you can center text on button - `button_text.rect.center = button.rect.center` or player on screen `player.rect.center = screen.get_rect().center` – furas Jan 05 '17 at 05:40
  • well that broke my whole game now i my player doesnt move with this error. self.rect += rate TypeError: unsupported operand type(s) for +=: 'pygame.Rect' and 'int' so i have to fix that now. movement with rect is confusing to me – Doppel Jan 05 '17 at 05:40
  • what is `rate` ? are you trying do `self.rect.x += rate` or `self.rect.y += rate` ? – furas Jan 05 '17 at 05:43
  • you can use `self.rect` instead of pair `(self.x, self.y)`, not instead of one element `self.x` or `self.y` – furas Jan 05 '17 at 05:45
  • lmao. i did a mass change of self.x and self.y to self.rect and i didnt notice it changed those too, i move now but im still running in place – Doppel Jan 05 '17 at 05:50
  • you can change tuple `(self.x, self.y)` to `self.rect` but single `self.x` to `self.rect.x` and single `self.y` to `self.rect.y` – furas Jan 05 '17 at 05:52
  • so confused as to how I'm still colliding with the house. – Doppel Jan 05 '17 at 06:22
  • never mind i fixed it, i changed the colliderect to collidepoint to test that and then i made some changes and didn't change that back its all working now, well excect i get stuck in the house now when i collide with it haha – Doppel Jan 05 '17 at 06:26
  • your code uses images and pyanim which I don't have (and other people don't have too) so it can't run it to see your problem and to make modifiactions to show you how to use `self.rect`. – furas Jan 05 '17 at 06:35
  • yeah I'm aware, but I'm pretty sure I'm figuring it out thanks though you been a lot of help – Doppel Jan 05 '17 at 06:53

0 Answers0