import random
import pygame as p
import time
p.init()
screen = p.display.set_mode((682, 500))
# 设置标题 captiion --标题
p.display.set_caption("无限接球")
# 具体指应用函数的时候写括号
ballx = 100
bally = 150
# 分别为:板的x坐标,板的y坐标,板的宽度,板的高度
ban_x, ban_y, ban_k, ban_g = 300, 460, 120, 40
# 让电脑没过多少毫秒监控一次
p.key.set_repeat(100, 1)
# 1.设置字体 font 字体和大小
fenshu_ziti = p.font.Font("ziti.ttf", 30)
gepi_ziti = p.font.Font("ziti.ttf", 80)
nyl = p.font.Font("ziti.ttf", 80)
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("bg.jpg")
while 1 > 0:
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:
text3 = gepi_ziti.render("嗝屁喽", True, (0, 0, 0))
screen.blit(text3, (330,250 ))
elif win:
text4 = nyl.render("you win", True, (0, 0, 0))
screen.blit(text4, (330, 250))
else:
# 设置背景颜色
# screen.fill((120, 255, 30))
screen.blit(bj,(0,0))
bally = bally + 1
# 判断没接到小球 ,生命值减1分
if bally > 500:
bally = 0
ballx = ballx + 100
smz = smz - 1
ziticolor = ziticolor + 50
if ballx > 682:
ballx = 0
# 左边空气墙
if ban_x < -50:
ban_x = 5
elif 682 < ban_x:
ban_x = 580
# 画一个小球
# p.draw.circle(screen, (172, 85, 255), (ballx, bally), 10, 0)
screen.blit(sanjitou,(ballx, bally))
# 画一个长方形 rect 屏幕 颜色
p.draw.rect(screen, (172, 85, 255), (ban_x, ban_y, ban_k, ban_g), 0)
# 判断接到小球才加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, 682)
# 2.通过设置的字体大小来设置文字和颜色
text = fenshu_ziti.render("分数:%d" % score, True, (0, 0, 0))
# 3,把文字放在窗口上
screen.blit(text, (0, 0))
text2 = fenshu_ziti.render("生命值:%d" % smz, True, (ziticolor, 0, 0))
screen.blit(text2, (500, 0))
# 判断咯屁
if smz == 0:
game_over = True
if score > 5:
win = True
p.display.update()
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交