import random
import pygame
pygame.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_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")
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
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))
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()
pygame.display.update()
2.0
import random
import pygame
import sys
def figure(screen, photo, x, y):
content = pygame.image.load(photo)
screen.blit(content, (x, y))
def button(screen, x,y,w,h,color):
pygame.draw.rect(screen,color,(x,y,w,h))
ziti(screen,'排行榜',(x+10),(y+10))
def ziti(screen, text, x, y, size = 24,color=(232, 206, 144)):
font = pygame.font.Font("ziti.ttf", size)
img_text = font.render(text, True, color)
screen.blit(img_text, (x, y))
def run():
screen_w, screen_h = 1080, 652
score = 0
result = {}
play_num = 1
hp = 3
speed = 5
ball_x, ball_y = 400, 0
ban_x, ban_y, ban_width, ban_height = 400, screen_h - 40, 360, 53
start = "请点击开始游戏"
result = ""
title = "接亚瑟"
ball_figure = "yase.png"
bj_figure = "bg.jpg"
ban ="ban5.png"
game_over = True
moving_left = False
moving_right = False
moving_up = False
moving_down = False
screen = pygame.display.set_mode((screen_w, screen_h))
pygame.display.set_caption(title)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
if
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_a or event.key == pygame.K_LEFT:
moving_left = True
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
moving_right = True
elif event.key == pygame.K_w or event.key == pygame.K_UP:
moving_up = True
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
moving_down = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
moving_left = False
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
moving_right = False
elif event.key == pygame.K_w or event.key == pygame.K_UP:
moving_up = False
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
moving_down = False
figure(screen, bj_figure, 0, 0)
if game_over:
ziti(screen, start, 100,screen_h / 2,50)
ziti(screen, result, 100, screen_h / 2 + 50,50)
button(screen, screen_w/2-50, 20,100,50,(0,0,0))
else:
ball_y = ball_y + speed
if moving_right:
ban_x += 30
elif moving_left:
ban_x -= 30
elif moving_down:
ban_y += 30
elif moving_up:
ban_y -= 30
if ball_y > screen_h:
ball_x = random.randint(0, screen_w)
ball_y = 0
hp = hp - 1
speed = 1
if hp == 0:
start = "重新开始请点击屏幕"
result = "你击杀了%d个小球" % score
game_over = True
if ban_x <= ball_x <= ban_x + ban_width and ban_y <= ball_y <= ban_y + ban_height:
score = score + 1
speed = speed + 1
ball_x = random.randint(0, 600)
ball_y = 0
ziti(screen, '分数:%d' % score, 0, 0)
ziti(screen, "生命值:%d" % hp, screen_w - 120, 0)
figure(screen, ball_figure, ball_x, ball_y)
figure(screen,ban,ban_x, ban_y)
pygame.display.update()
def main():
pygame.init()
run()
if __name__ == '__main__':
main()