基于pygame用python做接小球游戏

此程序共使用了pygame以及其他3个库如果电脑中没有这些库可以自行下载~~~

不知如何下载请看:python安装工具包

此程序的所有图片,字体在最后都有下载链接!!!

[/tudou]

 

下面是程序:

#导入库
import pygame
import sys
import time
import random
#初始化
pygame.init()
screen = pygame.display.set_mode((600, 500))  # 设置窗口
pygame.display.set_caption('猩球大战')
ball_x, ball_y = random.randint(50, 550), 50
rect_x, rect_y, rect_w, rect_h = 0, 500 - 80, 200, 20
Down = True
score = 0
font = pygame.font.Font("ziti.ttf", 24)
font2 = pygame.font.Font("ziti.ttf", 50)
xue = 5
pygame.mixer.init()
#事件
game_over = False
# 加载图片
'''
----------------------
图片可在文章结尾链接下载
----------------------
'''
boss = pygame.image.load("cai.png")
bg = pygame.image.load("bg1.jpg")
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # 判断事件类型是不是退出
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEMOTION:
            rect_x, rect_y = event.pos
            rect_x = rect_x - 100
            rect_y = 480
    screen.fill((255, 255, 0))
    screen.blit(bg,(0,0))
    ball_y += 1
    # 是否接到球
    if ball_y > 450:
        ball_y = 50
        ball_x = random.randint(50, 550)
        if xue > 1:
            xue = xue - 1
        else:
            xue = 0
            game_over = True
    if game_over:
        test3 = font2.render("GAME OVER!!!", True, (255, 0, 0))
        screen.blit(test3,(150,210))
    else:
        if ball_y + 50 > 480 and rect_x + 200 > ball_x > rect_x:
            hit.play()
            score = score + 1
            ball_y = 50
            ball_x = random.randint(50, 550)
        text1 = font.render("分数:%d" % score, True, (0, 0, 0))
        screen.blit(text1, (0, 0))
        text2 = font.render("血条:%d" % xue, True, (0, 0, 0))
        screen.blit(text2, (0, 30))
        # 画圆
         pygame.draw.circle(screen, (2, 20, 200), (ball_x, ball_y), 50)
        screen.blit(boss, (ball_x, ball_y))
        # 画矩形
        pygame.draw.rect(screen, (200, 200, 200), (rect_x, rect_y, rect_w, rect_h))
        time.sleep(0.01)
    pygame.display.update()  # 刷新
# 结束
pygame.quit()  # 退出游戏

点击链接下载资源:接小球游戏资源包

更多精彩内容

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注