Commit 0e1d69eb by BellCodeEditor

save project

parent d24acbc9
Showing with 43 additions and 58 deletions
import pygame import pygame
from pygame import locals from pygame import locals
# 初始化pygame,为使用硬件做准备 pygame.init() # 初始化
pygame.init() # 创建窗口
screen = pygame.display.set_mode((640, 480))
# 创建一个窗口 # 载入图片、资源
screen = pygame.display.set_mode((660, 480)) bg_img = pygame.image.load('background.png') # 背景图
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) play_img = pygame.image.load('play.png') # 播放按钮
stop_img = pygame.image.load('stop.png') # 暂停按钮
# 背景 last_img = pygame.image.load('last.png') # 上一曲按钮
background = pygame.image.load('bg.png') next_img = pygame.image.load('next.png') # 下一曲按钮
right = pygame.image.load('right.png') # 头 朝右 logo_img = pygame.image.load('logo.png') # 中间logo
food = pygame.image.load('apple.png') # 食物 苹果 pygame.mixer.music.load('歌曲3.wav')
body = pygame.image.load('body.png') # 身体 volume=0.1
left = pygame.image.load('left.png') # 头 朝左 click=0
up = pygame.image.load('up.png') # 头 朝上 a=stop_img
down = pygame.image.load('down.png') # 头 朝下 pygame.mixer.music.set_volume(volume)
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
setheading = "right"
snake_head = right
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' if event.type==locals.MOUSEBUTTONDOWN:
snake_head = down click+=1
# 设置贪吃蛇的头部坐标 if click%2==0:
if setheading == "right": pygame.mixer_music.unpause()
x += 30 a=stop_img
elif setheading == "left":
x -= 30
elif setheading == "up":
y -= 30
else: else:
y += 30 pygame.mixer_music.pause()
position.append((x, y)) a=play_img
position.pop(0) if pygame.mixer.music.get_busy()==False:
# 将背景图画上去 pygame.mixer.music.play()
screen.blit(background, (0, 0)) # 绘制画面
# 将贪吃蛇的头画上去 screen.blit(bg_img, (0, 0)) # 填充背景
screen.blit(snake_head, position[-1]) screen.blit(a, (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()
\ 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