当前位置:   article > 正文

Python版见缝插针小游戏源代码,球球旋转大作战源程序_python小游戏见缝插针代码150行

python小游戏见缝插针代码150行

见缝插针游戏是一款非常考验玩家手眼协调能力的休闲益智虐心虐脑小游戏,玩法很简单,但要过关却很有挑战性哟! 主要是将一系列的小球,插入到旋转的摩天轮转盘当中,插入过程中不能碰到旋转的摩天轮上的其他小球,全部插入即可过关!
在这里插入图片描述
核心程序代码
main.py

import pygame
import sys
import math
from tools import *

pygame.init()
size = width, height = 620, 450
screen = pygame.display.set_mode(size)
pygame.display.set_caption("见缝插针-Python代码大全")
fps = 60
fclock = pygame.time.Clock()
WHITE = 255, 255, 255
bg_color = 139, 134, 167
PURPLE = 114, 100, 122
ball_group = []
font = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 18)
stage_font = pygame.font.Font(r"C:\Windows\Fonts\simsun.ttc", 30)
# 转轴的中心位置
shaft = x0, y0 = 310, 160
# 针的长度
length = 140
# 转动的角速度
angular_speed = 0.004 * math.pi
stage = 8
# 每关要插的针的数量
ball_num = [10, 14, 12, 20, 13, 14, 16, 26]
remain_ball = 0
stage1 = [0, math.pi / 2, math.pi, math.pi * 1.5]
stage2 = [0, math.pi / 2, math.pi, math.pi * 1.5]
stage3 = [0, math.pi / 3, math.pi * 2 / 3, math.pi, math.pi * 4 / 3, math.pi * 5 / 3]
stage4 = [0, math.pi]
stage5 = [0, math.pi / 3, math.pi * 2 / 3, math.pi, math.pi * 4 / 3, math.pi * 5 / 3]
stage6 = [0, math.pi / 4, math.pi / 2, math.pi, math.pi * 3 / 4, math.pi, math.pi * 5 / 4, math.pi * 3 / 2,
          math.pi * 7 / 4]
stage7 = [0, math.pi / 4, math.pi / 2, math.pi, math.pi * 3 / 4, math.pi, math.pi * 5 / 4, math.pi * 3 / 2,
          math.pi * 7 / 4]
stage8 = [0, math.pi]
all_stage = [stage1, stage2, stage3, stage4, stage5, stage6, stage7, stage8]
gaming = True
stage_pass = False


class Ball:
    """
    针头
    """

    def __init__(self, angle):
        self.x = x0
        self.y = y0 + length
        self.center = (self.x, self.y)
        self.radius = 12
        self.angle = angle
        ball_group.append(self)

    def draw(self, surface):
        pygame.draw.line(surface, WHITE, shaft, self.center, 2)
        pygame.draw.circle(surface, WHITE, self.center, 12)

    def move(self, speed):
        """
        围绕转轴做圆周运动
        :param speed: 转动的角速度
        :return:
        """
        if self.angle < 2 * math.pi:
            self.angle += speed
        else:
            self.angle = self.angle - 2 * math.pi
        self.x = x0 - length * math.sin(self.angle)
        self.y = y0 + length * math.cos(self.angle)
        self.center = (self.x, self.y)


def check_collide(new_ball):
    for ball in ball_group:
        distance = math.sqrt(abs(ball.x - new_ball.x) ** 2 + abs(ball.y - new_ball.y) ** 2)
        if ball is not new_ball and distance <= new_ball.radius * 2:
            return True
    return False


def game_init():
    global stage, gaming, remain_ball, stage_pass

    if stage == len(all_stage):
        stage = 1
    elif stage_pass:
        stage += 1
    ball_group.clear()
    for a in all_stage[stage - 1]:
        b = Ball(a)
    remain_ball = ball_num[stage - 1]
    pygame.time.delay(200)
    gaming = True
    stage_pass = False


button = Button('重新开始', color=(220, 0, 0))
button.rect.center = shaft
button.click_connect(game_init)


def restart():
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        button.get_click(event)


def game_stage():
    global remain_ball, stage, gaming, stage_pass
    if remain_ball == 0 and gaming:
        stage_pass = True
        gaming = False


def update():
    screen.fill(bg_color)
    # 左上角关卡信息
    stage_num = stage_font.render('第{}关'.format(stage), True, WHITE)
    stage_rect = stage_num.get_rect()
    stage_rect.left = 5
    stage_rect.top = 5
    screen.blit(stage_num, stage_rect)
    # 绘制转轴的圆盘
    pygame.draw.circle(screen, WHITE, (310, 160), 42)
    # 绘制剩余针头提示
    pygame.draw.circle(screen, WHITE, (310, 380), 12)
    # 剩余针头数量
    remain_num = font.render('{}'.format(remain_ball), True, PURPLE)
    remain_rect = remain_num.get_rect()
    remain_rect.center = (310, 380)
    screen.blit(remain_num, remain_rect)
    for ball in ball_group:
        if gaming:
            ball.move(angular_speed)
        ball.draw(screen)
    if not gaming:
        if stage_pass and stage < len(all_stage):
            button.set_text('下一关')
        elif stage_pass and stage >= len(all_stage):
            button.set_text('重新开始')
        elif not stage_pass:
            button.set_text('重玩此关')
        button.draw(screen)


def main():
    global gaming, remain_ball, stage
    while True:
        if gaming:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    if remain_ball > 0:
                        new_ball = Ball(0)
                        remain_ball -= 1
                        if check_collide(new_ball):
                            gaming = False
        else:
            restart()
        game_stage()
        update()
        pygame.display.update()
        fclock.tick(fps)


if __name__ == '__main__':
    game_init()
    main()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175

完整程序下载地址:Python版见缝插针小游戏源代码

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号