乐愚社区Beta

 编程语言  >  pygame05.03 —— 贪吃蛇游戏新方法分享

pygame05.03 —— 贪吃蛇游戏新方法分享

原来是稀琳  L7  • 2024-10-17 • 回复 1 • 只看楼主举报    

这里我就不多说了,直接给出代码,想提升的自己可以学习一下。

代码展示:

import pygame 
import sys 
import random 

#节省内存
game_clock = pygame.time.Clock()
game_speed =  40

# 初始化Pygame 
pygame.init()  
 
# 设置窗口大小 
width, height = 640, 480 
screen = pygame.display.set_mode((width,  height)) 
pygame.display.set_caption(' 贪吃蛇游戏') 
 
# 定义颜色 
white = (255, 255, 255) 
black = (0, 0, 0) 
red = (255, 0, 0) 
 
# 定义贪吃蛇和食物的尺寸 
block_size = 20
class Snake: 
    def __init__(self): 
        self.size  = 1 
        self.elements  = [[100, 100]]  # 初始位置 
        self.direction  = 'RIGHT' 
        self.score  = 0 
 
    def draw(self): 
        for element in self.elements:  
            pygame.draw.rect(screen,  black, [element[0], element[1], block_size, block_size]) 
 
    def move(self): 
        if self.direction  == 'RIGHT': 
            head = [self.elements[0][0] + block_size, self.elements[0][1]]  
        elif self.direction  == 'LEFT': 
            head = [self.elements[0][0] - block_size, self.elements[0][1]]  
        elif self.direction  == 'UP': 
            head = [self.elements[0][0], self.elements[0][1]  - block_size] 
        elif self.direction  == 'DOWN': 
            head = [self.elements[0][0], self.elements[0][1]  + block_size] 
        self.elements.insert(0,  head) 
        if len(self.elements)  > self.size:  
            self.elements.pop()  
 
class Food: 
    def __init__(self): 
        self.position  = [random.randrange(1, (width//block_size)) * block_size, random.randrange(1,  (height//block_size)) * block_size] 
        self.is_food_on_screen  = True 
 
    def draw(self): 
        pygame.draw.rect(screen,  red, [self.position[0], self.position[1],  block_size, block_size])
        
def game_over(): 
    font = pygame.font.SysFont(None,  50) 
    text = font.render('Game  Over', True, black) 
    screen.blit(text,  [width/2 - 100, height/2 - 25]) 
    pygame.display.update()  
    pygame.time.wait(2000)  
    pygame.quit()  
    sys.exit()  
 
def main(): 
    snake = Snake() 
    food = Food() 
    clock = pygame.time.Clock()  
 
    while True: 
        for event in pygame.event.get():  
            if event.type  == pygame.QUIT: 
                pygame.quit()  
                sys.exit()  
            if event.type  == pygame.KEYDOWN: 
                if event.key  == pygame.K_RIGHT: 
                    snake.direction  = 'RIGHT' 
                if event.key  == pygame.K_LEFT: 
                    snake.direction  = 'LEFT' 
                if event.key  == pygame.K_UP: 
                    snake.direction  = 'UP' 
                if event.key  == pygame.K_DOWN: 
                    snake.direction  = 'DOWN'

def main(): 
    snake = Snake() 
    food = Food() 
    clock = pygame.time.Clock()  
 
    while True: 
        for event in pygame.event.get():  
            if event.type  == pygame.QUIT: 
                pygame.quit()  
                sys.exit()  
            if event.type  == pygame.KEYDOWN: 
                if event.key  == pygame.K_RIGHT and snake.direction  != 'LEFT': 
                    snake.direction  = 'RIGHT' 
                if event.key  == pygame.K_LEFT and snake.direction  != 'RIGHT': 
                    snake.direction  = 'LEFT' 
                if event.key  == pygame.K_UP and snake.direction  != 'DOWN': 
                    snake.direction  = 'UP' 
                if event.key  == pygame.K_DOWN and snake.direction  != 'UP': 
                    snake.direction  = 'DOWN' 
 
        snake.move()  
 
        # 检查是否吃到食物 
        if snake.elements[0][0]  == food.position[0]  and snake.elements[0][1]  == food.position[1]:  
            snake.size  += 1 
            snake.score  += 1 
            food.is_food_on_screen  = False 
 
        # 生成新的食物 
        if not food.is_food_on_screen:  
            food.position  = [random.randrange(1, (width//block_size)) * block_size, random.randrange(1,  (height//block_size)) * block_size] 
            food.is_food_on_screen  = True 
 
        # 检查是否撞墙或撞到自己 
        if snake.elements[0][0]  >= width or snake.elements[0][0]  < 0 or snake.elements[0][1]  >= height or snake.elements[0][1]  < 0: 
            game_over() 
        for block in snake.elements[1:]:  
            if snake.elements[0][0]  == block[0] and snake.elements[0][1]  == block[1]: 
                game_over() 
 
        # 绘制背景和元素 
        screen.fill(white)  
        snake.draw()  
        food.draw()  
        pygame.display.update()  
        clock.tick(8)   # 控制游戏速度
        
if __name__ == '__main__': 
    main()

 

与君共勉,下期再见


1条回帖
原来是稀琳 楼主  L7  评论于
(0)  回复(0) 1#
俄罗斯方块这个游戏好难写啊!
现在觉得贪吃蛇游戏还是太简单了!
还没注册帐号?快来注册社区帐号,和我们一起嗨起来!
关于本社区

集各类兴趣爱好于一身的轻量化交流社区,在此您可以和他人一起分享交流您觉得有价值的内容,社区鼓励大家发表原创内容,为社区添砖加瓦!

发帖奖励 → 社区版规 → 招聘版主 →
推荐版块
扫描二维码下载社区APP
回到顶部