Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

Display "You Win" when I reach a certain Point in python using Pygame

I want to know how to Display a "You Win" and a picture at the end of the game when my player reaches 2000 score in the game. I also want to randomly display a hint when the player collides with the class Reseta. Below is my current code. Please be patient with me. Thank you!

import pygame
import os
import random

pygame.init()
pygame.display.set_caption("Chimera")
SCREEN_HEIGHT = 576
SCREEN_WIDTH = 936
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

RUNNING = [pygame.transform.scale(pygame.image.load("images/Main1_side_right.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_1.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_2.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_3.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_4.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_5.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_6.png"), (64,64)),
           pygame.transform.scale(pygame.image.load("images/Main1_side_right_7.png"), (64,64))]

JUMPING = pygame.transform.scale(pygame.image.load("images/Main1_jump_right.png"), (64,64))

DUCKING = [pygame.transform.scale(pygame.image.load("images/slide3.png"), (64, 64)),
           pygame.transform.scale(pygame.image.load("images/slide3.png"), (64,64))]

TREE = [pygame.transform.scale(pygame.image.load("images/Tree_1.png"), (64,140)),
        pygame.transform.scale(pygame.image.load("images/Tree_1.png"), (64,140)),
        pygame.transform.scale(pygame.image.load("images/Tree_2.png"), (64,140))]
BOX = [pygame.transform.scale(pygame.image.load("images/box1.png"), (110,90)),
        pygame.transform.scale(pygame.image.load("images/box2.png"), (110,90)),
        pygame.transform.scale(pygame.image.load("images/box3.png"), (110,90))]

SHADOW = [pygame.transform.scale(pygame.image.load("images/Enemy_1.png"), (64,64)),
        pygame.transform.scale(pygame.image.load("images/Enemy_2.png"), (64,64)),]

PORTAL = [pygame.transform.scale(pygame.image.load("images/portal_real.png"), (64,128)),
          pygame.transform.scale(pygame.image.load("images/portal_real.png"), (64,128)),
          pygame.transform.scale(pygame.image.load("images/portal_real.png"), (64,128))]

RESETA = [pygame.transform.scale(pygame.image.load("images/reseta_real.png"), (45,120)),
          pygame.transform.scale(pygame.image.load("images/reseta_real.png"), (45,120)),
          pygame.transform.scale(pygame.image.load("images/reseta_real.png"), (45,120))]
DRUG = [pygame.transform.scale(pygame.image.load("images/Drug.png"), (45,90)),
        pygame.transform.scale(pygame.image.load("images/Drug.png"), (45,90)),
        pygame.transform.scale(pygame.image.load("images/Drug.png"), (45,90))]

STANDING = pygame.transform.scale(pygame.image.load("images/Main1_front.png"), (64,64))
BG = pygame.image.load(os.path.join("images", "Background_2.jpg"))

