[Pygame series] @ you, come and sign for the little Altman playing cartoon Q to play the little monster game~

preface

  Time phonograph.

It has been popular for decades since 1966.

Launched hundreds of classic works as the first special film in history.

Altman is not only the childhood and memories of generations, but also the happiness of children and the feelings of big friends!

In other words, everyone likes interesting case projects, etc., so the practice of this introductory series will be updated slowly, interspersed with updates

The same content - today we update a cartoon Q play little Altman playing little monsters game! This commemoration has been.

text

1) Which one do you like best, Altman fighting monsters?

Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing Coughing. I don't know whether to win or not 🦅, Try it yourself after writing?

2) What materials are prepared?

Protagonist on stage - monster on stageWow - it's a little cute. 🧙‍♀️🧙‍♀️

  Only a small part of the material is shown here. Other backgrounds can be modified and optimized by themselves.

3) What environments are needed?

The environment prepared by Xiaobian: Python 3 and pychar Community Edition, and then the game module Pygame needs to be installed.

 Installation: pip  install pygame

4) Code display

Main program:

import pygame
from pygame.locals import *
from random import randint
from sys import exit

class Basketball(pygame.sprite.Sprite):
    def __init__(self,basket_surface,basket_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = basket_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = basket_position
        self.speed = 8

    def update(self):
        self.rect.top -= self.speed
        if(self.rect.top<-10):
            self.kill()

class Thanos(pygame.sprite.Sprite):
    def __init__(self,thanos_surface,thanos_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = thanos_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = thanos_position
        self.speed = 2
    def update(self):
        self.rect.top += self.speed
        if self.rect.top>SCREEN_HEIGHT:
            self.kill()
            global score
            score-=10
            global missed
            missed+=1


class CXK(pygame.sprite.Sprite):
    def __init__(self,cxk_surface,cxk_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = cxk_surface
        self.rect = self.image.get_rect()
        self.rect.topleft = cxk_position
        self.speed = 5
        self.isJi = False
        self.bsks = pygame.sprite.Group()

    def move(self,offset):
        # Change the position of cxk and judge the edge
        offset_x = offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
        offset_y = offset[pygame.K_DOWN] - offset[pygame.K_UP]
        # Lateral edge judgment
        cxk_position_x = cxk.rect.left + offset_x
        if cxk_position_x < 0:
            cxk.rect.left = 0
        elif cxk_position_x > 400:
            cxk.rect.left = 400
        else:
            cxk.rect.left = cxk_position_x
        # Longitudinal edge judgment
        cxk_position_y = cxk.rect.top + offset_y
        if cxk_position_y < 150:
            cxk.rect.top = 150
        elif cxk_position_y > 700:
            cxk.rect.top = 700
        else:
            cxk.rect.top = cxk_position_y

    def singleShoot(self,bsk1_image):
        bsk1 = Basketball(bsk1_image,(self.rect.left+58,self.rect.top+45))#Control launch position
        self.bsks.add(bsk1)




SCREEN_WIDTH = 600
SCREEN_HEIGHT = 900

offset={pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0}

pygame.init()
pygame.mixer.init()
pygame.time.delay(1000)

clock = pygame.time.Clock()
screen =  pygame.display.set_mode([SCREEN_WIDTH,SCREEN_HEIGHT])
pygame.display.set_caption("Little Altman plays little mieba")#title

ticks = 0
#Load background
background=pygame.image.load('resources/images/bg.png')
#Load game end screen
gameover = pygame.image.load('resources/images/gameover.png')
#Load the picture of the protagonist cxk and set the initial position
cxk_image = pygame.image.load('resources/images/cxk.png')
cxk_position = [200,650]
#Picture of chicken changing at death and countdown to the end of the game
ji_image = pygame.image.load('resources/images/ji.png')
countdown = 0
#Instantiate the protagonist cxk
cxk = CXK(cxk_image,cxk_position)
#Load basketball picture
bsk_image = pygame.image.load('resources/images/basket.png')
#Load enemy pictures
thanos_image = pygame.image.load('resources/images/thanos.png')
thanos_group = pygame.sprite.Group()
thanos_down_group = pygame.sprite.Group()
#Set font
score = 0
pygame.font.init()
score_font = pygame.font.SysFont(None,32)
score_font.set_bold(True)
#Load shot
ji_sound = pygame.mixer.Sound('resources/audios/ji.ogg')
si_sound = pygame.mixer.Sound('resources/audios/si.ogg')
#Load background music
pygame.mixer.music.load('resources/audios/bgm.mp3')
pygame.mixer.music.play(-1)
#Set the number of anti bullying missed
missed = 0
missed_font = pygame.font.SysFont(None,32)
missed_font.set_bold(True)

while True:


    #Set frame rate
    clock.tick(90)
    #Bind background picture
    screen.blit(background,(0,0))
    #Set score panel
    ScoreFaceText = score_font.render("Score:"+str(score),True,(0,0,0))
    Scoretext = ScoreFaceText.get_rect()
    Scoretext.topleft = (10,10)
    screen.blit(ScoreFaceText,Scoretext)
    #Set miss panel
    MissedFaceText = missed_font.render("Missed:"+str(missed)+" (5 to die)",True,(0,0,0))
    Missedtext = MissedFaceText.get_rect()
    Missedtext.topleft = (10,40)
    screen.blit(MissedFaceText,Missedtext)
    #Bind character pictures
    if cxk.isJi :
        cxk.image = ji_image
        si_sound.play()
        countdown+=1
        if countdown == 40:
            pygame.mixer.music.stop()#Stop the background music after the game
            break#Exit the cycle after becoming a chicken
    else:
        cxk.image = cxk_image
    #Update basketball pictures
    cxk.bsks.update()
    cxk.bsks.draw(screen)
    #Generate mieba picture
    if ticks % 60 == 0:
        t = Thanos(thanos_image,[randint(0,SCREEN_WIDTH-thanos_image.get_width()),thanos_image.get_height()])
        thanos_group.add(t)
    #Increase the difficulty of the game according to the score
    if 200<=score<=400:
        t.speed = 3
    elif 400<score<=500:
        t.speed = 4
    elif 500<score<=1000:
        t.speed = 5
    elif score>1000:
        t.speed = 8
    thanos_group.update()
    thanos_group.draw(screen)
    #Destroy and score
    pre = len(thanos_down_group)
    thanos_down_group.add(pygame.sprite.groupcollide(thanos_group,cxk.bsks,True,True))
    if len(thanos_down_group)>pre:
        score+=10
    #Falling chicken
    thanos_down_list = pygame.sprite.spritecollide(cxk,thanos_group,True)
    if len(thanos_down_list)>0:
        thanos_down_group.add(thanos_down_list)
        cxk.isJi = True
    #When missed is greater than 5, exit the game
    if missed>=5:
        cxk.isJi = True
    screen.blit(cxk.image,cxk.rect)
    ticks+=1
    pygame.display.update()

    for event in pygame.event.get():
        #Process game exit
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
        #Detection key
        if event.type == pygame.KEYDOWN:
            if event.key in offset:
                offset[event.key] = cxk.speed
            elif event.key == pygame.K_a:
                cxk.singleShoot(bsk_image)
                ji_sound.play()
        elif event.type == pygame.KEYUP:
            if event.key in offset:
                offset[event.key] = 0

    cxk.move(offset)

#Jump out of the previous cycle and enter the game end screen
screen.blit(gameover,(0,0))
pygame.mixer.music.load('resources/audios/dead.mp3')
pygame.mixer.music.play()
while True:
    pygame.display.update()
    #Show last score
    score_font = pygame.font.SysFont(None, 72)
    ScoreFaceText = score_font.render("Final Score:" + str(score), True, (0, 0, 0))
    Scoretext = ScoreFaceText.get_rect()
    Scoretext.topleft = (130, 140)
    screen.blit(ScoreFaceText, Scoretext)
    #Exit interface
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

5) Effect display

Rules of the game:

You can get 10 points for defeating a small bully, deduct 10 points for missing a small bully, and more than 5 touched or missed by the bully will die. When the score exceeds 1000, the final boss will appear   sxc, his left and right sides will emit strip lasers.

Colored egg: your boss has been paying attention to you silently. If you keep firing at the upper left corner, you will have the opportunity to kill him and end the game directly. However, his blood is very thick. You should always pay attention to the missing number of tyrants while playing him

Running screenshot——

ending

Altman is always the immortal star in our hearts. Hey, hey, the game is finished. Let's make our own decisions. It's all free!

Didi, I can~

Tags: Python Mini Program pygame

Posted on Tue, 16 Nov 2021 21:00:50 -0500 by Thundarfoot