import pygame
import time
import random
# 游戏初始化
pygame.init()
# 设置窗口大小 640长度 窗口宽度500
screen = pygame.display.set_mode((640, 500))
# 设置窗口标题
pygame.display.set_caption("坦福版接球游戏")
# 文字1. 设置文字字体和大小
font = pygame.font.SysFont("SimHei", 35)
a = 200
b = 200
board_x = 400
board_y = 320
speedy = 1
speedx = 1
score = 0
game_over = True
r, g, bl = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
# 图片1。导入图片
background = pygame.image.load('black.jpeg')
ball = pygame.image.load('ball.png')
# 音乐
pygame.mixer.init() # 声音初始化
pygame.mixer.music.load("xxx.mp3") # 加载图片
pygame.mixer.music.set_volume(0.3) # 设置声音
pygame.mixer.music.play(-1) # 循环播放
# 音效
hit = pygame.mixer.Sound("hit.wav")
hit.set_volume(0.3)
while 1 < 100:
# 事件:我们对电脑的物理操作,按键,鼠标(按键,移动)
for event in pygame.event.get():
# print(event)
if event.type == pygame.QUIT:
pygame.quit() # 退出游戏
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
board_x = board_x - 10
if event.key == pygame.K_d:
board_x = board_x + 10
elif event.type == pygame.MOUSEMOTION:
board_x,board_y = event.pos
elif event.type == pygame.MOUSEBUTTONUP:
if game_over:
game_over = False
score = 0
a,b = 0,0
t1 = time.time()
screen.fill((0, 0, 0))
if game_over:
# 2.设置文字的内容和颜色
font3 = font.render("点击鼠标开始游戏", True, (0, 255, 0))
# 3.把文字放在窗口上
screen.blit(font3, (100, 100))
else:
# 图片2
screen.blit(background,(0,0))
b = b + speedy
a = a + speedx
if a > 640:
speedx = -1
elif a< 0:
speedx = 1
if b > 640:
b = 0
if b < 0:
r,g,bl =random.randint(0,255),random.randint(0,255),random.randint(0,255)
speedy = 1
if board_x< a<board_x + 200 and board_y < b < board_y+10:
score = score +1
board_y = b - 1
speedy = -1
hit.play()
# 画小球:屏幕,rgb三基色,坐标,半径
# pygame.draw.circle(screen,(r,g,bl),(a,b), 23)
screen.blit(ball, (a,b))
# time.sleep(1)
# 画一个长方形 pygame.draw.rect(屏幕,(r,g,b),(经度,维度,板长,板宽))
pygame.draw.rect(screen,(105,105,150),(board_x,board_y,200,10))
# 2.设置文字的内容和颜色
font2 = font.render("分数:%d"%score, True, (0,255,0))
# 3.把文字放在窗口上
screen.blit(font2, (100,100))
t2 = time.time()
djs = 30 - (t2-t1)
# 2.设置文字的内容和颜色
font4 = font.render("时间:%d"%djs, True, (0,255,0))
# 3.把文字放在窗口上
screen.blit(font4, (100,200))
if djs < 0:
game_over = True
# 刷新
pygame.display.update()
``
aden - 接小球游戏4.0
最新推荐文章于 2020-07-14 17:41:56 发布