I know this question has been asked multiple times but I just cant wrap my head around the concept of collisions, now I know I am asking a lot but I will appreciate it greatly!, All I ask is someone to add collisions to my game and explain each step so I can try to understand whats going on,I tried youtube videos and they aren't explained very well or I just get overwhelmed which is what I feel right now, So I just want the player to stop when hitting the tree, something as simple as that! And if possible I want it to get the collision from a list for example I would have trees and other images in the list.
import pygame
import sys
import math
from pygame.locals import *
import tiles_list
pygame.init()
display_w = 800
display_h = 600
window = pygame.display.set_mode((display_w, display_h), HWSURFACE | DOUBLEBUF | RESIZABLE)
pygame.display.set_icon(pygame.image.load("game_icon.png"))
pygame.display.set_caption("Work in progress")
clock = pygame.time.Clock()
background = pygame.image.load("background.png")
class Player(object):
"""The controllable player in game"""
def __init__(self, x, y, width, height, speed):
self.x = x
self.y = y
self.width = width
self.height = height
self.image = pygame.image.load("sprite_sheet.png")
self.speed = speed
self.timer = 0
self.frames = 1
self.direction = 2
def animation(self):
x_coord = 50 * self.frames
y_coord = 50 * self.direction
self.character = self.image.subsurface(x_coord, y_coord, self.width, self.height)
self.timer += 0
if self.timer >= 10:
self.timer = 0
self.frames += 1
if self.frames >= 9:
self.frames = 1
def draw(self):
window.blit(self.character, (self.x, self.y))
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def movement(self):
self.keys = pygame.key.get_pressed()
if self.keys[pygame.K_LEFT] or self.keys[pygame.K_a]:
self.x -= self.speed
self.direction = 1
self.timer += 2
if self.keys[pygame.K_RIGHT] or self.keys[pygame.K_d]:
self.x += self.speed
self.direction = 3
self.timer += 2
if self.keys[pygame.K_UP] or self.keys[pygame.K_w]:
self.y -= self.speed
self.direction = 0
self.timer += 2
if self.keys[pygame.K_DOWN] or self.keys[pygame.K_s]:
self.y += self.speed
self.direction = 2
self.timer += 2
if self.x >= 780:
self.x = 780
self.frames = 0
if self.y >= 555:
self.y = 555
self.frames = 0
if self.x <= 0:
self.x = 0
self.frames = 0
if self.y <= 0:
self.y = 0
self.frames = 0
player = Player(400, 300, 50, 50, 4.5)
class Tree:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.image = (
pygame.transform.scale(tiles_list.tiles.subsurface(pygame.Rect(99, 147, self.width, self.height)),
(62, 82)))
def draw(self):
window.blit(self.image, (self.x, self.y))
tree = Tree(348, 300, 42, 62)
running = True
while running:
window.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == VIDEORESIZE:
screen = pygame.display.set_mode((event.w, event.h), RESIZABLE)
elif event.type == pygame.KEYUP:
if player.keys:
player.frames = 0
dist_x = math.hypot(tree.x - player.x)
dist_y = math.hypot(tree.y - player.y) # Does nothing except calculate the distence
print("distx", dist_x, "disty", dist_y)
tree.draw()
player.movement()
player.animation()
player.draw()
clock.tick(60)
pygame.display.flip()