Commit 6d64c20e by BellCodeEditor

save project

parent 9667d8f9
last.png

3.79 KB

logo.png

40.9 KB

import pygame
from pygame import locals
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')
pygame.mixer.music.load("歌曲4.ogg")
volume=0.2
click=0
play_button=stop_img
while True:
for event in pygame.event.get():
if event.type==locals.QUIT:
exit()
if event.type==locals.KEYDOWN:
if event.key==locals.K_UP:
volume+=0.1
if volume>1:
volume=1
if event.key==locals.K_DOWN:
volume-=0.1
if volume<0:
volume=0
if event.type==locals.MOUSEBUTTONDOWN:
click+=1
if click%2==0:
play_button=stop_img
pygame.mixer.music.unpause()
else:
play_button=play_img
pygame.mixer.music.pause()
pygame.mixer.music.set_volume(volume)
if pygame.mixer.music.get_busy()==False:
pygame.mixer.music.play()
screen.blit(bg_img, (0, 0))
screen.blit(play_button, (270, 330))
screen.blit(logo_img, (170, 60))
screen.blit(last_img, (120, 350))
screen.blit(next_img, (420, 350))
pygame.display.update()
next.png

3.87 KB

play.png

12.1 KB

import pygame import pygame
from pygame import locals from pygame import locals
import random pygame.init()
# 初始化pygame,为使用硬件做准备 screen = pygame.display.set_mode((640, 480))
pygame.init() bg_img = pygame.image.load('background.png')
play_img = pygame.image.load('play.png')
# 创建一个窗口 stop_img = pygame.image.load('stop.png')
screen = pygame.display.set_mode((660, 480)) last_img = pygame.image.load('last.png')
FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) next_img = pygame.image.load('next.png')
logo_img = pygame.image.load('logo.png')
# 背景 pygame.mixer.music.load("歌曲4.ogg")
background = pygame.image.load('bg.png') volume=0.2
right = pygame.image.load('right.png') # 头 朝右 click=0
food = pygame.image.load('apple.png') # 食物 苹果 play_button=stop_img
body = pygame.image.load('body.png') # 身体
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
apple_x,apple_y=360,300
position = [(180, 90), (180, 120), (210, 120), (x, y)]
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' if event.key==locals.K_DOWN:
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 if event.type==locals.MOUSEBUTTONDOWN:
if event.key == locals.K_DOWN and setheading != "up": if event.button==1:
setheading = 'down' x,y=event.pos
snake_head = down if x>270 and x<370 and y>350 and y<450:
click+=1
# 设置贪吃蛇的头部坐标 if click%2==0:
if setheading == "right": play_button=stop_img
x += 30 pygame.mixer.music.unpause()
elif setheading == "left": else:
x -= 30 play_button=play_img
elif setheading == "up": pygame.mixer.music.pause()
y -= 30 pygame.mixer.music.set_volume(volume)
else: if pygame.mixer.music.get_busy()==False:
y += 30 pygame.mixer.music.play()
position.append((x, y)) screen.blit(bg_img, (0, 0))
if x==apple_x and y==apple_y: screen.blit(play_button, (270, 330))
apple_x=random.randint(1,22) screen.blit(logo_img, (170, 60))
apple_y=random.randint(1,16) screen.blit(last_img, (120, 350))
apple_x=apple_x*30-30 screen.blit(next_img, (420, 350))
apple_y=apple_y*30-30
score+=10
else:
position.pop(0)
if x<0 or y<0 or x>630 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()
FPSCLOCK.tick(3)
\ No newline at end of file
stop.png

11.6 KB

File added
File added
File added
File added
File added
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