资源简介
音乐,图像,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
相关资源
- python实现多项式加减乘除
- zw_基于python的网络爬虫设计.zip
- 简单的好友通讯录管理程序
- 将文件字母小写变大写并复制到其他
- Iterative Bagging和MultiBoosting 算法python实
- 租房API调用
- python 滑雪小游戏
- 5层神经网络带L2正则化的损失函数计
- 查找两幅图片中的不同
- python标准库中文版.tar
- python多人在线聊天室
- Python3 实现SM3国产哈希算法
- python爬取微博热搜并直接写入mysql.r
- Python-Scrapy 入门级爬虫项目实战
- 用python编写的信息管理系统的增删改
- 用python编写的饮料自动购物机
- [计算方法作业]利用python中matplotlib实
- python电子书大全.txt
- python用Django实现简单的web版学生信息
- Python-从Python高效处理FASTQ文件
- Python-机器学习完全课程
- Python-利用flask搭建的一个简单的个人
- Python-利用Python实现中文文本关键词抽
- Python-一个WindowsLinux和Mac的简单键盘记
- Python-Glyce用于汉字表示的字形向量
- paillier 纯 python Paillier同态密码.zip
- fancyimpute 在 python 中,实现了多元插值
- 利用pythonscihub成文献为PDF操作
- 详解python实现交叉验证法与留出法
- 用python做一个搜索引擎(Pylucene)的代
评论
共有 条评论