class Boy:
    X_POS = 80
    Y_POS = 390
    Y_POS_DUCK = 430
    JUMP_VEL = 8.5

    def __init__(self):
        self.duck_img = DUCKING
        self.run_img = RUNNING
        self.jump_img = JUMPING

        self.boy_duck = False
        self.boy_run = True
        self.boy_jump = False

        self.step_index = 0
        self.jump_vel = self.JUMP_VEL
        self.image = self.run_img[0]
        self.boy_rect = self.image.get_rect()
        self.boy_rect.x = self.X_POS
        self.boy_rect.y = self.Y_POS

    def update(self, userInput):
        if self.boy_duck:
            self.duck()
        if self.boy_run:
            self.run()
        if self.boy_jump:
            self.jump()

        if self.step_index >= 10:
            self.step_index = 0

        if userInput[pygame.K_UP] and not self.boy_jump:
            self.boy_duck = False
            self.boy_run = False
            self.boy_jump = True
        elif userInput[pygame.K_DOWN] and not self.boy_jump:
            self.boy_duck = True
            self.boy_run = False
            self.boy_jump = False
        elif not (self.boy_jump or userInput[pygame.K_DOWN]):
            self.boy_duck = False
            self.boy_run = True
            self.boy_jump = False

    def duck(self):
        self.image = self.duck_img[self.step_index // 5]
        self.boy_rect = self.image.get_rect()
        self.boy_rect.x = self.X_POS
        self.boy_rect.y = self.Y_POS_DUCK
        self.step_index += 1

    def run(self):
        self.image = self.run_img[self.step_index // 5]
        self.boy_rect = self.image.get_rect()
        self.boy_rect.x = self.X_POS
        self.boy_rect.y = self.Y_POS
        self.step_index += 1

    def jump(self):
        self.image = self.jump_img
        if self.boy_jump:
            self.boy_rect.y -= self.jump_vel * 4
            self.jump_vel -= 0.8
        if self.jump_vel < - self.JUMP_VEL:
            self.boy_jump = False
            self.jump_vel = self.JUMP_VEL

    def draw(self, SCREEN):
        SCREEN.blit(self.image, (self.boy_rect.x, self.boy_rect.y))
class Obstacle:
    def __init__(self, image, type):
        self.image = image
        self.type = type
        self.rect = self.image[self.type].get_rect()
        self.rect.x = SCREEN_WIDTH

    def update(self):
        self.rect.x -= game_speed
        if self.rect.x < -self.rect.width:
            obstacles.pop()

    def draw(self, SCREEN):
        SCREEN.blit(self.image[self.type], self.rect)


class Box(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 380


class Tree(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 325


class Shadow(Obstacle):
    def __init__(self, image):
        self.type = 0
        super().__init__(image, self.type)
        self.rect.y = 390
        self.index = 0


    def draw(self, SCREEN):
        if self.index >= 9:
            self.index = 0
        SCREEN.blit(self.image[self.index//5], self.rect)
        self.index += 1

class Drug(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 325


class Portal(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 300
class Reseta(Obstacle):
    def __init__(self, image):
        self.type = random.randint(0, 2)
        super().__init__(image, self.type)
        self.rect.y = 350
def main():
    global game_speed, x_pos_bg, y_pos_bg, points, obstacles
    run = True
    clock = pygame.time.Clock()
    player = Boy()
    # cloud = Cloud()
    game_speed = 10
    x_pos_bg = 0
    y_pos_bg = 0
    points = 0
    font = pygame.font.Font('freesansbold.ttf', 20)
obstacles = []
death_count = 0
def score():
    global points, game_speed
    points += 1
    if points % 500 == 0:
        game_speed += 1
    text = font.render("Points: " + str(points), True, (0, 0, 0))
    textRect = text.get_rect()
    textRect.center = (850, 30)
    SCREEN.blit(text, textRect)
def background():
    global x_pos_bg, y_pos_bg
    image_width = BG.get_width()
    SCREEN.blit(BG, (x_pos_bg, y_pos_bg))
    SCREEN.blit(BG, (image_width + x_pos_bg, y_pos_bg))
    if x_pos_bg <= -image_width:
        SCREEN.blit(BG, (image_width + x_pos_bg, y_pos_bg))
        x_pos_bg = 0
    x_pos_bg -= game_speed
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    SCREEN.fill((255, 255, 255))
    userInput = pygame.key.get_pressed()


    background()
    player.draw(SCREEN)
    player.update(userInput)

    if len(obstacles) == 0:
        if random.randint(0, 2) == 0:
            obstacles.append(Box(BOX))
        elif random.randint(0, 2) == 1:
            obstacles.append(Tree(TREE))
        elif random.randint(0, 2) == 2:
            obstacles.append(Shadow(SHADOW))
        elif random.randint(0, 2) == 0:
            obstacles.append(Portal(PORTAL))
        elif random.randint(0, 2) == 0:
            obstacles.append(Reseta(RESETA))
        elif random.randint(0, 2) == 0:
            obstacles.append(Drug(DRUG))

    for obstacle in obstacles:
        obstacle.draw(SCREEN)
        obstacle.update()
        if player.boy_rect.colliderect(obstacle.rect):
            pygame.time.delay(2000)
            death_count += 1
            menu(death_count)
    score()

    clock.tick(30)
    pygame.display.update()
def menu(death_count):
global points
run = True
while run:
    # SCREEN.fill((255, 255, 255))
    SCREEN.blit(BG, (0,0))
    font = pygame.font.Font('freesansbold.ttf', 30)

    if death_count == 0:
        text = font.render("Press any Key to Start", True, (250, 245, 225))
        save = font.render("Score 1000 to save the Girl", True, (250, 245, 225))
        saveRect = save.get_rect()
        saveRect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50)
        SCREEN.blit(save, saveRect)
    elif death_count > 0:
        text = font.render("Press any Key to Restart", True, (250, 245, 225))
        score = font.render("Your Score: " + str(points), True, (250, 245, 225))
        scoreRect = score.get_rect()
        scoreRect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 50)
        SCREEN.blit(score, scoreRect)
    textRect = text.get_rect()
        textRect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2)
        SCREEN.blit(text, textRect)

        SCREEN.blit(STANDING, (SCREEN_WIDTH // 2 - 20, SCREEN_HEIGHT // 2 - 140))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                main()
menu(death_count=0)

below is the file for the folder of images used in my game. https://drive.google.com/file/d/1t_kDNw3G1Q6X4KKZ9IfsCmYKpEecz9Ci/view?usp=sharing


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a very simple way that you can have a "You Win" show up in your game when the score reaches 200. You would need a variable for the score that increases every time something happens. First, you would have to load an image for the "You Win" by doing pygame.image.load(file name.png) then you would have to assign variables for the x-axis and y-axis of the image (I used You_WinX and You_WinY for the example) then you can simply do this:

if score == 2000:
     You_WinX = (location on x-axis)
     You_WinY = (location in y-axis)

What this would do is when the score reaches the value of 2000, the image of "You Win" will appear where you want it to be on the screen (x,y)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...