Commit 2995fc28 by BellCodeEditor

auto save

parent fb010fc2
Showing with 68 additions and 74 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import os
# 初始化pygame,为使用硬件做准备 pygame.init() # 初始化
pygame.init() # 创建窗口
screen = pygame.display.set_mode((640, 480))
# 载入图片、资源
bg_img = pygame.image.load('background.png') # 背景图
play_img = pygame.image.load('play.png') # 播放按钮
stop_img = pygame.image.load('stop.png') # 暂停按钮
last_img = pygame.image.load('last.png') # 上一曲按钮
next_img = pygame.image.load('next.png') # 下一曲按钮
logo_img = pygame.image.load('logo.png') # 下一曲按钮
# 创建一个窗口 # 音乐
screen = pygame.display.set_mode((660, 480)) music_list = []
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) path = "C:\\Users\\xingxing\\Desktop\\1"
filelist = os.listdir(path)
for i in filelist:
if i[-4:] == ".ogg" or i[-4:] == ".wav":
music_list.append(i)
# 背景 num = -1
background = pygame.image.load('bg.png') volume = 0.2
right = pygame.image.load('right.png') # 头 朝右 pygame.mixer.music.set_volume(volume) # 初始播放音量
food = pygame.image.load('apple.png') # 食物 苹果 click = 0
body = pygame.image.load('body.png') # 身体 play_button = stop_img
left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下
my_font = pygame.font.Font('neuropol.ttf', 18) # 字体
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
apple_x = 360 # 苹果横坐标
apple_y = 300 # 苹果纵坐标
setheading = "right"
snake_head = right
score = 0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit() exit()
# 按键,控制声音大小
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if event.key == locals.K_RIGHT and setheading != "left": if event.key == locals.K_UP:
setheading = 'right' volume += 0.1
snake_head = right if volume > 1:
if event.key == locals.K_LEFT and setheading != "right": volume = 1
setheading = 'left' pygame.mixer.music.set_volume(volume)
snake_head = left if event.key == locals.K_DOWN:
if event.key == locals.K_UP and setheading != "down": volume -= 0.1
setheading = 'up' if volume < 0:
snake_head = up volume = 0
if event.key == locals.K_DOWN and setheading != "up": pygame.mixer.music.set_volume(volume)
setheading = 'down'
snake_head = down
# 设置贪吃蛇的头部坐标 if event.type == locals.MOUSEBUTTONDOWN:
if setheading == "right": if event.button == 1: # 左击
x += 30 x, y = event.pos
elif setheading == "left": if x > 270 and x < 370 and y > 350 and y < 450:
x -= 30 click += 1
elif setheading == "up": if click % 2 == 0:
y -= 30 play_button = stop_img
else: pygame.mixer.music.unpause()
y += 30 else:
position.append((x, y)) play_button = play_img
# 贪吃蛇吃到了苹果 pygame.mixer.music.pause()
if x == apple_x and y == apple_y: if x > 120 and x < 220 and y > 350 and y < 400: #上一曲
num1= random.randint(1, 22) # 横排第几个格子 num -= 1
num2 = random.randint(1, 16) # 竖排第几个格子 if num < 0:
apple_x = 30*num1-30 # 苹果的x坐标 num = len(music_list) - 1
apple_y = 30*num2-30 # 苹果的y坐标 pygame.mixer.music.load(path +"\\" +music_list[num])
else: pygame.mixer.music.play()
position.pop(0) play_button = stop_img
# 贪吃蛇撞墙 click = 0
if x < 0 or x > 630 or y < 0 or y > 450:
exit()
# 将背景图画上去
screen.blit(background, (0, 0))
# 将贪吃蛇的头画上去
screen.blit(snake_head, position[-1])
# 将贪吃蛇的身体画上去
for i in range(len(position)-1):
screen.blit(body, position[i])
# 将果实画上去 if pygame.mixer.music.get_busy() == False:
screen.blit(food, (apple_x, apple_y)) num += 1
# 绘制分数 if num > len(filelist)-1:
info = "Score:"+ str(score) num = 0
text = my_font.render(info, True, (0, 0, 0)) print(num)
screen.blit(text, (540, 10)) pygame.mixer.music.load(path + "\\"+music_list[num])
pygame.mixer.music.play()
# 绘制画面
screen.blit(bg_img, (0, 0)) # 填充背景
screen.blit(play_button, (270, 330)) # 暂停按钮
screen.blit(logo_img, (170, 60)) # 中间logo图
screen.blit(last_img, (120, 350)) # 上一曲
screen.blit(next_img, (420, 350)) # 下一曲
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPSCLOCK.tick(3) \ No newline at end of file
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment