I'm creating a top-down shooter game, and I cant seem to get the player vs. tiles collisions right. At this point, I've been trying to fix this for three days without community help, but at this point, I'm tired and my feeble mind is falling apart. I literally just pasted all my code below, so if you find any errors or if you have a method for tile collisions, please lemme know.
This is probably something I could figure out on my own, but if I can have some help with removing lag, I'd be grateful.
import pygame
import random
import sys
import math
pygame.init()
Credits = 0
WIDTH = 800
HEIGHT = 600
RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0,)
GREEN = (0,128,0)
GREEN2 = (24,164,86)
PURPLE = (255,25,255)
ORANGE = (255,165,0)
BROWN = (165, 42, 42)
WHITE = (255,255,255)
GREY = (211,211,211)
LIGHT_GREEN = (124,252,0)
BLACK = (0,0,0)
pygame.display.set_caption("ARMOR")
display = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()
player_walk_images = [pygame.image.load("running0.png"),pygame.image.load("running1.png"),pygame.image.load("running2.png"),pygame.image.load("running3.png"),pygame.image.load("running4.png"),pygame.image.load("running5.png"),pygame.image.load("running6.png")]
player_weapon = pygame.image.load("CartoonPistol.png")
#player_weapon.set_colorkey(())
class SlimeEnemy:
def __init__(self, x, y):
self.x = x
self.y = y
self.animation_images = [pygame.image.load("Leech0.png"),pygame.image.load("Leech1.png"),pygame.image.load("Leech2.png"),pygame.image.load("Leech3.png"),pygame.image.load("Leech4.png"),pygame.image.load("Leech5.png"),]
self.animation_count = 0
self.reset_offset = 0
self.offset_x = random.randrange(-150,150)
self.offset_y = random.randrange(-150,150)
def main(self, display):
if self.animation_count + 1 >= 24:
self.animation_count = 0
self.animation_count += 1
if self.reset_offset == 0:
self.offset_x = random.randrange(-150,150)
self.offset_y = random.randrange(-150,150)
self.reset_offset = random.randrange(120,150)
else:
self.reset_offset -= 1
if player.x + self.offset_x > self.x-display_scroll[0]:
self.x += 1
elif player.x + self.offset_x < self.x-display_scroll[0]:
self.x -= 1
elif player.y + self.offset_y > self.y-display_scroll[1]:
self.y += 1
elif player.y + self.offset_y < self.y-display_scroll[1]:
self.y -= 1
class PlayerBullet:
def __init__(self, x, y, mouse_x, mouse_y):
self.x = x
self.y = y
self.mouse_x = mouse_x
self.mouse_y = mouse_y
self.speed = 15
self.angle = math.atan2(y-mouse_y, x-mouse_x)
self.x_vel = math.cos(self.angle) * self.speed
self.y_vel = math.sin(self.angle) * self.speed
def main(self, display):
self.x -= int(self.x_vel)
self.y -= int(self.y_vel)
pygame.draw.circle(display, (YELLOW), (self.x, self.y), 5)
class Tile:
def __init__(self, x, y, texture):
self.x = x
self.y = y
self.texture = texture
def main(self, display):
display.blit(pygame.image.load(self.texture),(self.x-display_scroll[0], self.y-display_scroll[1]))
class Player:
Credits = 0
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.animation_count = 0
self.moving_right = False
self.moving_left = False
def handle_weapons(self, display):
mouse_x, mouse_y = pygame.mouse.get_pos()
rel_x, rel_y = mouse_x - player.x, mouse_y - player.y
angle = (180/math.pi) * -math.atan2(rel_y,rel_x)
player_weapon_copy = pygame.transform.rotate(player_weapon, angle)
display.blit(player_weapon_copy,(self.x+15-int(player_weapon_copy.get_width()/2),self.y+25-int(player_weapon_copy.get_height()/2)))
def main(self, display):
if self.animation_count + 1 >= 28:
self.animation_count = 0
self.animation_count += 1
if self.moving_right == True:
display.blit(pygame.transform.scale(player_walk_images[self.animation_count//4], (32, 42)), (self.x, self.y))
elif self.moving_left == True:
display.blit(pygame.transform.scale(pygame.transform.flip(player_walk_images[self.animation_count//4], True, False), (32, 42)), (self.x, self.y))
else:
display.blit(pygame.transform.scale(pygame.transform.flip(player_walk_images[0], True, False), (32, 42)), (self.x, self.y))
self.handle_weapons(display)
self.moving_right = False
self.moving_left = False
tiles = [Tile(500,300,"TestingBlock.png"),Tile(532,300,"TestingBlock.png"),Tile(564,300,"TestingBlock.png"),Tile(0,0,"xy ref.png")]
enemies = [SlimeEnemy(400,300),SlimeEnemy(432,332)]
player = Player(400, 300, 32, 32)
#dev note: a 'block' is 32 px in size
display_scroll = [0,0]
player_bullets = []
while True:
display.blit(pygame.image.load("LoginScreen.png"),(0,0))
display.fill((GREEN2))
mouse_x, mouse_y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.QUIT
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
player_bullets.append(PlayerBullet(player.x, player.y, mouse_x, mouse_y))
keys = pygame.key.get_pressed()
pygame.draw.rect(display, (WHITE), (100-display_scroll[0], 100-display_scroll[1], 16, 16))
if keys[pygame.K_a]:
display_scroll[0] -= 5
player.moving_left = True
for bullet in player_bullets:
bullet.x += 5
if keys[pygame.K_d]:
display_scroll[0] += 5
player.moving_right = True
for bullet in player_bullets:
bullet.x -= 5
if keys[pygame.K_w]:
display_scroll[1] -= 5
player.moving_left = True
for bullet in player_bullets:
bullet.y += 5
if keys[pygame.K_s]:
player.moving_right = True
for bullet in player_bullets:
bullet.y -= 5
display_scroll[1] += 5
for enemy in enemies:
enemy.main(display)
for Tile in tiles:
Tile.main(display)
for bullet in player_bullets:
bullet.main(display)
player.main(display)
clock.tick(60)
pygame.display.update()