Home How to create a new class that makes the player quit?
Reply: 0

How to create a new class that makes the player quit?

AmirbekS
1#
AmirbekS Published in 2017-12-07 02:00:19Z

I'm new to coding and I have problem with a project I'm working on. I'm supposed to create a maze game where if the player touches the corner he restarts the maze. The problem is having the player win. I want to have the player touch a certain sprite and have python close. I'm using Python 3 and pygame. I used a bit of code from elsewhere too.

import pygame
pygame.init()
pygame.font.init()

# Some Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
ORANGE = ( 229, 134, 25)

#Creating font of Text
font = pygame.font.SysFont('Impact', 15, True, False)
text = font.render("Hit This Block To Win!!",True,RED)


class Wall(pygame.sprite.Sprite):
    """This class represents the bar at the bottom that the player controls """

    def __init__(self, x, y, width, height, color):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()

        # Make a wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill(color)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x


class Player(pygame.sprite.Sprite):
    """ This class represents the bar at the bottom that the
    player controls """

    # Set speed vector
    change_x = 0
    change_y = 0

    def __init__(self, x, y):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()

        # Set height, width

        self.image = pygame.Surface([15, 15])
        self.image.fill(WHITE)




        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

    def changespeed(self, x, y):
        """ Change the speed of the player. Called with a keypress. """
        self.change_x += x
        self.change_y += y

    def move(self, walls):
        """ Find a new position for the player """

        # Move left/right
        self.rect.x += self.change_x

        # Updates to see if player hit wall
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            # If there is a collision return the player to the start
            if self.change_x > 0:
                self.rect.y = 30
                self.rect.x = 30
            else:
                self.rect.y = 30
                self.rect.x = 30

        # Move up/down
        self.rect.y += self.change_y

        #Checks and see if player hit the wall
        block_hit_list = pygame.sprite.spritecollide(self, walls, False)
        for block in block_hit_list:
            #Retruns player back to start of mze
            if self.change_y > 0:
                self.rect.y = 30
                self.rect.x = 30
            else:
                self.rect.y = 30
                self.rect.x = 30


class Room(object):
    """ Base class for all rooms. """

    # Each room has a list of walls
    wall_list = None

    def __init__(self):
        """ Constructor, create our lists. """
        self.wall_list = pygame.sprite.Group()
        self.enemy_sprites = pygame.sprite.Group()
        self.win_list = pygame.sprite.Group()


class Room1(Room):
    """This creates all the walls in room 1"""
    def __init__(self):
        super().__init__()
        # Make the walls. (x_pos, y_pos, width, height, colour)

        # This is a list of walls. Each is in the form [x, y, width, height, colour]
        walls = [[0, 0, 500, 20, BLUE],#Outside Wall
                 [0, 0, 20, 500, BLUE],#Outside Wall
                 [480, 0, 20, 500, BLUE],#Outside Wall
                 [0, 480, 500, 20, BLUE],#Outside Wall
                 [20, 80, 250, 20, WHITE],
                 [350, 70, 100, 300, RED],
                 [300, 20, 20, 150, WHITE],
                 [50, 150, 270, 20, WHITE],
                 [20, 200, 350, 20, WHITE],
                 [350, 50, 20, 300, WHITE],
                 [370, 50, 90, 20, WHITE],
                 [440, 50, 20, 300, WHITE],
                 [350, 350, 110, 20, WHITE],
                 [50, 390, 430, 20, WHITE],
                 [310, 250, 20, 140, PURPLE],
                 [270, 220, 20, 140, PURPLE],
                 [230, 250, 20, 140, PURPLE],
                 [190, 220, 20, 140, PURPLE],
                 [150, 250, 20, 140, PURPLE],
                 [110, 220, 20, 140, PURPLE],
                 [70, 250, 20, 140, PURPLE],
                 [20, 220, 20, 150, PURPLE],
                 [20, 430, 430, 20, WHITE],
                ]

        # Loop through the list. Create the wall, add it to the list
        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.wall_list.add(wall)


class Win(Room):
    """This creates all the walls in room 1"""
    def __init__(self):
        super().__init__()
        # Make the walls. (x_pos, y_pos, width, height, colour)

        # This is a list of walls. Each is in the form [x, y, width, height, colour]
        walls = [[20, 420, 20, 20, GREEN],#If player hits this they win
                ]



        # Loop through the list. Create the wall, add it to the list
        for item in walls:
            wall = Wall(item[0], item[1], item[2], item[3], item[4])
            self.win_list.add(wall)


def main():
    """ Main Program """

    # Call this function so the Pygame library can initialize itself

    # Create an 800x600 sized screen
    screen = pygame.display.set_mode([500, 500])

    # Set the title of the window
    pygame.display.set_caption('The Ultimate Maze')

    # Create the player paddle object
    player = Player(50, 50)
    movingsprites = pygame.sprite.Group()
    movingsprites.add(player)

    rooms = []

    room = Room1()
    rooms.append(room)

    current_room_no = 0
    current_room = rooms[current_room_no]

    winblock = Win()
    rooms.append(winblock)

    clock = pygame.time.Clock()
    done = False

    while not done:
        # --- Event Processing ---
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    player.changespeed(-5, 0)
                if event.key == pygame.K_RIGHT:
                    player.changespeed(5, 0)
                if event.key == pygame.K_UP:
                    player.changespeed(0, -5)
                if event.key == pygame.K_DOWN:
                    player.changespeed(0, 5)

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    player.changespeed(5, 0)
                if event.key == pygame.K_RIGHT:
                    player.changespeed(-5, 0)
                if event.key == pygame.K_UP:
                    player.changespeed(0, 5)
                if event.key == pygame.K_DOWN:
                    player.changespeed(0, -5)

        # --- Game Logic ---
        player.move(current_room.wall_list)

        if Wall in current_room.wall_list:
            player.rect.x = 20
            player.rect.y = 20

        # --- Drawing ---
        screen.fill(ORANGE)
        movingsprites.draw(screen)
        current_room.wall_list.draw(screen)
        current_room.win_list.draw(screen)
        # Puts Text on Screen
        pygame.display.flip()

        clock.tick(60)

    pygame.quit()

if __name__ == "__main__":
    main()
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO