Home How do I stop my player icon from moving when a collision is detected
Reply: 0

How do I stop my player icon from moving when a collision is detected

Paul Brown
1#
Paul Brown Published in 2018-01-12 13:54:24Z

I am trying to make a game using pygame and have come across two problems. I have to somehow make a maze out of blocks and have the player icon be stopped by that block when it interacts with it. I know how to do the blocks and how to DETECT when they interact but not how to stop the player icon from moving through it. Also, I have managed to blit the player icon to the screen and have only one on the screen at any time, but I can't figure out how to move the icon using the arrow keys. I am not asking for anyone to rewrite my code, but to just point me in the right direction.

import pygame, sys
from pygame.locals import *
from PIL import Image



pygame.init()
FPS = 60
fps_clock = pygame.time.Clock()
surface = pygame.display.set_mode((640, 480))
mouse = pygame.mouse.get_pos()
playericon = pygame.image.load("Player_Icon.png")
white = 255, 255, 255
cover = pygame.image.load("Player_Icon_cover.png")
s = 0
DISPLAY_SURF = pygame.display.set_mode((640, 720))
DISPLAY_SURF.fill(white)
coord=[]

class Blocks:
    def __init__(self, x, y):
        self.x = x
        self.y = y


    def set_position():
        x = self.x
        y = self.y

    def get_position():
        return set_position

    class Visible_Blocks:

        def image():
            block = pygame.image.load("Plain block.png")
            DISPLAY_SURF.blit(block, (self.x, self.y))

        def detect_collision():
            block_image = Image.open("Plain block.png")
            width, height = block_image.size
            if self.x >= coord[1][0] > self.x + width:
                if self.y >= coord[1][1] < self.y + height:

while True:
    pygame.display.update()
    fps_clock.tick(FPS)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEBUTTONDOWN:
            x, y = event.pos
            coord.append(event.pos)
            if s == 0:
                DISPLAY_SURF.blit(playericon, (x, y))
            else:
                DISPLAY_SURF.blit(cover, coord[-2])
                DISPLAY_SURF.blit(playericon, (x, y))
                pygame.display.flip()
            s += 1
            if s >= 2:
                coord_length = len(coord)
                for i in range(coord_length - 2):
                    del coord[i]
                    print(coord)
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30602 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO