Pgame Level Game Lipstick Challenge Game (Group)

Game Profile and Operations:

1. The game is divided into three levels, each level lasts 60 seconds.

2. The difficulty of the game increases at each level.(The wheel spins faster and faster, and the number of lipsticks increases)

3. Lipstick hits one, 10 points more.

4. The lipstick hits the repeating position and fails.

Development Tools: Pycharm

Screenshot of the game page:

Home page:

First level:

The next level shows: (Transition between levels, basically every two levels, until the end of all levels)

Second level:

Third level:

Failure Page:

Game making steps

1. Preparing materials

Collect material and fonts

2. Game Initial Settings

1. Set game screen size, game interface title

SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Game interface title
pygame.display.set_caption('Lipstick challenge')

image_dir = 'image/lipstick/'
ttf_dir = 'ttf/'

index_background = pygame.image.load(image_dir+'index_background.jpg')
index_background = pygame.transform.scale(index_background, (SCREEN_WIDTH, SCREEN_HEIGHT))

lipstick_head = pygame.image.load(image_dir+'lipstick_head.png')
lipstick_head = pygame.transform.scale(lipstick_head, (400, 100))

game_begin = pygame.image.load(image_dir+'game_begin.png')
game_begin = pygame.transform.scale(game_begin, (400, 200))

process_background = pygame.image.load(image_dir+'process_background.jpg')
process_background = pygame.transform.scale(process_background, (SCREEN_WIDTH, SCREEN_HEIGHT))

fail_image = pygame.image.load(image_dir+'fail.png')
fail_image = pygame.transform.scale(fail_image, (400, 164))

success_image = pygame.image.load(image_dir+'success.png')
success_image = pygame.transform.scale(success_image, (400, 164))

man_image = pygame.image.load(image_dir+'manImg.jpg')
man_image = pygame.transform.scale(man_image, (65, 65))



2.60 0-59, drawing circles

# 60 0-59
circle_img = pygame.image.load(image_dir+'circle.png')
circle_pos = [120, 200]
# Picture of a circle
circle_rect = pygame.Rect(0, 0, 240, 240)

# Lipstick
lipstick_img = pygame.image.load(image_dir + 'lipstick_zhijia.png')

# Next Level
next_game_img = pygame.image.load(image_dir + 'Next Level.png')

