python +pygame 制作五子连珠小游戏

python +pygame 制作五子连珠小游戏

学习python半年了,今天分享一个利用pygame制作的五子连珠游戏。
一、代码:
1.球类,ball.py

"""
球类
"""
import pygame
from pygame.sprite import Sprite
from settings import Settings
import time
from music import iMusic

class Ball(Sprite):

    def __init__(self,id,screen,image_id,preantRect):
        super().__init__()
        """
        初始化球
        :param screen: 屏幕
        :param image: 球的着色
        :param preantRect:所在区域 
        """
        self.ai_settings=Settings
        self.images=self.ai_settings.images
        self.id=id
        self.image_id=image_id
        self.image=self.images[self.image_id]
        self.rect=self.image.get_rect()
        self.rect.center=preantRect.center
        self.screen=screen
        self.x=float(self.rect.x)
        self.y=float(self.rect.y)
        self.music=iMusic('Dou')
        self.draw()


    def draw(self):
        self.screen.blit(self.image,self.rect)



    def move(self,firsti,firstj,endi,endj):

        length=self.ai_settings.length
        endid=endj*10+endi

        # print('firstid:',firstid,'endid:',endid)

        if firsti>endi:
            # self.rect.move(-50,0)
            self.x-=length
            self.rect.x=self.x

            # print('从{0}移动到{1}成功:'.format(firstid, endid))
        elif firsti<endi:
            # self.rect.move(50, 0)
            self.x += length
            self.rect.x = self.x
            # print('从{0}移动到{1}成功:'.format(firstid, endid))
        elif firstj > endj:
            self.y -= length
            self.rect.y = self.y
            # self.rect.move(0,-50)
            # print('从{0}移动到{1}成功:'.format(firstid, endid))
        elif firstj < endj:
            self.y += length
            self.rect.y = self.y
            # print('从{0}移动到{1}成功:'.format(firstid, endid))
        self.id=endid
        # print('id:',self.id,self.rect)
        self.draw()
        self.music.play()
        time.sleep(self.ai_settings.speed)


2.按钮类,button.py

import pygame.font
"""
v2.0
创建内置按钮


"""
class Button():
    def __init__(self,screen,msg):
        """
        初始化按钮
        :param ai_settings: 设置类
        :param screen: 屏幕类
        :param msg: 文字
        
        """
        self.screen=screen
        self.screen_rect=screen.get_rect()
    #   设置按钮的尺寸和其他属性
        self.width=200
        self.height=50
        self.button_color=(0,255,0)
        self.text_color=(255,255,255)
        self.font=pygame.font.SysFont('kaiti',48)
    #     设置按钮的rect属性,并让其居中
        self.rect=pygame.Rect(0,0,self.width,self.height)
        self.rect.center=self.screen_rect.center
    #     按钮的标签只需创建一次
        self.prep_msg(msg)
    def prep_msg(self,msg):
        """
        将msg渲染为图像,并使其在按钮上居中
        :param msg: 要显示的文本
        :return: None
        """
        self.msg_image=self.font.render(
            msg,True,self.text_color,self.button_color)
        self.msg_image_rect=self.msg_image.get_rect()
        self.msg_image_rect.center=self.rect.center
    def draw_button(self):
    #     绘制一个用颜色填充的按钮,再绘制文本
        self.screen.fill(self.button_color,self.rect)
        self.screen.blit(self.msg_image,self.msg_image_rect)

3.主函数类,是游戏的精华所在,game_function.py

"""
主函数类
"""
from ball import Ball
from settings import Settings
# from score import Score

# from pygame.mixer import music
from configobj import ConfigObj

from music import iMusic

import pygame
import pygame.gfxdraw
import random

import time

class Function():
    def __init__(self):
        self.ai_settings = Settings
        pygame.display.set_caption('五子连珠'+self.ai_settings.version)
        self.screen = pygame.display.set_mode(
            (self.ai_settings.screen_height,self.ai_settings.screen_width))
        self.screen.fill(self.ai_settings.background_color)
        # 难度控制
        self.difficulty = self.ai_settings.difficulty
        # 游戏区的设置
        # 1.网格长度
        self.length=self.ai_settings.length
        # 2.设置游戏区的范围
        self.rect=pygame.Rect(20,20,self.length*10,self.length*10)
        # 3.存储网格位置列表
        self.rect_list = []
        # 4.存储网格背景色列表
        self.color_list = []
        # 游戏主数据列
        # 1.存储球移动路径的列表
        self.ball_list=[]
        # 2.存储没有球的区域列表
        self.surplus_ball_list=[]
        # 3.存储各个球所在位置的列表
        self.main_ball_list=[]
        # 4.存储Ball类的列表
        self.balls=pygame.sprite.Group()
        # 5.存储ball_move_list列表
        self.ball_move_list=[]
        # 6.判断是否要移动球
        self.isMove=False

        # 6.1判断球是否被选中
        self.isChoose=False
        # 7.存储移动前后的id
        self.ball_first_id=-1
        self.ball_end_id=-1

        # 8.存储可删除球的id
        self.remove_ball_list=[]
        # 设置文字
        self.font=pygame.font.SysFont('kaiti',30)

        # 初始总分
        self.Main_score=0

        # 最高分
        self.Max_score=0
        # 显示屏幕
        self.screen_draw()
        # 显示球列表
        self.images = ['r','g','b', 'y', 'p','o','w']

        self.Move_music=iMusic('Move')
        self.Great_music=iMusic('Great')
        self.Fantastic_music = iMusic('Fantastic')
        self.Dou_music=iMusic('Dou')

    #     next
        self.next_ball_list=[]

        self.next_rect=[]

        self.file_list = []


    def read_maxscore_file(self):

        inifile=ConfigObj('data.ini',encoding='UTF8')


        temp_high_score=inifile['data']['hihg_score']
        # print(tempdata)

        self.Max_score=eval(temp_high_score)

    def write_maxscore_file(self):

        inifile=ConfigObj('data.ini',encoding='UTF8')


        inifile['data']['hihg_score']=self.Max_score
        # print(tempdata)

        # self.Max_score=temp_high_score
        inifile.write()

    def read_ball_scro_file(self):

        inifile = ConfigObj('data.ini', encoding='UTF8')

        temp_ball_list = inifile['Main']['ball_list']
        print(temp_ball_list)
        temp_ball_list=list(map(int,temp_ball_list))
        print(temp_ball_list)

        temp_score=int(inifile['Main']['score'])
        # temp_high_score = inifile['hihg_score'][hihg_score]
        # print(tempdata)
        if len(temp_ball_list)!=0 and temp_score!=0:
            self.main_ball_list = temp_ball_list
            self.Main_score=temp_score
        # self.Max_score = temp_high_score

    def write_ball_scro_file(self):

        inifile = ConfigObj('data.ini', encoding='UTF8')

        if not self.game_over():
            print(self.main_ball_list)
            inifile['Main']['ball_list']=self.main_ball_list

            inifile['Main']['score']=self.Main_score
            inifile.write()
        # temp_high_score = inifile['hihg_score'][hihg_score]
        # print(tempdata)

            # self.Max_score = temp_high_score

    def game_over(self):
        isgameover=False
        if len(self.surplus_ball_list)==0:
            isgameover=True
            print('游戏结束了!')
        return isgameover

    def show_next_ball(self):

        for i in range(3):
            if i==1:
                temp_color=self.ai_settings.foreground_color
            else:
                temp_color=self.ai_settings.high_color

            temp_rect=pygame.Rect(660+i*self.length,230,self.length,self.length)

            self.next_rect[i]=temp_rect

            self.screen.fill(temp_color,
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值