|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
food模块
import pygame
from random import *
class Food:
def __init__(self,bg_size,food_length=20):
self.width,self.height=bg_size[0],bg_size[1]
self.x,self.y=randint(0,self.width//food_length),\
randint(0,self.height//food_length)
self.active=True
def reset(self):
self.active=True
self.x,self.y=randint(0,self.width//food_length),\
randint(0,self.height//food_length)
snake模块
import pygame
from random import *
UP=0
DOWN=1
LEFT=2
RIGHT=3
class Snake:
def __init__(self,bg_size,x=10,y=10):
self.width,self.height=bg_size[0],bg_size[1]
self.body=[]
#初始化蛇头在中间的位置
self.body.insert(0,[x,y])
#默认蛇的初始移动方向向右
self.direction=UP
self.eats=False
self.life=True
def move(self):
#始终在蛇头增加方块,然后再删除最后的方块
#比如向上移动,再原来的蛇头上方为新蛇头,然后再删除原来的蛇尾,从而实现了整体的移动
#是否选择删除由main里面是否吃了食物决定,没有吃就删除,吃了的话正好加了一块,不需要删除
if self.direction==UP:
self.body.insert(0,[self.body[0][0],self.body[0][1]-1])
if self.direction==DOWN:
self.body.insert(0,[self.body[0][0],self.body[0][1]+1])
if self.direction==LEFT:
self.body.insert(0,[self.body[0][0]-1,self.body[0][1]])
if self.direction==RIGHT:
self.body.insert(0,[self.body[0][0]+1,self.body[0][1]])
主程序
import pygame
import sys
import traceback
import snake
import food
from pygame.locals import *
from random import *
UP=0
DOWN=1
LEFT=2
RIGHT=3
pygame.init()
pygame.mixer.init()
bg_size=width,height=400,400
cell_length=20
screen=pygame.display.set_mode(bg_size)
pygame.display.set_caption('贪吃蛇')
background=pygame.image.load('images/background.png').convert_alpha()
#载入游戏所需要的音乐
pygame.mixer.music.load('sound/game_music.mp3')
pygame.mixer.music.set_volume(0.2)
get_food_sound=pygame.mixer.Sound('sound/get_food.wav')
get_food_sound.set_volume(0.2)
snake_die_sound=pygame.mixer.Sound('sound/snake_die.wav')
snake_die_sound.set_volume(0.2)
RED=(255,0,0)
WHITE=(255,255,255)
BLACK=(0,0,0)
def main():
pygame.mixer.music.play(-1)
clock=pygame.time.Clock()
running=True
#生成食物
foods=food.Food(bg_size)
#生成贪吃蛇
snakes=snake.Snake(bg_size)
while running:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
key_pressed=pygame.key.get_pressed()
if (key_pressed[K_w] or key_pressed[K_UP]) and snakes.direction!=DOWN:
snakes.direction=UP
if (key_pressed[K_s] or key_pressed[K_DOWN]) and snakes.direction!=UP :
snakes.direction=DOWN
if (key_pressed[K_a] or key_pressed[K_LEFT]) and snakes.direction!=RIGHT:
snakes.direction=LEFT
if (key_pressed[K_d] or key_pressed[K_RIGHT]) and snakes.direction!=LEFT:
snakes.direction=RIGHT
#绘制食物
if foods.active==True:
pygame.draw.rect(screen,RED,(foods.x*cell_length,foods.y*cell_length,cell_length,cell_length),0)
pygame.display.update()
else:
foods.reset()
#绘制贪吃蛇
for each in snakes.body:
pygame.draw.rect(screen,BLACK,(each[0]*cell_length,each[1]*cell_length,cell_length,cell_length),0)
pygame.display.update()
#吃食物
if snakes.body[0]==[foods.x,foods.y]:
foods.active=False
snakes.eats=True
get_food_sound.play()
#蛇的移动
if snakes.life==True:
if snakes.eats==True:
snakes.move()
snakes.eats=False
else:
snakes.move()
snakes.body.pop()
#判断死亡情况
#碰到边界
if (snakes.body[0][0]==0) or (snakes.body[0][0]==bg_size[0]//cell_length) or (snakes.body[0][1]==0) or (snakes.body[0][1]==bg_size[1]//cell_length):
snakes.life=False
#碰到自身
i_index=0
j_index=0
if len(snakes.body)!=1:
for i in snakes.body:
i_index+=1
j_index=0
for j in snakes.body:
j_index+=1
if (i==j) and (i_index!=j_index):
snakes.life=False
#结束游戏
if snakes.life==False:
snake_die_sound.play()
pygame.mixer.music.stop()
print('Game over')
screen.blit(background,(0,0))
pygame.display.update()
pygame.display.flip()
clock.tick(60)
if __name__=='__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
问题:一运行蛇头方块就位于边界,疯狂打压game over ,我怀疑是因为刚开始蛇头的移动速度太快了,求大神解决方法,具体怎么写
|
|