事件小游戏(是男人就下100层)

2018-07-30  本文已影响0人  慢节拍_2709

第一关

import pygame
import random
def draw_ball(place,color,pos):
    #画球
    pygame.draw.circle(place,color,pos,40)

def ran_color():
    return random.randint(0,255),random.randint(0,255),random.randint(0,255)

def is_touch():
    if (((ball_x-ball1_x)**2)+((ball_y-ball1_y)**2)<=6400):
        print('失败,游戏结束!')
        exit()
    if (((ball_x - ball2_x) ** 2) + ((ball_y - ball2_y) ** 2) <= 6400):
        print('失败,游戏结束!')
        exit()
    if (((ball_x - ball3_x) ** 2) + ((ball_y - ball3_y) ** 2) <= 6400):
        print('失败,游戏结束!')
        exit()
    if (((ball_x - ball4_x) ** 2) + ((ball_y - ball4_y) ** 2) <= 6400):
        print('失败,游戏结束!')
        exit()

def is_contact(ball_x, x_speed,speeds):
    if ball_x+40 >= 800:
        x_speed *= -1
    elif ball_x-40 <= 0:
        x_speed += speeds
    return x_speed
if __name__ == '__main__':
    pygame.init()
    desktop = pygame.display.set_mode((800,600))
    pygame.display.set_caption('球球游戏')
    desktop.fill((255,255,255))

    # 保存初始坐标
    ball_x = 100
    ball_y = 100

    ball1_x = 200
    ball1_y = 200

    ball2_x = 200
    ball2_y = 400

    ball3_x = 300
    ball3_y = 300

    ball4_x = 500
    ball4_y = 500

    x_speed = 0
    y_speed = 0

    x_speed1 = 10
    y_speed1 = 0

    x_speed2 = 20
    y_speed2 = 0

    x_speed3 = -15
    y_speed3 = 0

    x_speed4 = -45
    y_speed4 = 0
    # 方向对应的key值
    Up = 273
    Down = 274
    Left = 276
    Right = 275



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

            if event.type == pygame.KEYDOWN:
                if event.key == Up:
                    y_speed = -15
                    x_speed = 0
                elif event.key == Down:
                    y_speed = +15
                    x_speed = 0
                elif event.key == Left:
                    y_speed = 0
                    x_speed = -15
                elif event.key == Right:
                    y_speed = 0
                    x_speed = +15

        pygame.time.delay(60)

        # 刷新屏幕
        desktop.fill((255,255,255))

        ball_x += x_speed
        ball_y += y_speed

        ball1_x += x_speed1
        ball1_y += y_speed1

        ball2_x += x_speed2
        ball2_y += y_speed2

        ball3_x += x_speed3
        ball3_y += y_speed3

        ball4_x += x_speed4
        ball4_y += y_speed4
        # 调用函数来判断圆是否碰壁
        x_speed = is_contact(ball_x, x_speed, 15)
        x_speed1 = is_contact(ball1_x, x_speed1,10)
        x_speed2 = is_contact(ball2_x, x_speed2,20)
        x_speed3 = is_contact(ball3_x, x_speed3,15)
        x_speed4 = is_contact(ball4_x, x_speed4, 45)

        if ball_y >= 600:
            print('恭喜通关!')
            exit()

        draw_ball(desktop,(255,0,0),(ball_x,ball_y))
        # 画出移动的圆
        draw_ball(desktop, ran_color(), (ball1_x,ball1_y))
        draw_ball(desktop, ran_color(), (ball2_x,ball2_y))
        draw_ball(desktop, ran_color(), (ball3_x, ball3_y))
        draw_ball(desktop, ran_color(), (ball4_x, ball4_y))
        pygame.display.update()
        print(is_touch())
黑色的是控制球

目前只有第一关,有兴趣的可以搞一哈

上一篇下一篇

猜你喜欢

热点阅读