pygame制作简单小游戏

初学者,有问题大家一起交流

import random
import pygame
from pygame.locals import *
import sys
import math;
import pygame.freetype


class Game:
    def __init__(self):
        pygame.init()
        self.W, self.H = 800, 600;
        self.screen = pygame.display.set_mode((self.W, self.H))
        pygame.display.set_caption("Window");
        self.ball = pygame.image.load("./image/04.jpg");
        # 设置碳板的宽度
        self.leng = 140;
        # 设置碳板的初始坐标
        self.left = 0;
        self.right = self.left + self.leng;
        # 圆心的初始位置:
        self.cirx = random.randint(20, self.W - 20);
        self.ciry = random.randint(35, self.H - 55);
        # flag
        self.flag = 1;
        self.to = "r";
        self.cnt = 1;
        # 设置帧速率:
        self.fpsClock = pygame.time.Clock();
        self.fps = 60;
        # 设置检测时间
        pygame.key.set_repeat(10, 15);
        # 背景音乐
        pygame.mixer.init()
        pygame.mixer.music.load("./sing/01.mp4");
        pygame.mixer.music.play(-1)
        # 设置速度
        self.stspeed = 2;
        self.ti = 0;
        # 设置成绩列表
        self.score = [0];
        self.maxscore=0;
        # 绘制文字
        pygame.font.init();
        self.str1="您目前的最高得分为:{}".format(self.maxscore);
        self.a = pygame.font.SysFont("kaiti", 36);
        self.num1 = self.a.render("重新开始", True, (0, 0, 0));
        self.num2 = self.a.render("退出游戏", True, (0, 0, 0));
        self.num3=self.a.render(self.str1,True,(0,0,0));
        #游戏开关
        self.ok=1;
        #获取鼠标位置
        self.x=0;
        self.y=0;

    def listen(self):
        #求最大分数
        for i in self.score:
            if i>self.maxscore:
                self.maxscore=i;
        self.str1="您目前的最高得分为:{}".format(self.maxscore);
        self.num3 = self.a.render(self.str1, True, (0, 0, 0));
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            # 底部碳板的移动轨迹
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a:  # 左
                    if self.left <= -70:
                        break;
                    else:
                        self.left -= 5;
                        self.right -= 5;
                elif event.key == pygame.K_d:  # 右
                    if self.right >= self.W + 70:
                        break;
                    else:
                        self.left += 5;
                        self.right += 5;
            elif event.type==pygame.MOUSEBUTTONUP:
                (self.x,self.y)=pygame.mouse.get_pos();
        # 球的移动轨迹
        if self.ti % 1000 == 0 and self.stspeed < 12:
            self.stspeed += 1;
        if self.flag == 1:  # 第一次下降状态
            self.ti += 2;
            self.ciry += 2;
            if self.ciry < 573 and self.ciry > 559 and self.cirx >= self.left and self.cirx <= self.right:
                self.flag = 2;  # 打在板上
            if self.ciry > self.H:
                self.flag = 0;
                print("lose");
        elif self.flag == 2:  # 上升状态               不用改变状态
            self.ti += 2;
            if self.to == "r":
                self.ciry -= self.stspeed;
                self.cirx += self.stspeed;
            elif self.to == "l":
                self.ciry -= self.stspeed;
                self.cirx -= self.stspeed;
            if self.ciry <= 12.5 and self.cirx > 20 and self.cirx < self.W - 20:
                self.flag = 3;  # 打在上面反弹;
            if self.cirx <= 20 or self.cirx >= self.W - 20:
                self.flag = 5;  #
            if self.ciry > self.H:
                self.flag = 0;
                print("lose");
        elif self.flag == 3:  # 第一次以后的下降墙上       不需要改变状态
            self.ti += 2;
            if self.to == "r":
                self.ciry += self.stspeed;
                self.cirx += self.stspeed;
            elif self.to == "l":
                self.ciry += self.stspeed;
                self.cirx -= self.stspeed;
            if self.ciry < 573 and self.ciry > 559 and self.cirx >= self.left and self.cirx <= self.right:
                self.flag = 2;  # 打在板上
            if self.cirx <= 20 or self.cirx >= self.W - 20:
                self.flag = 4;  # 打在墙体上;
            if self.ciry > self.H:
                self.flag = 0;
                print("lose");
        elif self.flag == 4:  # 从上面打到墙上
            self.ti += 2;
            if self.cnt == 1:
                self.cnt -= 1;
                if self.to == "r":
                    self.to = "l";
                elif self.to == "l":
                    self.to = "r";
            else:
                if self.to == "r":
                    self.ciry += self.stspeed;
                    self.cirx += self.stspeed;
                elif self.to == "l":
                    self.ciry += self.stspeed;
                    self.cirx -= self.stspeed;
            # if self.cirx <= 20 or self.cirx >= self.W - 20:
            # self.flag = 4;  # 打在墙体上;
            if self.ciry < 573 and self.ciry > 559 and self.cirx >= self.left and self.cirx <= self.right:
                self.cnt += 1;
                self.flag = 2;  # 打在板上
            if self.ciry > self.H:
                self.flag = 0;
                print("lose");
        elif self.flag == 5:  # 从下面达到墙上
            self.ti += 2;
            if self.cnt == 1:
                self.cnt -= 1;
                if self.to == "r":
                    self.to = "l";
                elif self.to == "l":
                    self.to = "r"
            else:
                if self.to == "r":
                    self.ciry -= self.stspeed;
                    self.cirx += self.stspeed;
                elif self.to == "l":
                    self.ciry -= self.stspeed;
                    self.cirx -= self.stspeed;
            # if self.cirx <= 20 or self.cirx >= self.W - 20:
            #   self.flag = 5;  # 打在墙体上;
            if self.ciry <= 12.5 and self.cirx > 20 and self.cirx < self.W - 20:
                self.cnt += 1;
                self.flag = 3;  # 打在上面反弹;
            if self.ciry > self.H:
                self.flag = 0;
                print("lose");
        elif self.flag == 0:
            self.score.append(self.ti);
            pygame.mixer.music.stop();
            self.screen.blit(self.num3,(250,200));
            self.screen.blit(self.num1, (300, 300));
            self.screen.blit(self.num2, (300, 400));
            if self.x>300 and self.x<440 and self.y>300 and self.y<330:
                self.x=0;
                self.y=0;
                self.left = 0;
                self.cnt = 1;
                self.right = self.left + self.leng;
                self.cirx = random.randint(20, self.W - 20);
                self.ciry = random.randint(35, self.H - 55);
                self.flag=1;
                self.ti=0;
                self.to="r";
                pygame.mixer.music.play();
                self.stspeed=2;
            elif self.x>300 and self.x<440 and self.y>400 and self.y<430:
                sys.exit()

    def draw(self):
        self.screen.fill((255, 255, 255))
        # 渲染背景图片
        self.ball = pygame.transform.scale(self.ball, (self.W, self.H));
        self.screen.blit(self.ball, (0, 0));
        # 画顶部直线
        pygame.draw.line(self.screen, (255, 0, 0), (0, 10), (self.W, 10), 5);
        # 画底部碳板
        pygame.draw.line(self.screen, (0, 0, 0), (self.left, self.H), (self.right, self.H), 30);
        # 画弹弹球
        pygame.draw.circle(self.screen, (130, 130, 130), (self.cirx, self.ciry), 20, 20);

    def run(self):
        while self.ok:
            self.draw()
            self.listen()
            self.fpsClock.tick(self.fps);
            pygame.display.update()


if __name__ == '__main__':
    game = Game()
    game.run();

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卑微的小红猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值