I am having trouble with my player having horizontal collision. I have commented out the code that I have tried and have looked at tutorials from DaFluffyPotato and KidsCanCode but I either the character would hardly move or he would fall infinitely.
def collide(self):
self.player.rect.x = self.player.pos.x
for each in self.map.blocks:
if self.player.rect.colliderect(each):
# Landing Collision
if self.player.vel.y > 0:
if self.player.pos.y < each.bottom:
self.player.pos.y = each.top + 1
self.player.vel.y = 0
# if self.player.rect.x < 0:
# self.player.rect.right = each.left
# self.player.vel.x = 0
# # # self.player.rect.top = each.bottom
# elif self.player.rect.x > 0:
# self.player.rect.right = each.left
# self.player.vel.x = 0
# Jump
self.player.able_jump = True
jump = pygame.key.get_pressed()
if jump[pygame.K_SPACE]:
self.player.vel.y = -self.settings.jump_height
else:
self.player.able_jump = False
Player Movement Code
def update(self):
"""Controls the player's position."""
self.acc = vec(0,0.5)
moving = pygame.key.get_pressed()
if moving[pygame.K_LEFT]:
self.acc.x = -self.settings.acceleration
if moving[pygame.K_RIGHT]:
self.acc.x = self.settings.acceleration
self.acc.x += self.vel.x * self.settings.friction
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
if self.pos.x > self.settings.screen_width:
self.pos.x = 0
if self.pos.x < 0:
self.pos.x = self.settings.screen_width
if self.pos.y > (self.settings.screen_height + 400):
self.pos.y = 0
self.rect.midbottom = self.pos
self.able_jump = False
Map Code(I know there is a Class Indention)
import csv
import pygame
from pygame.sprite import Sprite
class Map(Sprite):
def __init__(self, game):
super().__init__()
self.screen = game.screen
self.settings = game.settings
mapname = 'practice\map.csv'
infinitemapname = 'practice\infinitemap.csv'
self.full_map = []
with open(mapname) as f:
reader = csv.reader(f,delimiter=',')
for row in reader:
self.full_map.append(list(row))
self.dirt_block = pygame.image.load('block.png')
def draw(self):
"""This method draws the map."""
self.blocks = []
for y, line in enumerate(self.full_map):
for x, c in enumerate(line):
if c == "0":
center = ((x*10)+4, (y*10)+4)
self.blocks.append(self.dirt_block.get_rect(center=center))
self.screen.blit(self.dirt_block, (x * 10-self.settings.scroll[0], y * 10-self.settings.scroll[1]))