import random
import pygame as p
import time
def wenzi(screen, ziti_size, wenzi_content, xy, color=(0, 0, 0)):
# 窗口,字体大小,文字内容,坐标,颜色-默认为黑色
ziti = p.font.Font("ziti.ttf", ziti_size)
text3 = ziti.render(wenzi_content, True, color)
screen.blit(text3, xy)
def main():
p.init()
screen_width, screen_height = 1080, 652
screen = p.display.set_mode((screen_width, screen_height))
# 设置标题 captiion --标题
p.display.set_caption("无限接球")
# 具体指应用函数的时候写括号
ballx = 100
bally = 150
# 分别为:板的x坐标,板的y坐标,板的宽度,板的高度
ban_x, ban_y, ban_k, ban_g = 300, screen_height - 40, 120, 40
# 让电脑没过多少毫秒监控一次
p.key.set_repeat(100, 1)
# 1.设置字体 font 字体和大小
score = 0
smz = 5
ziticolor = 0
game_over = False
win = False
# time.sleep(3)
# 精灵图
# 加载图片 iamge图片 load加载
sanjitou = p.image.load("ball.png")
bj = p.image.load("bg1.jpg")
# 音乐初始化
p.mixer.init()
# 背景音乐
p.mixer.music.load("xxxxx.mp3")
p.mixer.music.set_volume(0.3)
p.mixer.music.play(-1)
# 音效
die = p.mixer.Sound("xxxxx.wav")
die.set_volume(0.4)
while True:
for sj in p.event.get():
# print(sj)
if sj.type == p.QUIT:
p.quit()
# 判断事件类型是不是按键 键 key
elif sj.type == p.KEYDOWN:
# 判断按了哪一个键
if sj.key == p.K_RIGHT:
ban_x = ban_x + 10
elif sj.key == p.K_LEFT:
ban_x = ban_x - 10
# 判断事件类型是不是鼠标动作 motion动作
elif sj.type == p.MOUSEMOTION:
ban_x, _ = sj.pos
if game_over:
die.play()
wenzi(screen, 80, "嗝屁喽", (330, 250))
elif win:
wenzi(screen, 80, "yun win", (330, 250))
else:
# 设置背景颜色
screen.blit(bj, (0, 0))
bally = bally + 5
# 判断没接到小球 ,生命值减1分
if bally > screen_height:
bally = 0
ballx = ballx + 100
smz = smz - 1
ziticolor = ziticolor + 50
if ballx > screen_width:
ballx = 0
# 左边空气墙
if ban_x < -50:
ban_x = 5
elif screen_width < ban_x:
ban_x = screen_width - 100
# 判断接到小球才加1分
if ban_x < ballx < ban_x + ban_k:
if ban_y < bally < ban_y + ban_g:
score = score + 1
bally = 0
ballx = random.randint(0, screen_width)
# 判断咯屁
if smz == 0:
game_over = True
if score > 5:
win = True
# 画一个小球
screen.blit(sanjitou, (ballx, bally))
# 画一个长方形 rect 屏幕 颜色
p.draw.rect(screen, (172, 85, 255), (ban_x, ban_y, ban_k, ban_g), 0)
# 2.通过设置的字体大小来设置文字和颜色
wenzi(screen, 30, "分数:%d" % score, (0, 0))
wenzi(screen, 30, "生命值:%d" % smz, (screen_width - 180, 0))
p.display.update()
if __name__ == '__main__':
main()
ziheng -接小球游戏6.0
最新推荐文章于 2020-07-05 20:51:59 发布