0
import pygame, sys
from pygame.locals import *



#Game mechanics that dosent fit into a class


global collide
collide = False
def checkCollision(players, blocks):
    for player in players:
        ox = player.x
        oy = player.y
        for block in blocks:
            if player.rect.colliderect(block.rect):
                print "collide"
                player.movePlayer(ox, oy)
                collide = True
# The class that controls the game      
class Game:
    def __init__(self):
        self.earth = World("Earth", 0.3)
        self.PlayerEntities = [Player(10,10)]


    #Method that starts the game    
    def init_game(self):
        jumpc = 0
        global screen
        pygame.init()
        screen = pygame.display.set_mode((800,500))
        pygame.display.set_caption(self.earth.returnName())
        moveDown = False
        moveUp = False
        moveLeft = False
        moveRight = False
        tickCounter = 0
        while True:
            screen.fill((200,200,255))
            self.earth.drawWorld()
            #print tickCounter
            #print self.PlayerEntities[0].x, self.PlayerEntities[0].y
            checkCollision(self.PlayerEntities, self.earth.returnBlocks())
            for player in self.PlayerEntities:
                player.drawPlayer()

            #Game mechanic checkers go here:
            for players in self.PlayerEntities:
                self.earth.gravityMove(players, moveUp)
            #Movement has to go here for it to be smooth, sadly.

            #Keypress
            for event in pygame.event.get():
                #Pushing the key
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.KEYDOWN:
                    if event.key == K_DOWN:
                        moveDown = True
                    if event.key == K_UP:
                        moveUp = True
                        jumpc = tickCounter
                    if event.key == K_LEFT:
                        moveLeft = True
                    if event.key == K_RIGHT:
                        moveRight = True

                #Releasing the key
                if event.type == pygame.KEYUP:
                    if event.key == K_DOWN:
                        moveDown = False
                    if event.key == K_UP:
                        moveUp = False
                    if event.key == K_LEFT:
                        moveLeft = False
                    if event.key == K_RIGHT:
                        moveRight = False                       
            #Moving
            if moveDown == True:
                self.PlayerEntities[0].movePlayer(0,0.5)
            if moveUp == True:
                self.PlayerEntities[0].movePlayer(0,-0.5)
            if tickCounter == jumpc + 100:
                    moveUp = False
            if moveLeft == True:
                self.PlayerEntities[0].movePlayer(-0.3, 0)
            if moveRight == True:
                self.PlayerEntities[0].movePlayer(0.3, 0)


            pygame.display.flip()
            tickCounter += 1
    def returnScreen(self):
        return screen

# The player class
class Player:
    # Initializing the position and the looks of the player class.
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.rect = pygame.Rect((self.x, self.y), (self.x+10, self.y+20))

    def drawPlayer(self):

        pygame.draw.rect(screen, (255,0,0), (self.x,self.y,10,20),1)
    def movePlayer(self, nx, ny):

        self.x = self.x + nx
        self.y = self.y + ny
        self.rect.move_ip(self.x, self.y)

# For diffrent textures
class Textures:
    def __init__(self):
        self.BlockTextures = {"DIRT":(0,255,0)}

    def returnTexture(self, type):
        return self.BlockTextures["DIRT"]



#World class
class World(Game):
    def __init__(self, name, gravity):
        self.blocks = [Block("DIRT",10, 50),Block("DIRT",20, 50),Block("DIRT",50, 50)]
        self.name = name
        self.maxGravity = gravity
        self.gravmodify = gravity

    def returnName(self):
        return self.name

    def drawWorld(self):
        for block in self.blocks:
            block.drawBlock()

    def returnBlocks(self):
        return self.blocks

    def gravityMove(self, player, moveUp):
        if collide == False:
            if moveUp == False:
                if player.y < 500:
                    player.movePlayer(0, self.maxGravity-self.gravmodify)
                    self.gravmodify = self.gravmodify - 0.004
            if moveUp == True:
                self.gravmodify = self.maxGravity

# block class
class Block(World):
    def __init__(self, type, x, y):
        self.x = x
        self.type = type
        self.y = y
        self.rect = pygame.Rect((self.x, self.y), (self.x+10, self.y+10))
    def drawBlock(self):
        pygame.draw.rect(screen, Texture.returnTexture(self.type),(self.x,self.y,10,10), 1)



# Debugging and testing for the API
Texture = Textures()
mainGame = Game()
mainGame.init_game()

I have tried 3 diffrent ways to make collisions for this game. And neither of them would work. This time it just keeps falling through the blocks, and even tho it print "Collision" it dosent stop as it should.

Help would be appericated, thanks.

Håvard Nygård
  • 386
  • 3
  • 14
  • Why the use of `global` outside of a function? It is entirely superfluous. – Jason Apr 05 '16 at 18:50
  • I made it outside so it wouldnt reset everytime the main loop went by. It was supposed to turn of gravity to check if that was the problem. – Håvard Nygård Apr 05 '16 at 18:53
  • 1
    That doesn't matter. Read [this](http://stackoverflow.com/questions/423379/using-global-variables-in-a-function-other-than-the-one-that-created-them). There is **no** reason to use the `global` keyword **outside** of a function. – Jason Apr 05 '16 at 18:55
  • Thanks, still dosent fix the main problem tho. – Håvard Nygård Apr 05 '16 at 18:57

0 Answers0