Commit b1f75d1c by BellCodeEditor

save project

parent c7b1ad07
Showing with 63 additions and 50 deletions
import pygame import pygame
from pygame import locals from pygame import locals
pygame.init() # 初始化 # 初始化pygame,为使用硬件做准备
# 创建窗口 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') # 中间logo
pygame.mixer.music.load('歌曲4.ogg')
while True: # 创建一个窗口
for event in pygame.event.get(): screen = pygame.display.set_mode((660, 480))
if event.type == locals.QUIT: FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
exit()
volume = 0.2 # 背景
pygame.mixer.music.set_volume(volume) background = pygame.image.load('bg.png')
right = pygame.image.load('right.png') # 头 朝右
food = pygame.image.load('apple.png') # 食物 苹果
body = pygame.image.load('body.png') # 身体
left = pygame.image.load('left.png') # 头 朝左
up = pygame.image.load('up.png') # 头 朝上
down = pygame.image.load('down.png') # 头 朝下
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
apple_x = 360 # 苹果横坐标
apple_y = 300 # 苹果纵坐标
setheading = "right"
snake_head = right
start = stop_img
click = 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_UP: if event.key == locals.K_RIGHT and setheading != "left":
volume += 0.1 setheading = 'right'
if volume < 0 or volume==0: snake_head = right
volume = 0 if event.key == locals.K_LEFT and setheading != "right":
pygame.mixer.music.set_volume(volume) setheading = 'left'
if event.key == locals.K_DOWN: snake_head = left
volume -= 0.1 if event.key == locals.K_UP and setheading != "down":
if volume < 0 <or 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:
pygame.mixer.music.unpause() # 设置贪吃蛇的头部坐标
start = stop_img if setheading == "right":
x += 30
elif setheading == "left":
x -= 30
elif setheading == "up":
y -= 30
else: else:
pygame.mixer.music.pause() y += 30
start = play_img position.append((x, y))
# 贪吃蛇吃到了苹果
if volume <=0: if x == apple_x and y == apple_y:
volume = 0 apple_x = random.randint(0, 660)
if pygame.mixer.music.get_busy() == False: apple_y = random.randint(0, 480)
pygame.mixer.music.play()
if event.button ==1: position.pop(0)
x,y=event.pos # 将背景图画上去
if x > 270 and x < 370 and y > 350 and y < 450: screen.blit(background, (0, 0))
# 绘制画面 # 将贪吃蛇的头画上去
screen.blit(bg_img, (0, 0)) # 填充背景 screen.blit(snake_head, position[-1])
screen.blit(start, (270, 330)) # 暂停按钮 # 将贪吃蛇的身体画上去
screen.blit(logo_img, (170, 60)) # 中间logo图 for i in range(len(position)-1):
screen.blit(last_img, (120, 350)) # 上一曲 screen.blit(body, position[i])
screen.blit(next_img, (420, 350)) # 下一曲
# 将果实画上去
screen.blit(food, (360, 300))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
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