1.0
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((700,600))
pygame.display.set_caption("接小球游戏")
ball_x,ball_y = 400,400
ban_x, ban_y ,ban_width, ban_height = 400,550,220,100
font = pygame.font.Font('ziti.ttf',24)
ball_true = pygame.image.load("qiu.gif")
score=0
hp = 3
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
ban_x,_ = event.pos
ball_y = ball_y+1
screen.fill((255,255,255))
if ball_y>600:
ball_x = random.randint(0,600)
ball_y = 0
hp=hp-1
if ban_x <=ball_x<=ban_x+220 and ban_y <=ball_y<=ban_y+100:
score = score+1
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))
screen.blit(ball_true,(ball_x,ball_y))
pygame.draw.rect(screen,(100,255,0),(ban_x, ban_y ,ban_width, ban_height),0)
pygame.display.update()
2.0
import random
import pygame
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((700,600))
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_true = pygame.image.load("ball2.png")
hit = pygame.mixer.Sound("hit_wall.wav")
hit.set_volume(0.4)
score=0
hp = 3
game_over = True
while True:
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
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))
screen.blit(ball_true,(ball_x,ball_y))
pygame.draw.rect(screen,(100,255,0),(ban_x, ban_y ,ban_width, ban_height),0)
pygame.display.update()