import pygame
import time
import random
pygame
.init
()
screen_height
= 652
screen_width
= 1080
ball_x
= 300
ball_y
= 250
rect_x
, rect_y
, rect_w
, rect_h
= 300, 612, 120, 40
score
= 0
lives
= 30
game_over
= True
screen
= pygame
.display
.set_mode
((screen_width
, screen_height
))
pygame
.display
.set_caption
("蔡旭坤大招乔碧落")
font1
= pygame
.font
.Font
('ziti.ttf', 24)
ball1
= pygame
.image
.load
('ball.png')
ball2
= pygame
.image
.load
('ball2.png')
bg
= pygame
.image
.load
("bg1.jpg")
pygame
.mixer
.init
()
pygame
.mixer
.music
.load
('hit_wall.wav')
pygame
.mixer
.music
.set_volume
(0.3)
pygame
.mixer
.music
.play
(-1)
hit
= pygame
.mixer
.Sound
("hit_wall.wav")
hit
.set_volume
(0.4)
def ball(ball_x
, ball_y
):
pygame
.draw
.circle
(screen
, (255., 25, 52), (ball_x
, ball_y
), 20, 0)
while True:
for event
in pygame
.event
.get
():
print(event
)
if event
.type == pygame
.QUIT
:
pygame
.quit
()
elif event
.type == pygame
.MOUSEBUTTONUP
:
if game_over
:
game_over
=False
elif event
.type == pygame
.MOUSEMOTION
:
rect_x
, _
= event
.pos
elif event
.type == pygame
.KEYDOWN
:
if event
.key
== pygame
.K_a
:
rect_x
= rect_x
- 50
elif event
.key
== pygame
.K_d
:
rect_x
= rect_x
+ 50
if score
< 10:
screen
.fill
((240, 240, 240))
else:
screen
.blit
(bg
, (0, 0))
if game_over
:
Text_start
= font1
.render
('点击游戏', True, (0, 0, 0))
screen
.blit
(Text_start
, (200, 200))
else:
ball_y
= ball_y
+ 10
if ball_y
> screen_height
:
ball_y
= 0
ball_x
= random
.randint
(0, 600)
lives
= lives
-1
if lives
== 0:
game_over
= True
score
= 0
lives
= 30
if score
<10:
ball
(ball_x
, ball_y
)
elif score
< 20:
screen
.blit
(ball1
,(ball_x
, ball_y
))
else:
screen
.blit
(ball2
, (ball_x
, ball_y
))
if rect_x
< ball_x
< rect_x
+ rect_w
and rect_y
< ball_y
< rect_y
+ rect_h
:
hit
.play
()
score
= score
+ 1
ball_y
= 0
ball_x
= random
.randint
(0, 600)
Text_score
= font1
.render
('分数:%d' % score
, True, (0, 0, 0))
screen
.blit
(Text_score
, (0, 0))
Text_lives
= font1
.render
('生命值:%d' % lives
, True, (0, 0, 0))
screen
.blit
(Text_lives
, (screen_width
-100, 0))
pygame
.draw
.rect
(screen
, (100, 200, 30), (rect_x
, rect_y
, rect_w
, rect_h
), 0)
pygame
.display
.update
()
pygame
.quit
()
转载请注明原文地址: https://mac.8miu.com/read-512716.html