Pygame game wolf eat sheep

1, Technical points:

Animation Sprite, full screen mode, collision detection, sound effect playback, boundary judgment

Game Description:

1. There are 10 lambs on the screen with random speed.

2. Keyboard up and down to control the movement of the wolf, when the wolf met the lamb, the lamb disappeared.

The code is as follows:

# -*- coding: utf-8 -*-d
import pygame
import sys
from random import *


# Lambs generate lambs
class Lamb(pygame.sprite.Sprite):
    def __init__(self, image, position, speed, bg_size):
        # Initialize animation Sprite
        pygame.sprite.Sprite.__init__(self)
        # Load the transferred picture
        self.image = pygame.image.load(image)
        self.rect = self.image.get_rect()
        # Define random locations
        self.rect.left, self.rect.top = position
        # Speed of motion
        self.speed = speed
        # Screen size
        self.width, self.height = bg_size[0], bg_size[1]
        # Generate radius, detect collisions
        self.radius = self.rect.width / 2

    # Moving boundary judgment
    def move(self):
        self.rect = self.rect.move(self.speed)

        if self.rect.right < 0:
            self.rect.left = self.width

        elif self.rect.left > self.width:
            self.rect.right = 0

        elif self.rect.bottom < 0:
            self.rect.top = self.height

        elif self.rect.top > self.height:
            self.rect.bottom = 0


def main():
    pygame.init()
    lamb_image = "Lamb.png"
    lang_image = "lang.png"
    BG_image = "BG1366.png"
    BG_image1 = "BG1920.png"
    end_image = "end1.png"
    # I want to add words, but I can't get them out. Frame is shown
    pygame.font.init()
    font = pygame.font.Font(None, 30)
    str1 = "Sitting alone in the room, plucking the strings, this is the song I wrote for you. The song ends, the time stops.\n Looking out of the window, the light is brilliant, and the empty and silent room is only the tick of the watch needle.\
            //Time seems to be at a standstill at this moment, the thoughts are disordered, and your appearance in your mind is gradually blurred. Has the movie ended like this..... The simplest melody, the most simple lyrics, the most sincere love“
    num = 0
    line_h = font.get_linesize()
    # Load and play background music
    pygame.mixer_music.load("bg.ogg")
    pygame.mixer_music.set_volume(0.2)
    pygame.mixer_music.play()
    # Loading sheep calls
    lamb_music = pygame.mixer.Sound("lamb.wav")
    lamb_music.set_volume(0.2)
    # Load end music
    end_music = pygame.mixer.Sound("end.wav")
    end_music.set_volume(0.2)

    bigf11 = True
    running = True
    oldbg_size = width, height = 800, 600
    newbg_size = oldbg_size
    screen = pygame.display.set_mode(oldbg_size)
    pygame.display.set_caption("Wolf in love with sheep")

    # Generate the wolf object. The initial speed is 0, 60 and 78 are the width and height of the sample
    speed = [0, 0]
    position = randint(0, width - 60), randint(0, height - 78)
    lang = Lamb(lang_image, position, speed, oldbg_size)

    # Generate 5 lamb objects and add them to the list lambs
    lambs = []
    # Generate a new group for collision detection
    group = pygame.sprite.Group()
    for i in range(10):
        # Randomly generated positions 85 and 100 are the width and height of the sample
        speed = [randint(-4, 4), randint(-4, 4)]
        position = randint(0, width - 85), randint(0, height - 100)
        lamb = Lamb(lamb_image, position, speed, oldbg_size)
        # If the lamb meets the wolf at birth, redefine the position of the wolf
        while pygame.sprite.spritecollide(lang, group, False, pygame.sprite.collide_circle):
            lang.rect.left, lang.rect.top = randint(0, width - 60), randint(0, height - 78)
        lambs.append(lamb)
        group.add(lamb)

    # Background map
    background = pygame.image.load(BG_image)
    # End graph
    endimg = pygame.image.load(end_image)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                # Press F11 to turn on the full screen
                if event.key == pygame.K_F11:
                    bigf11 = not bigf11
                    # Get computer resolution
                    newsize = width, height = pygame.display.list_modes()[1][0], pygame.display.list_modes()[1][1]
                    if bigf11:
                        screen = pygame.display.set_mode(newsize)
                        newbg_size = [width, height]
                        # Change the screen size of the lamb
                        for each in lambs:
                            each.width, each.height = newbg_size[0], newbg_size[1]
                        # Change the screen size of wolf
                        lang.width, lang.height = newbg_size[0], newbg_size[1]
                        if width == 1920:
                            background = pygame.image.load(BG_image1)
                    else:
                        screen = pygame.display.set_mode(oldbg_size)
                        # Change the screen size of the lamb
                        for each in lambs:
                            each.width, each.height = oldbg_size[0], oldbg_size[1]
                        # Change the screen size of wolf
                        lang.width, lang.height = oldbg_size[0], oldbg_size[1]

                # Coyote movement
                if event.key == pygame.K_UP:
                    lang.speed = [0, -4]
                    lang.move()
                if event.key == pygame.K_DOWN:
                    lang.speed = [0, 4]
                    lang.move()
                if event.key == pygame.K_RIGHT:
                    lang.speed = [4, 0]
                    lang.move()
                if event.key == pygame.K_LEFT:
                    lang.speed = [-4, 0]
                    lang.move()

        # Fill background
        screen.blit(background, (0, 0))
        # Show the lamb
        for each in group:
            each.move()
            screen.blit(each.image, each.rect)
        # Show the wolf
        screen.blit(lang.image, lang.rect)
        # Collision detection, if the wolf and the lamb collide, delete the lamb
        if pygame.sprite.spritecollide(lang, group, True, pygame.sprite.collide_circle):
            lamb_music.play()
            if len(group) <= 0:
                end_music.play()

        # game over
        if len(group) <= 0:
            screen.blit(background, (0, 0))
            screen.blit(endimg, (0, 0))
            # screen.blit(font.render(str1, True, (0, 0, 0)), (0, 0))
        # Update page
        pygame.display.flip()
        # Set delay
        pygame.time.delay(10)


if __name__ == '__main__':
    main()

The effect is as follows:

Material link:

Link: https://pan.baidu.com/s/1jTyx1Lz_I9W6UilByeQLXg
Extraction code: ucs6

At the end of the original want to display text, that font can not get out, next time learn to get again.


There is always one on the way to study and keep fit

Tags: Python

Posted on Sat, 09 May 2020 07:13:13 -0700 by digitalecartoons