资源简介
音乐,图像,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试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
- 一个简单的python爬虫
- csv文件行列转换python实现代码
- Python操作Mysql教程手册
- Python Machine Learning Case Studies
- python获取硬件信息
评论
共有 条评论