import random
import pygame
pygame.init() # 1.游戏初始化
screen = pygame.display.set_mode((700,600)) # 2.创建一个窗口,设置大小
pygame.display.set_caption("接小球游戏")
ball_x,ball_y = 400,0
ban_x, ban_y ,ban_width, ban_height = 400,550,220,100
font = pygame.font.Font('ziti.ttf',24)
ball_true1 = pygame.image.load("ball.bmp")
ball_true2 = pygame.image.load("ball1.png")
ball_true3 = pygame.image.load("ball2.png")
ball_true4 = pygame.image.load("ball3.png")
# pygame.mixer.init() # 2.加载音效
# hit = pygame.mixer.Sound("hit_wall.wav") # 加载音乐
# hit.set_volume(0.4) # 设置音量
#
#pygame.mixer.music.load("bg.mp3")
#pygame.mixer.music.set_volume(0.3)
#pygame.mixer.music.plat(-1)
score=0
hp = 3
game_over = True
a_left = 0
a_right = 0
while True:
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONUP:
if game_over:
game_over = False
score = 0
hp = 4
elif event.type == pygame.MOUSEMOTION:
ban_x,_ = event.pos
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
a_right = 0
a_left =a_left+10
ban_x = ban_x-(10+a_left)
elif event.key == pygame.K_RIGHT:
a_left = 0
a_right = a_right+10
ban_x= ban_x+10+a_right
screen.fill((255,255,255))
if game_over:
imgtext = font.render('请点击屏幕开始游戏' , True, (0, 0, 0)) # 设置文字参数,内容,锯齿话,颜色
screen.blit(imgtext, (350, 300)) # 将文字放在屏幕上
else:
ball_y = ball_y + 1
# 没接到小球的判定
if ball_y>600:
ball_x = random.randint(0,600)
ball_y = 0
hp = hp-1
if hp ==0:
game_over=True
# 接到小球的判定结果
if ban_x <=ball_x<=ban_x+220 and ban_y <=ball_y<=ban_y+100:
score = score+1
# hit.play()
ball_x = random.randint(0, 600)
ball_y = 0
imgtext = font.render('分数:%d'%score,True,(0,0,0)) # 设置文字参数,内容,锯齿话,颜色
screen.blit(imgtext,(0,0)) # 将文字放在屏幕上
imgtext2 = font.render("生命值:%d"%hp,True,(150,162,22))
screen.blit(imgtext2,(580,0))
# pygame.draw.circle(screen,(100,255,0),(ball_x,ball_y),30,0)
if 0< score <=5:
screen.blit(ball_true1,(ball_x,ball_y))
elif 10> score>=5:
screen.blit(ball_true2,(ball_x,ball_y))
elif 15>score>=10:
screen.blit(ball_true3, (ball_x, ball_y))
elif 20>score>=15:
screen.blit(ball_true4, (ball_x, ball_y))
pygame.draw.rect(screen,(100,255,0),(ban_x, ban_y ,ban_width, ban_height),0)
pygame.display.update() #3.不断循环
pygame.display.update() # 3.不断循环
Boren-python接小球游戏
猜你喜欢
转载自blog.csdn.net/houlaos/article/details/102765194
今日推荐
周排行