Commit 788533f9 by BellCodeEditor

auto save

parent 0c429cae
Showing with 79 additions and 49 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
pygame.init() # 初始化 # 初始化pygame,为使用硬件做准备
# 创建窗口 pygame.init()
screen = pygame.display.set_mode((640, 480))
# 载入图片、资源 # 创建一个窗口
bg_img = pygame.image.load('background.png') # 背景图 screen = pygame.display.set_mode((660, 480))
play_img = pygame.image.load('play.png') # 播放按钮 FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
stop_img = pygame.image.load('stop.png') # 暂停按钮
last_img = pygame.image.load('last.png') # 上一曲按钮 # 背景
next_img = pygame.image.load('next.png') # 下一曲按钮 background = pygame.image.load('bg.png')
logo_img = pygame.image.load('logo.png') # 下一曲按钮 right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果
# 载入音乐 body = pygame.image.load('body.png') # 身体
pygame.mixer.music.load('歌曲3.wav') # 载入音乐 left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
volume = 0.2 down = pygame.image.load('down.png') # 头 朝下
pygame.mixer.music.set_volume(volume) # 初始播放音量 my_font = pygame.font.Font('neuropol.ttf', 18) # 字体
click = 0
play_button = stop_img 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_w: if event.key == locals.K_RIGHT and setheading != "left":
volume += 0.1 setheading = 'right'
if volume > 1: snake_head = right
volume = 1 if event.key == locals.K_LEFT and setheading != "right":
pygame.mixer.music.set_volume(volume) setheading = 'left'
if event.key == locals.K_s: snake_head = left
volume -= 0.1 if event.key == locals.K_UP and setheading != "down":
if volume < 0: setheading = 'up'
volume = 0 snake_head = up
pygame.mixer.music.set_volume(volume) if event.key == locals.K_DOWN and setheading != "up":
if event.type == locals.MOUSEBUTTONDOWN: setheading = 'down'
click += 1 snake_head = down
if click % 2 == 0:
play_button = stop_img # 设置贪吃蛇的头部坐标
pygame.mixer.music.unpause() if setheading == "right":
else: x += 30
play_button = play_img elif setheading == "left":
pygame.mixer.music.pause() x -= 30
elif setheading == "up":
if pygame.mixer.music.get_busy() == False: y -= 30
pygame.mixer.music.play() else:
y += 30
# 绘制画面 position.append((x, y))
screen.blit(bg_img, (0, 0)) # 填充背景 # 贪吃蛇吃到了苹果
screen.blit(play_button, (270, 330)) # 暂停按钮 if x == apple_x and y == apple_y:
screen.blit(logo_img, (170, 60)) # 中间logo图 num1= random.randint(1, 22) # 横排第几个格子
screen.blit(last_img, (120, 350)) # 上一曲 num2 = random.randint(1, 16) # 竖排第几个格子
screen.blit(next_img, (420, 350)) # 下一曲 apple_x = 30*num1-30 # 苹果的x坐标
apple_y = 30*num2-30 # 苹果的y坐标
else:
position.pop(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])
# 将果实画上去
screen.blit(food, (apple_x, apple_y))
# 绘制分数
info = "Score:"+ str(score)
text = my_font.render(info, True, (0, 0, 0))
screen.blit(text, (540, 10))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
\ No newline at end of file FPSCLOCK.tick(3)
\ 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