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 字体
ziti = p.font.Font("ziti.ttf", 30)
score = 0
smz = 5
ziticolor = 0
game_over = False
win = False
# time.sleep(3)
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:
pass
#需要显示你死了
elif win:
pass
else:
screen.fill((120, 255, 30))
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)
# 画一个长方形 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)
# 设置文字
text = ziti.render("分数:%d" % score, True, (0, 0, 0))
screen.blit(text, (0, 0))
text2 = ziti.render("生命值:%d" % smz, True, (ziticolor, 0, 0))
screen.blit(text2, (500, 0))
# 判断咯屁
if smz == 0:
game_over = True
p.display.update()
ziheng -接小球游戏4.0
最新推荐文章于 2020-07-05 20:51:59 发布