资源简介

音乐,图像,tk开始界面的插入,满足作业或者自己练习的需要。定义函数,如蛇身食物等,之后进入main主函数中,图片的插入pygame.image.load,音乐的插入分为两种,一种为MP3格式的音频的插入pygame.mixer.music.load,另一种为WAV格式的插入pygame.mixer.Sound。使用if fdrect.collidepoint(pos):来判断是否发生蛇头与食物的碰撞。使用if来实现游戏的重开和按键退出。在tkinter中运用到了label和button实现图片得插入与按钮的插入。(text.render(),())来完成文字的插入。

资源截图

代码片段和文件信息

import pygame
import random
import copy
import tkinter
from tkinter import *
import tkinter as tk
class snake:
    def __init__(self):
        “““
        init the snake
        “““
        self.poslist = [[100100]]
    def position(self):
        “““
        return the all of the snake‘s point
        “““
        return self.poslist
    def gowhere(selfwhere):
        “““
        change the snake‘s point to control the snake‘s moving direction
        “““
        count = len(self.poslist)
        pos = count-1
        while pos > 0:
            self.poslist[pos] = copy.deepcopy(self.poslist[pos-1])
            pos -= 1
        if where is ‘U‘:
            self.poslist[pos][1] -= 10
            if self.poslist[pos][1] < 10:
                
                self.poslist[pos][1] = 440
        if where is ‘D‘:
            self.poslist[pos][1] += 10
            if self.poslist[pos][1] > 440:
                self.poslist[pos][1] = 10
        if where is ‘L‘:
            self.poslist[pos][0] -= 10
            if self.poslist[pos][0] < 10:
                self.poslist[pos][0] = 790
        if where is ‘R‘:
            self.poslist[pos][0] += 10
            if self.poslist[pos][0] > 790:
                self.poslist[pos][0] = 10
    def eatfood(selffoodpoint):
        “““
        eat the food and add point to snake
        “““
        self.poslist.append(foodpoint)

class food:
    def __init__(self):
        “““
        init the food‘s point
        “““
        self.x = random.randint(10790)
        self.y = random.randint(10440)
    def display(self):
        “““
        init the food‘s point and return the point
        “““
        self.x = random.randint(10790)
        self.y = random.randint(10440)
        return self.position()
    def position(self):
        “““
        return the food‘s point
        “““
        return [self.xself.y]




def main():
    moveup = False
    movedown = False
    moveleft = False
    moveright = True
    pygame.init()
    clock = pygame.time.Clock()
    width = 800
    height = 450
    i=0
    text = pygame.font.SysFont(“arial“ 36)
    screen = pygame.display.set_mode([widthheight])
    bgp=pygame.image.load(‘timg.jpg‘).convert()
    restart = True
    #音乐的插入
    pygame.mixer.init()
    eat=pygame.mixer.Sound(‘7780.wav‘)
    eat.set_volume(1)
    gameover1=pygame.mixer.Sound(‘gameover1.wav‘)
    gameover1.set_volume(1)
    pygame.mixer.music.load(‘bgm.mp3‘)
    pygame.mixer.music.play(-10)
    pygame.mixer.music.set_volume(0.5) 
    while restart:
        sk = snake()
        fd = food()
        screentitle = pygame.display.set_caption(“eat snake“)
        sk.gowhere(‘R‘)
        running = True
        while running:
            # fill the background is white
            screen.blit(bgp(00))
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    exit(0)
                # judge the down key
                if event.t

评论

共有 条评论