I asked a question the other day about how to implement a scrolling camera. I got a great answer which helped me achieve that... but in doing so I have lost basically all collision detection. Strangely, There are still small, invisible "platforms" that can be collided with... Above the first platform in view upon spawning (the second platform down), there is a very small area where I can get collision detection... happens near coordinates (60, 51)
. Why is collision only happening in that tiny area, and not on any of the platforms throughout the entire level?
Thanks for any help, code at github: https://github.com/tear727/Netse/blob/master/game2.py
import random
import pygame
from pygame import *
display_width = 800
display_height = 640
camera = [0,0]
size = (display_width, display_height)
screen = pygame.display.set_mode(size)
black = (0,0,0)
white = (255,255,255)
player_image = pygame.image.load("yasuo.png")
background = Surface((32, 32))
background.convert()
background.fill(Color('#783131'))
level = [
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"P P",
"P P",
"P P",
"P PPPPPPPPPPP P",
"P P",
"P P",
"P P",
"P PPPPPPPP P",
"P P",
"P PPPPPPP P",
"P PPPPPP P",
"P P",
"P PPPPPPP P",
"P P",
"P PPPPPP P",
"P P",
"P PPPPPPPPPPP P",
"P P",
"P PPPPPPPPPPP P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
]
def main():
pygame.init()
clock = pygame.time.Clock()
x = 0
y = 0
left_viewbox = display_width/2 - display_width/8
right_viewbox = display_width/2 + display_width/10
up_viewbox = display_height/2 - display_height/12
down_viewbox = display_height/2 - display_height/12
platformlst = []
players = []
player = Player(camera[0], camera[1])
map = Map(x, y)
platforms = Platform(camera[0], camera[1])
players.append(player)
pygame.display.set_caption("My game")
obstacle_img = pygame.image.load("poro.png")
def buildMap(xpos, ypos):
for i in level:
for j in i:
if j == "P":
platformlst.append(platforms)
screen.blit(platforms.image, (xpos + camera[0], ypos + camera[1]))
xpos += 64
ypos += 64
xpos = 0
def follow(shift_x, shift_y):
camera[0] += shift_x
camera[1] += shift_y
for i in players:
i.rect.x += shift_x
i.rect.y += shift_y
def viewbox():
if player.x <= left_viewbox:
view_difference = left_viewbox - player.x
player.x = left_viewbox
follow(view_difference, 0)
if player.x >= right_viewbox:
view_difference = right_viewbox - player.x
player.x = right_viewbox
follow(view_difference, 0)
if player.y <= up_viewbox:
view_difference = up_viewbox - player.y
player.y = up_viewbox
follow(0, view_difference)
if player.y >= down_viewbox:
view_difference = down_viewbox - player.y
player.y = down_viewbox
follow(0, view_difference)
def collide():
if (platforms.rect.x < player.rect.x + player.rect.w and
platforms.rect.x + platforms.rect.w > player.rect.x and
platforms.rect.y < player.rect.y + player.rect.h and
platforms.rect.h + platforms.rect.y > player.rect.y):
print "collided"
player.grounded = True
game_exit = False
while not game_exit:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
print "pressed key"
if event.key == pygame.K_LEFT:
player.left()
if event.key == pygame.K_RIGHT:
print "move right"
player.right()
if event.key == pygame.K_UP:
print "move up"
player.up()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player.stopx()
if event.key == pygame.K_UP or pygame.K_DOWN:
player.stopy()
if event.type == pygame.MOUSEBUTTONDOWN:
print "pressed mouse button"
player.boost()
if event.type == pygame.MOUSEBUTTONUP:
print "mouse button up"
player.stop()
print player.rect.x
print player.rect.y
map.buildBackground()
buildMap(x, y)
player.draw()
player.move()
collide()
viewbox()
pygame.display.update()
class Map:
def __init__(self, x, y):
self.x = x
self.y = y
def buildBackground(self):
for i in range(32):
for j in range(32):
screen.blit(background, (i*32, j*32))
class Player:
def __init__(self, x, y):
self.grounded = True
self.x = x
self.y = y
self.velx = 0
self.vely = 0
self.image = player_image
self.rect = player_image.get_rect()
def draw(self):
screen.blit(self.image, (self.x, self.y))
def move(self):
self.x += self.velx
self.y += self.vely
if not self.grounded:
self.y += 7
self.grounded = False
def right(self):
self.velx = 20
def left(self):
self.velx = -20
def up(self):
self.vely = -20
def stopx(self):
self.velx = 0
def stopy(self):
self.vely = 0
def boost(self):
self.velx += 20
self.vely += 20
def stop(self):
self.velx = 0
self.vely = 0
class Platform:
def __init__(self, x, y):
self.image = Surface((64, 64))
self.image.convert()
self.image.fill(Color('#000000'))
self.rect = Rect(x, y, 64, 64)
def draw(self):
screen.blit(self.image, (x, y))
if __name__ == "__main__":
main()