欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于pygame的小游戏

程序员文章站 2022-04-07 20:16:16
...

一.pygame的安装

pip install pygame

pip主要用法

二.球球游戏代码


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


# 创建关于球的类
class Ball(pygame.sprite.Sprite):
    def __init__(self, image, position, speed, bg_size):
        # 继承父类属性
        pygame.sprite.Sprite.__init__(self)
        # 外观,在屏幕位置,运动速度
        self.image = pygame.image.load(image).convert_alpha()
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = position
        self.speed = speed
        self.width, self.height = bg_size[0], bg_size[1]
        # 圆半径,调用pygame.sprite.collide_circle函数所需参数
        self.radius = self.rect.width / 2
    # 小球移动
    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.top >self.height:
            self.rect.bottom =0

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

def main():
    pygame.init()

    #添加游戏音乐
    pygame.mixer.music.load("music/背景音乐.mp3")
    pygame.mixer.music.play()
    losing = pygame.mixer.Sound("music/3.wav")
    win = pygame.mixer.Sound("music/35.wav")
    #循环播放背景音乐
    circulation = USEREVENT
    pygame.mixer.music.set_endevent(circulation)


    # 背景和球外观图片位置
    ball_image = "photo/ball.png"
    bg_image = "photo/background.png"
    face_image = "photo/face.png"
    a_image = "photo/331.png"
    speed_image = "photo/123.png"
    #设置屏幕窗口大小,背景,标题
    bg_size = width,height = 1080,724
    screen = pygame.display.set_mode(bg_size)
    background = pygame.image.load(bg_image).convert_alpha()
    pygame.display.set_caption("球球大作战")
    #小球出生位置和初始化,且建立一个列表存储产生的球对象group,为调用pygame.sprite.spritecollide(碰撞函数)所需组
    group = pygame.sprite.Group()
    group_1 = pygame.sprite.Group()
    group_2 =pygame.sprite.Group()
    #创建随机位置,随机速度的笑脸球
    for i in range(12):
        position = randint(0, width - 100), randint(0, height - 100)
        speed = [randint(-10, 10), randint(-10, 10)]
        ball = Ball(speed_image, position, speed, bg_size)
        # 判断两个球出生时位置是否重叠
        while pygame.sprite.spritecollide(ball, group_1, False, pygame.sprite.collide_circle):
            ball.rect.left, ball.rect.top = randint(0, width - 100), randint(0, height - 100)
        group_1.add(ball)
    #固定的精灵球
    for i in range(3):
        position =randint(0,width-100),randint(0,height-100)
        speed = [0,0]
        ball = Ball(a_image,position,speed,bg_size)
        #判断两个球出生时位置是否重叠
        while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
            ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
        group.add(ball)
    #运动的精灵球
    for i in range(5):
        position =randint(0,width-100),randint(0,height-100)
        speed = [randint(-10,10),randint(-10,10)]
        ball = Ball(ball_image,position,speed,bg_size)
        #判断两个球出生时位置是否重叠
        while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
            ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
        group.add(ball)
    #斜笑脸球
    position = randint(0, width - 100), randint(0, height - 100)
    speed = [0,-5]
    face = Ball(face_image, position, speed, bg_size)
    group_2.add(face)
    while pygame.sprite.spritecollide(face, group, False, pygame.sprite.collide_circle):
        face.rect.left, face.rect.top = randint(0, width - 100), randint(0, height - 100)

    #游戏开始
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            elif event.type == circulation:
                pygame.mixer.music.play()
            if event.type == pygame.KEYDOWN:
                if event.key == K_LEFT:
                    face.speed = [-10,0]
                if event.key == K_RIGHT:
                    face.speed = [10,0]
                if event.key == K_UP:
                    face.speed = [0,-10]
                if event.key == K_DOWN:
                    face.speed = [0,10]


        #绘制背景
        screen.blit(background,(0,0))
        #绘制精灵球,笑脸球,斜笑脸球
        for each in group:
            screen.blit(each.image,each.rect)
            each.move()
        for each in group_1:
            screen.blit(each.image,each.rect)
            each.move()
        screen.blit(face.image,face.rect)
        face.move()
        #同种球碰撞后速度反向

        for each in group:
            group.remove(each)
            if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
                each.speed[0] = -each.speed[0]
                each.speed[1] = -each.speed[1]
            group.add(each)

        for each in group_1:
            group_1.remove(each)
            if pygame.sprite.spritecollide(each,group_1,False,pygame.sprite.collide_circle):
                each.speed[0] = -each.speed[0]
                each.speed[1] = -each.speed[1]
            group_1.add(each)

        for each in group_1:

            if pygame.sprite.spritecollide(each,group_2,False,pygame.sprite.collide_circle):
               group_1.remove(each)


        #斜笑脸在游戏开始前五秒无敌
        if pygame.sprite.spritecollide(face, group, False, pygame.sprite.collide_circle):
            if pygame.time.get_ticks() < 5000:
                running = True
            else:
                running = False
                screen.blit(background, (0, 0))
                pygame.display.flip()
                pygame.mixer.music.pause()
                losing.play()
                losing.set_volume(100)


        if not group_1:
            running = False
            screen.blit(background, (0, 0))
            pygame.display.flip()
            pygame.mixer.music.pause()
            win.play()
            win.set_volume(100)

        #屏幕更新,1秒30帧
        pygame.display.flip()
        clock.tick(30)



    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

        if group_1:
            text = pygame.font.Font("fonts/simfang.ttf",100)
            text_fmt = text.render("你太菜了",True,(0,0,0))
            screen.blit(text_fmt,(350,300))
            pygame.display.flip()

        else:
            text = pygame.font.Font("fonts/simfang.ttf",80)

            text_fmt = text.render("哈哈哈哈", True, (0, 0, 0))
            screen.blit(text_fmt, (200, 350))
            pygame.display.flip()




        clock.tick(30)


if __name__ == '__main__':
    main()

三.游戏界面

基于pygame的小游戏

四.代码素材包

链接:https://pan.baidu.com/s/159ahWG_ngsOXylD-zh5nbg 
提取码:67io
相关标签: python