# Remaining lipstick
lipstick_remain = pygame.transform.scale(lipstick_img, (lipstick_img.get_width()//2, lipstick_img.get_height()//2))
lipstick_remain = pygame.transform.rotate(lipstick_remain, -140)


lipstick_pos = [220, 660]

lipstick_group = pygame.sprite.Group()

lipstick_hit_group =pygame.sprite.Group()



3. Game cycle frame rate setting, the higher the rotation speed, the faster the rotation

# Game Loop Frame Rate Settings
clock = pygame.time.Clock()

# The greater the speed of rotation, the faster the rotation
ROLL_SPEED = 6



4. Set the number of lipsticks, current off

# Number of lipsticks
LIPSTICK_COUNT = 6

# Current Off
CURRENT_INDEX = 1



5. Create lipsticks

class Lipstick(pygame.sprite.Sprite):
    def __init__(self, init_img, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = init_img
        self.rect = self.image.get_rect()
        self.rect.topleft = init_pos
        self.rect.top = self.rect.top-140
        self.speed = 30
        self.degrees = 0
        self.roll_speed = ROLL_SPEED
        self.is_hit = False
        self.is_rotate = True

    def move(self):
        self.rect.top -= self.speed
        if self.rect.top < circle.rect.bottom:
            self.rect.top = circle.rect.bottom-55

    def auto_move(self):
        self.degrees = self.degrees + self.roll_speed
        if self.degrees >= 360:
            self.degrees = 0
        angle_ = 360 - self.degrees  # Rotation angle
        x = CENTER[0] + (RADIUS + 35) * math.cos(math.pi * (angle_ - 90) / 180)  # A point on a circle
        y = CENTER[1] - (RADIUS + 35) * math.sin(math.pi * (angle_ - 90) / 180)  # Vertical coordinates of a point on a circle
        center = (x, y)
        self.rect = pygame.transform.rotate(self.image, angle_).get_rect()
        self.rect.center = center
        screen.blit(pygame.transform.rotate(self.image, angle_), self.rect)




6. Create a turntable class

class Circle(pygame.sprite.Sprite):
    def __init__(self, init_img, init_rect, init_pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = init_img  # picture
        self.rect = init_rect  # Initialize the rectangle where the picture is located
        self.rect.topleft = init_pos  # Initialize the upper left corner coordinates of the rectangle
        self.speed = ROLL_SPEED  # Initialization speed, here is a deterministic value
        self.is_hit = False  # Is the player hit
        self.degrees = 0

    def roll(self):
        self.degrees = self.degrees+self.speed
        if self.degrees >= 180:
            self.degrees = 0
        angle_ = 180 - self.degrees  # Rotation angle
        self.rect = pygame.transform.rotate(self.image, angle_).get_rect()
        self.rect.center = (240, 320)
        screen.blit(pygame.transform.rotate(self.image, angle_), self.rect)




7. Create Button Class

class Button(object):
    def __init__(self, image, position):
        self.image = image
        self.position = position

    def is_click(self):
        point_x, point_y = pygame.mouse.get_pos()
        x, y = self.position
        w, h = self.image.get_size()
        in_x = x < point_x < x + w
        in_y = y < point_y < y + h
        return in_x and in_y

    def render(self):
        return self.is_click()


begin_button = Button(game_begin, (55, 500))

lipstick_button = Button(lipstick_img, (220, 640))

next_game_button = Button(next_game_img, (140, SCREEN_HEIGHT//2))

is_index = True
is_begin = False



8. Define game scores, play times, and customize timing events

# Define Game Score
score = 0
# Define Game Time
game_time = 60
# Custom Timing Events
COUNT = pygame.USEREVENT + 1

CLICK = pygame.USEREVENT + 2

circle = None



9. Determine the parameters of the game's cycle exit, whether the scaffolding is generated, whether the challenge is successful

# Parameters to determine the exit of the game loop
running = True

# Whether to generate scaffolds
is_produce = False

produce_count = 0

direction = pygame.math.Vector2(0, 1)

CENTER = (240, 320)
RADIUS = 120

# Is Challenge Successful
is_success = False

is_next_game = False



10. Define next level, number and time of lipsticks

def draw_next_game():
    screen.blit(process_background, (0, 0))
    screen.blit(man_image, (50, 30))
    screen.blit(success_image, (40, 80))
    score_font = pygame.font.Font(ttf_dir + 'Kai Typography GB2312.ttf', 36)
    score_text = score_font.render('Score:' + str(score), True, (128, 128, 128))
    score_text_rect = score_text.get_rect()
    score_text_rect.topleft = [180, 320]
    screen.blit(score_text, score_text_rect)
    screen.blit(next_game_button.image, next_game_button.position)
    pygame.display.update()


# Draw the number of lipsticks
def draw_lipstick_count():
    score_font = pygame.font.Font(ttf_dir + 'Kai Typography GB2312.ttf', 25)
    score_text = score_font.render('Number:' + str(LIPSTICK_COUNT-len(lipstick_hit_group)), True, (255, 0, 0))
    score_text_rect = score_text.get_rect()
    score_text_rect.topleft = [SCREEN_WIDTH//10+50, SCREEN_HEIGHT*2//3+30]
    screen.blit(score_text, score_text_rect)
    screen.blit(lipstick_remain, [SCREEN_WIDTH//10, SCREEN_HEIGHT*2//3])


# Drawing time
def draw_time():
    time_font = pygame.font.Font(ttf_dir + 'Kai Typography GB2312.ttf', 36)
    time_text = time_font.render(str('time:') + str(game_time), True, (128, 128, 128))
    current_font = pygame.font.Font(ttf_dir + 'Kai Typography GB2312.ttf', 30)
    current_text = current_font.render(str('No.') + str(CURRENT_INDEX)+'shut', True, (0, 0, 255))
    time_text_rect = time_text.get_rect()
    time_text_rect.topleft = [190, 30]
    current_text_rect = current_text.get_rect()
    current_text_rect.topleft = [360, 32]
    screen.blit(time_text, time_text_rect)
    screen.blit(current_text, current_text_rect)



3. Set up the main game cycle

1. Control the maximum frame rate of the game, display on the first page, draw background, center coordinates, screen fill, draw background and avatar, time

while running:
    # Control Game Maximum Frame Rate 60
    clock.tick(60)
    # First page display
    if is_index:
        # Draw Background
        screen.blit(index_background, (0, 0))
        screen.blit(lipstick_head, (40, 50))
        screen.blit(game_begin, (55, 500))
        pygame.display.flip()
    # Game begins
    if is_begin:
        direction.rotate_ip(6)  # By 4 degrees.
        # Center point coordinates
        ball_pos = [int(i) for i in CENTER + direction * (RADIUS + 50)]
        # Screen Fill
        screen.fill((0, 0, 0))
        # Draw background and Avatar
        screen.blit(process_background, (0, 0))
        screen.blit(man_image, (25, 15))

        # Drawing time
        draw_time()



2. Lipstick button, set of hits

# Lipstick button
        screen.blit(lipstick_button.image, lipstick_button.position)

        # Location set hit
        degrees_list = []
        for lip in lipstick_hit_group:
            degrees_list.append(lip.degrees)



3. Stand, display lipstick list

# Bracket
        for lip in lipstick_group:
            # Bracket Movement
            lip.move()
            # # Is hit or not
            if pygame.sprite.collide_rect(lip, circle):
                # Remove Build group
                lipstick_group.remove(lip)
                lip.is_hit = True
                # Determine if the hit point is repeated
                if 0 in degrees_list:
                    running = False
                    break
                else:
                    score += 10
                # Add to hit group
                lipstick_hit_group.add(lip)

        # Show a list of lipsticks
        lipstick_group.draw(screen)

        for lip in lipstick_hit_group:
            lip.auto_move()



4. Draw scores, number of lipsticks

# Draw Score
        score_font = pygame.font.Font(None, 36)
        score_text = score_font.render(str(score), True, (128, 128, 128))
        score_text_rect = score_text.get_rect()
        score_text_rect.topleft = [100, 40]
        screen.blit(score_text, score_text_rect)

        # The number of lipsticks you draw is important here
        draw_lipstick_count()

        # circular
        if circle is None:
            circle = Circle(circle_img, circle_rect, circle_pos)



5. Change the picture index to make it animated

circle.roll()

        roll_frequency += 2
        if roll_frequency >= 60:
            roll_frequency = 0
        pygame.display.update()



6. Detect KEYDOWN events: KEYDOWN is a constant defined in pygame.locals, the pygame.locals file has been imported since

(If Esc is pressed, the main loop terminates

Detect QUIT: If QUIT, terminate main loop

Send custom events every 1 second

Reset Time

Setting speed is 1.5 times faster than before)

    for event in pygame.event.get():
        # Detect KEYDOWN events: KEYDOWN is a constant defined in pygame.locals, the pygame.locals file has been imported since the beginning
        if event.type == pygame.KEYDOWN:
            # If Esc is pressed, the main loop terminates
            if event.key == pygame.K_ESCAPE:
                running = False
        # Detect QUIT: If QUIT, terminate main loop
        elif event.type == pygame.QUIT:
            running = False
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN and is_index:
            point_x, point_y = pygame.mouse.get_pos()
            is_begin = begin_button.render()
            if is_begin:
                is_index = False
                # Send custom events every 1 second
                pygame.time.set_timer(COUNT, 1000)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if lipstick_button.render() and is_begin:
                lipstick = Lipstick(lipstick_img, lipstick_pos)
                lipstick_group.add(lipstick)
            if is_next_game and next_game_button.render():
                is_begin = True
                # Reset Time
                game_time = 60
                # Setting speed is 1.5 times faster than before
                ROLL_SPEED = ROLL_SPEED * 1.5
                CURRENT_INDEX += 1
                LIPSTICK_COUNT += 2
                circle = None

        elif event.type == COUNT:
            if game_time > 0:
                game_time -= 1



7. When the game is over, decide if the challenge is successful or if the number of lipsticks is all clicked.

    if game_time <= 0:
        # Time to end of game
        running = False

    # Determine whether the challenge was successful
    if CURRENT_INDEX == 3 and len(lipstick_hit_group) == LIPSTICK_COUNT:
        # A little pause
        time.sleep(0.3)
        is_success = True
        running = False

    # Determine if the total number of lipsticks is clicked
    if len(lipstick_hit_group) == LIPSTICK_COUNT:
        # A little pause
        time.sleep(0.3)
        is_begin = False
        is_next_game = True
        draw_next_game()
        for lip in lipstick_hit_group:
            lipstick_hit_group.remove(lip)

print('-----over------')
# screen.fill((0, 0, 0))
screen.blit(process_background, (0, 0))
screen.blit(man_image, (50, 30))

if is_success:
    screen.blit(success_image, (40, 80))
else:
    screen.blit(fail_image, (40, 80))



8. Draw score, show score and handle exit

score_font = pygame.font.Font(ttf_dir+'Kai Typography GB2312.ttf', 36)
score_text = score_font.render('Score:'+str(score), True, (128, 128, 128))
score_text_rect = score_text.get_rect()
score_text_rect.topleft = [180, 320]
screen.blit(score_text, score_text_rect)

# Show score and handle game exit
while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
    pygame.display.update()



Tags: Pycharm

Posted on Mon, 06 Apr 2020 09:22:35 -0700 by Pie