用户:
littleyuan查看:3 回复:1 评论:3 创建时间:2022-07-31T20:53:42
【作品展示】

【作品介绍】
好玩的游戏
【作品源代码】
import pygame, sys, random, time
from pygame.locals import *
# 初始化pygame环境
pygame.init()
# 创建一个长宽分别为820/410窗口
canvas = pygame.display.set_mode((820, 410))
canvas.fill((255, 255, 255))
pygame.display.set_caption("全民小游戏")
#加载图片
bg2 = pygame.image.load("images/bg2.jpg")
pygame.mixer.init()
pygame.mixer.music.load('music/start.mp3')
pygame.mixer.music.play(-1)
now = 0
def fjdz():
import pygame, sys, random, time, easygui
# 初始化pygame环境
pygame.init()
# 创建一个长宽分别为480/650窗口
canvas = pygame.display.set_mode((480, 650))
canvas.fill((255, 255, 255))
# 设置窗口标题
pygame.display.set_caption("飞机大战")
bg = pygame.image.load("images/bg1.png")
enemy1 = pygame.image.load("images/enemy1.png")
enemy2 = pygame.image.load("images/enemy2.png")
enemy3 = pygame.image.load("images/enemy3.png")
b = pygame.image.load("images/bullet1.png")
h = pygame.image.load("images/hero.png")
#开始游戏图片
startgame=pygame.image.load("images/startGame.png")
#logo图片
logo=pygame.image.load("images/LOGO.png")
#暂停图片
pause = pygame.image.load("images/game_pause_nor.png")
# 添加时间间隔的方法
def isActionTime(lastTime, interval):
if lastTime == 0:
return True
currentTime = time.time()
return currentTime - lastTime >= interval
# 定义Sky类
class Sky():
def __init__(self):
self.width = 480
self.height = 852
self.img = bg
self.x1 = 0
self.y1 = 0
self.x2 = 0
self.y2 = -self.height
# 创建paint方法
def paint(self):
canvas.blit(self.img, (self.x1, self.y1))
canvas.blit(self.img, (self.x2, self.y2))
# 创建step方法
def step(self):
self.y1 = self.y1 + 1
self.y2 = self.y2 + 1
if self.y1 > self.height:
self.y1 = -self.height
if self.y2 > self.height:
self.y2 = -self.height
# 定义父类FlyingObject
class FlyingObject(object):
def __init__(self, x, y, width, height, life, img):
self.x = x
self.y = y
self.width = width
self.height = height
self.life = life
self.img = img
# 敌飞机移动的时间间隔
self.lastTime = 0
self.interval = 0.01
# 添加删除属性
self.canDelete = False
# 定义paint方法
def paint(self):
canvas.blit(self.img, (self.x, self.y))
# 定义step方法
def step(self):
# 判断是否到了移动的时间间隔
if not isActionTime(self.lastTime, self.interval):
return
self.lastTime = time.time()
# 控制移动速度
self.y = self.y + 2
# 定义hit方法判断两个对象之间是否发生碰撞
def hit(self, component):
c = component
return c.x > self.x - c.width and c.x < self.x + self.width and \
c.y > self.y - c.height and c.y < self.y + self.height
# 定义喵方法处理对象之间碰撞后的处理
def 喵(self, 喵sign):
# 敌机和英雄机碰撞之后的处理
if 喵sign:
if hasattr(self, 'score'):
GameVar.score += self.score
if 喵sign == 2:
self.life -= 1
# 设置删除属性为True
self.canDelete = True
# 敌机和子弹碰撞之后的处理
else:
self.life -= 1
if self.life == 0:
# 设置删除属性为True
self.canDelete = True
if hasattr(self, 'score'):
GameVar.score += self.score
# 定义outOfBounds方法判断对象是否越界
def outOfBounds(self):
return self.y > 650
# 重构Enemy类
class Enemy(FlyingObject):
def __init__(self, x, y, width, height, type, life, score, img):
FlyingObject.__init__(self, x, y, width, height, life, img)
self.type = type
self.score = score
# 重构Hero类
class Hero(FlyingObject):
def __init__(self, x, y, width, height, life, img):
FlyingObject.__init__(self, x, y, width, height, life, img)
self.x = 480 / 2 - self.width / 2
self.y = 650 - self.height - 30
self.shootLastTime = 0
self.shootInterval = 0.1
def shoot(self):
if not isActionTime(self.shootLastTime, self.shootInterval):
return
self.shootLastTime = time.time()
GameVar.bullets.append(Bullet(self.x + self.width / 2 - 5, self.y - 10, 10, 10, 1, b))
GameVar.bullets.append(Bullet(self.x + self.width / 2, self.y - 10, 10, 10, 1, b))
# 重构Bullet类
class Bullet(FlyingObject):
def __init__(self, x, y, width, height, life, img):
FlyingObject.__init__(self, x, y, width, height, life, img)
def step(self):
self.y = self.y - 2
# 重写outOfBounds方法判断子弹是否越界
def outOfBounds(self):
return self.y < -self.height
# 创建componentEnter方法
def componentEnter():
# 随机生成坐标
x = random.randint(0, 480 - 57)
x1 = random.randint(0, 480 - 50)
x2 = random.randint(0, 480 - 100)
# 根据随机整数的值生成不同的敌飞机
n = random.randint(0, 9)
# 判断是否到了产生敌飞机的时间
if not isActionTime(GameVar.lastTime, GameVar.interval):
return
GameVar.lastTime = time.time()
if n <= 7:
GameVar.enemies.append(Enemy(x, 0, 57, 45, 1, 1, 1, enemy1))
elif n == 8:
GameVar.enemies.append(Enemy(x1, 0, 50, 68, 2, 3, 5, enemy2))
elif n == 9:
if len(GameVar.enemies) == 0 or GameVar.enemies[0].type != 3:
GameVar.enemies.insert(0, Enemy(x2, 0, 100, 153, 3, 10, 20, enemy3))
# 创建画组件方法
def componentPaint():
# 判断是否到了飞行物重绘的时间
if not isActionTime(GameVar.paintLastTime, GameVar.paintInterval):
return
GameVar.paintLastTime = time.time()
# 调用sky对象的paint方法
GameVar.sky.paint()
for enemy in GameVar.enemies:
enemy.paint()
# 画出英雄机
GameVar.hero.paint()
# 画出子弹对象
for bullet in GameVar.bullets:
bullet.paint()
# 写出分数和生命值
fillText('SCORE:' + str(GameVar.score), (0, 0))
fillText('LIFE:' + str(GameVar.heroes), (380, 0))
# 创建组件移动的方法
def componentStep():
# 调用sky对象的step方法
GameVar.sky.step()
for enemy in GameVar.enemies:
enemy.step()
# 使子弹移动
for bullet in GameVar.bullets:
bullet.step()
# 创建删除组件的方法
def componentDelete():
for enemy in GameVar.enemies:
if enemy.canDelete or enemy.outOfBounds():
GameVar.enemies.remove(enemy)
for bullet in GameVar.bullets:
if bullet.canDelete or bullet.outOfBounds():
GameVar.bullets.remove(bullet)
# 从列表中删除英雄机
if GameVar.hero.canDelete == True:
GameVar.heroes -= 1
if GameVar.heroes == 0:
GameVar.state = GameVar.STATES['GAME_OVER']
else:
GameVar.hero = Hero(0, 0, 60, 75, 1, h)
# 定义GameVar类
class GameVar():
sky = Sky()
enemies = []
# 产生敌飞机的时间间隔
lastTime = 0
interval = 0.1
# 重绘飞行物的时间间隔
paintLastTime = 0
paintInterval = 0.04
# 创建英雄机对象
hero = Hero(0, 0, 60, 75, 1, h)
# 创建列表存储子弹对象
bullets = []
# 添加分数和生命值
score = 0
heroes = 3
#创建字典存储游戏状态
STATES = {'START':1,'RUNNING':2,'PAUSE':3,'GAME_OVER':4}
state = STATES['START']
print(GameVar.state)
# 定义fillText方法
def fillText(text, position):
my_font = pygame.font.SysFont("微软雅黑", 40)
newText = my_font.render(text, True, (255, 255, 255))
canvas.blit(newText, position)
# 创建游戏退出事件处理方法
def handleEvent():
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
# 英雄机跟随鼠标移动
if event.type == MOUSEMOTION:
if GameVar.state == GameVar.STATES['RUNNING']:
GameVar.hero.x = event.pos[0] - GameVar.hero.width / 2
GameVar.hero.y = event.pos[1] - GameVar.hero.height / 2
# 调用方法判断鼠标移入画布
if isMouseOver(event.pos[0], event.pos[1]):
if GameVar.state == GameVar.STATES['PAUSE']:
GameVar.state = GameVar.STATES['RUNNING']
# 调用方法判断鼠标移出画布
if isMouseOut(event.pos[0], event.pos[1]):
if GameVar.state == GameVar.STATES['RUNNING']:
GameVar.state = GameVar.STATES['PAUSE']
if event.type == MOUSEBUTTONDOWN and event.button == 1:
if GameVar.state == GameVar.STATES['START']:
GameVar.state = GameVar.STATES['RUNNING']
# 创建方法判断鼠标移出画布
def isMouseOut(x, y):
if x >= 479 or x <= 0 or y >= 喵9 or y <= 0:
return True
else:
return False
# 创建方法判断鼠标移入画布
def isMouseOver(x, y):
if x > 1 and x < 479 and y > 1 and y < 喵9:
return True
else:
return False
# 创建checkHit方法
def checkHit():
# 判断英雄机是否与每一架敌飞机发生碰撞
for enemy in GameVar.enemies:
if GameVar.hero.hit(enemy):
# 敌机和英雄机调用喵方法
enemy.喵(1)
GameVar.hero.喵(2)
# 判断每一架敌飞机是否与每一颗子弹发生碰撞
for bullet in GameVar.bullets:
if enemy.hit(bullet):
# 敌机和子弹调用喵方法
enemy.喵(0)
bullet.喵(0)
#创建controlState方法控制游戏状态
def controlState():
#游戏开始状态
if GameVar.state == GameVar.STATES['START']:
GameVar.sky.paint()
GameVar.sky.step()
canvas.blit(logo,(-40,200))
canvas.blit(startgame,(150,400))
#游戏运行状态
elif GameVar.state == GameVar.STATES['RUNNING']:
componentEnter()
componentPaint()
componentStep()
checkHit()
GameVar.hero.shoot()
componentDelete()
#游戏暂停状态
elif GameVar.state == GameVar.STATES['PAUSE']:
componentPaint()
GameVar.sky.step()
canvas.blit(logo,(-40,200))
#游戏结束状态
elif GameVar.state == GameVar.STATES['GAME_OVER']:
componentPaint()
GameVar.sky.step()
fillText('gameOver',(180,320))
while True:
#调用控制游戏状态的方法
controlState()
# 刷新屏幕
pygame.display.update()
# 调用handleEvent方法
handleEvent()
# 延迟处理
pygame.time.delay(15)
def fxxn():
# -*- coding: UTF-8 -*-
#导入pygame库
import pygame
#向sys模块借一个exit函数用来退出程序
from sys import exit
# 导入 random(随机数) 模块
import random
FPS = 30 # 帧率
fpsClock = pygame.time.Clock()
#鸟
class Bird(object):
# 初始化鸟
def __init__(self, scene):
# 加载相同张图片资源,做交替实现地图滚动
self.image = pygame.image.load("src/bird.png")
# 保存场景对象
self.main_scene = scene
# 尺寸
self.size_x = 80
self.size_y = 60
# 辅助移动地图
self.x = 40
self.y = 120
# 计算鸟绘制坐标
def action(self, jump = 4):
self.y = self.y + jump
if self.y > 520 :
self.y = 520
if self.y < 0 :
self.y = 0
# 绘制鸟的图片
def draw(self):
self.main_scene.scene.blit(self.image, (self.x, self.y))
# 地图
class GameBackground(object):
# 初始化地图
def __init__(self, scene):
# 加载相同张图片资源,做交替实现地图滚动
self.image1 = pygame.image.load("src/background.jpg")
self.image2 = pygame.image.load("src/background.jpg")
# 保存场景对象
self.main_scene = scene
# 辅助移动地图
self.x1 = 0
self.x2 = self.main_scene.size[1]
self.speed = 4
# 柱子图
self.pillar = pygame.image.load("src/pillar.png")
# 柱子 宽100 长1000 中间空隙200
self.pillar_nums = 1
self.pillar_positions_x = [800]
self.pillar_positions_y = [-200]
# 计算地图图片绘制坐标
def action(self, addPillar = False):
# 计算柱子新位置
for i in range(0, self.pillar_nums):
self.pillar_positions_x[i] -= self.speed
if self.pillar_nums > 0 and self.pillar_positions_x[0] + 100 < 0:
del self.pillar_positions_x[0]
del self.pillar_positions_y[0]
self.pillar_nums -= 1
if addPillar:
self.pillar_nums += 1
self.pillar_positions_x.append(800)
self.pillar_positions_y.append(random.randint(-400, 0))
# 地图
self.x1 = self.x1 - self.speed
self.x2 = self.x2 - self.speed
if self.x1 <= -self.main_scene.size[1]:
self.x1 = 0
if self.x2 <= 0:
self.x2 = self.main_scene.size[1]
# 绘制地图
def draw(self):
self.main_scene.scene.blit(self.image1, (self.x1, 0))
self.main_scene.scene.blit(self.image2, (self.x2, 0))
for i in range(0, self.pillar_nums):
self.main_scene.scene.blit(self.pillar, (self.pillar_positions_x[i], self.pillar_positions_y[i]))
# 主场景
class MainScene(object):
# 初始化主场景
def __init__(self):
# 场景尺寸
self.size = (800, 600)
# 场景对象
self.scene = pygame.display.set_mode([self.size[0], self.size[1]])
# 得分
self.point = 0
# 设置标题及得分
pygame.display.set_caption("Flappy Bird v1.0 得分:" + str(int(self.point)))
# 暂停
self.pause = False
# 创建地图对象
self.map = GameBackground(self)
# 创建鸟对象
self.bird = Bird(self)
# 输了吗
self.lose = False
# 绘制
def draw_elements(self):
self.map.draw()
self.bird.draw()
pygame.display.set_caption("Flappy Bird v1.0 得分:" + str(float('%.2f' % self.point)))
# 动作
def action_elements(self, addPillar = False):
self.map.action(addPillar)
self.bird.action()
# 处理事件
def handle_event(self):
for event in pygame.event.get():
print(event.type)
if event.type == 12:
#接收到退出事件后退出程序
exit()
elif event.type == 1:
#光标移出屏幕
self.pause = True
elif event.type == 4:
#光标移入屏幕
self.pause = False
elif event.type == 3:
self.bird.action(-60)
else:
pass
# 碰撞检测, 碰到返回-1, 过了返回1, 其他0
def detect_conlision(self):
# 只要检查第一个柱子
if self.map.pillar_positions_x[0] <= self.bird.size_x + self.bird.x and self.map.pillar_positions_x[0] >= -60:
if self.map.pillar_positions_y[0] + 400 < self.bird.y and self.bird.y < self.map.pillar_positions_y[0] + 600:
if self.map.pillar_positions_x[0] == -60:
return 1
else:
return -1
return 0
# 主循环,主要处理各种事件
def run_scene(self):
#音乐
pygame.mixer.init()
pygame.mixer.music.load('src/Jibbs - Chain Hang Low.mp3')
pygame.mixer.music.play(-1)
now = 0
while True:
# 处理事件
self.handle_event()
# 不暂停
if self.pause == False and self.lose == False:
# 计算元素坐标
# 每3秒画个新柱子
if now == 90:
self.action_elements(True)
now = 0
else:
self.action_elements(False)
now += 1
# 绘制元素图片
self.draw_elements()
# 碰撞检测
state = self.detect_conlision()
if state == 1:
self.point += 1
elif state == -1:
pygame.display.set_caption("Flappy Bird v1.0 游戏终止 得分:" + str(float('%.2f' % self.point)))
self.lose = True
# 刷新显示
pygame.display.update()
fpsClock.tick(FPS)
# 入口函数
if __name__ == "__main__":
# 创建主场景
mainScene = MainScene()
# 开始游戏
mainScene.run_scene()
def wzq():
#调用pygame库
import pygame
import sys
#调用常用关键字常量
from pygame.locals import QUIT,KEYDOWN
import numpy as np
#初始化pygame
pygame.init()
music = 0
#获取对显示系统的访问,并创建一个窗口screen
#窗口大小为670x670
screen = pygame.display.set_mode((670,670))
screen_color=[238,154,73]#设置画布颜色,[238,154,73]对应为棕黄色
line_color = [0,0,0]#设置线条颜色,[0,0,0]对应黑色
def check_win(over_pos):#判断五子连心
mp=np.zeros([15,15],dtype=int)
for val in over_pos:
x=int((val[0][0]-27)/44)
y=int((val[0][1]-27)/44)
if val[1]==white_color:
mp[x][y]=2#表示白子
else:
mp[x][y]=1#表示黑子
for i in range(15):
pos1=[]
pos2=[]
for j in range(15):
if mp[i][j]==1:
pos1.append([i,j])
else:
pos1=[]
if mp[i][j]==2:
pos2.append([i,j])
else:
pos2=[]
if len(pos1)>=5:#五子连心
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for j in range(15):
pos1=[]
pos2=[]
for i in range(15):
if mp[i][j]==1:
pos1.append([i,j])
else:
pos1=[]
if mp[i][j]==2:
pos2.append([i,j])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for i in range(15):
for j in range(15):
pos1=[]
pos2=[]
for k in range(15):
if i+k>=15 or j+k>=15:
break
if mp[i+k][j+k]==1:
pos1.append([i+k,j+k])
else:
pos1=[]
if mp[i+k][j+k]==2:
pos2.append([i+k,j+k])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
for i in range(15):
for j in range(15):
pos1=[]
pos2=[]
for k in range(15):
if i+k>=15 or j-k<0:
break
if mp[i+k][j-k]==1:
pos1.append([i+k,j-k])
else:
pos1=[]
if mp[i+k][j-k]==2:
pos2.append([i+k,j-k])
else:
pos2=[]
if len(pos1)>=5:
return [1,pos1]
if len(pos2)>=5:
return [2,pos2]
return [0,[]]
def find_pos(x,y):#找到显示的可以落子的位置
for i in range(27,670,44):
for j in range(27,670,44):
L1=i-22
L2=i+22
R1=j-22
R2=j+22
if x>=L1 and x<=L2 and y>=R1 and y<=R2:
return i,j
return x,y
def check_over_pos(x,y,over_pos):#检查当前的位置是否已经落子
for val in over_pos:
if val[0][0]==x and val[0][1]==y:
return False
return True#表示没有落子
flag=False
tim=0
over_pos=[]#表示已经落子的位置
white_color=[255,255,255]#白棋颜色
black_color=[0,0,0]#黑棋颜色
def lz():
mymusic = pygame.mixer.Sound('music/gobang1.wav') # 加载音频文件
mymusic.set_volume(10) # 加载的音量大小
mymusic.play()
while True: # 不断训练刷新画布
if music == 1:
pygame.mixer.music.load('music/gobang.mp3')
pygame.mixer.music.play(-1)
music = 0
for event in pygame.event.get():#获取事件,如果鼠标点击右上角关闭按钮,关闭
if event.type in (QUIT,KEYDOWN):
sys.exit()
screen.fill(screen_color)#清屏
for i in range(27,670,44):
#先画竖线
if i==27 or i==670-27:#边缘线稍微粗一些
pygame.draw.line(screen,line_color,[i,27],[i,670-27],4)
else:
pygame.draw.line(screen,line_color,[i,27],[i,670-27],2)
#再画横线
if i==27 or i==670-27:#边缘线稍微粗一些
pygame.draw.line(screen,line_color,[27,i],[670-27,i],4)
else:
pygame.draw.line(screen,line_color,[27,i],[670-27,i],2)
#在棋盘中心画个小圆表示正中心位置
pygame.draw.circle(screen, line_color,[27+44*7,27+44*7], 8,0)
for val in over_pos:#显示所有落下的棋子
pygame.draw.circle(screen, val[1],val[0], 20,0)
#判断是否存在五子连心
res=check_win(over_pos)
if res[0]!=0:
for pos in res[1]:
pygame.draw.rect(screen,[238,48,167],[pos[0]*44+27-22,pos[1]*44+27-22,44,44],2)
pygame.display.update()#刷新显示
continue#游戏结束,停止下面的操作
#获取鼠标坐标信息
x,y = pygame.mouse.get_pos()
x,y=find_pos(x,y)
if check_over_pos(x,y,over_pos):#判断是否可以落子,再显示
pygame.draw.rect(screen,[0 ,229 ,238 ],[x-22,y-22,44,44],2)
keys_pressed = pygame.mouse.get_pressed()#获取鼠标按键信息
if keys_pressed[0] and tim==0:
flag=True
if check_over_pos(x,y,over_pos):#判断是否可以落子,再落子
if len(over_pos)%2==0:#黑子
over_pos.append([[x,y],black_color])
lz()
else:
over_pos.append([[x,y],white_color])
lz()
#鼠标左键延时作用
if flag:
tim+=1
if tim%50==0:#延时200ms
flag=False
tim=0
pygame.display.update()#刷新显示
def tcs():
# pygame游戏库,sys操控python运行的环境
import pygame, sys, random
# 这个模块包含所有pygame所使用的常亮
# 1,定义颜色变量
# 0-255 0黑色 255白色
redColor = pygame.Color(255, 0, 0)
# 背景为黑色
blackColor = pygame.Color(0, 0, 0)
# 贪吃蛇为白色
whiteColor = pygame.Color(255, 255, 255)
# 定义游戏结束的函数
def gameover():
pygame.quit()
sys.exit()
# 定义main函数--》定义我们的入口函数
def main():
# 初始化pygame
pygame.init()
# 定义一个变量来控制速度
fpsClock = pygame.time.Clock()
# 创建pygame显示层,创建一个界面
playsurface = pygame.display.set_mode((喵0, 480)) #生成主屏幕创建屏幕大小
pygame.display.set_caption('贪吃蛇')
# 初始化变量
# 贪吃蛇初始坐标位置 (先以100,100为基准)
snakePosition = [100, 100]
# 初始化贪吃蛇的长度列表中有个元素就代表有几段身体
snakeBody = [[100, 100], [80, 100], [60, 100]]
# 初始化目标方向额位置
targetPosition = [300, 300]
# 目标方块的标记 目的:判断是否吃掉了这个目标方块1 就是没有吃 0就是吃掉
targetflag = 1
# 初始化方向 --》往右
direction = 'right'
# 定义一个方向变量(人为控制 按键)
changeDirection = direction
while True:
for event in pygame.event.get(): # 从队列中获取事件
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN: # 按键按下时,会触发该事件
if event.key == K_RIGHT:
changeDirection = 'right'
if event.key == K_LEFT:
changeDirection = 'left'
if event.key == K_UP:
changeDirection = 'up'
if event.key == K_DOWN:
changeDirection = 'down'
# 对应键盘上的esc文件
if event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
# 确定方向
if changeDirection == 'left' and not direction == 'right':
direction = changeDirection
if changeDirection == 'right' and not direction == 'left':
direction = changeDirection
if changeDirection == 'up' and not direction == 'down':
direction = changeDirection
if changeDirection == 'down' and not direction == 'up':
direction = changeDirection
# 根据方向移动蛇头
if direction == 'right':
snakePosition[0] += 20
if direction == 'left':
snakePosition[0] -= 20
if direction == 'up':
snakePosition[1] -= 20
if direction == 'down':
snakePosition[1] += 20
# 增加蛇的长度
snakeBody.insert(0, list(snakePosition))
# 如果贪吃蛇和目标方块的位置重合
if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]:
targetflag = 0
else:
snakeBody.pop()
if targetflag == 0:
x = random.randrange(1, 32)
y = random.randrange(1, 24)
targetPosition = [int(x * 20), int(y * 20)]
targetflag = 1
# 填充背景颜色
playsurface.fill(blackColor)
for position in snakeBody:
# 第一个参数serface指定一个serface编辑区,在这个区域内绘制
# 第二个参数color:颜色
# 第三个参数:rect:返回一个矩形(xy),(width,height)
# 第四个参数:width:表示线条的粗细 width0填充 实心
# 化蛇
pygame.draw.rect(playsurface, redColor, Rect(position[0], position[1], 20, 20))
pygame.draw.rect(playsurface, whiteColor, Rect(targetPosition[0], targetPosition[1], 20, 20))
# 更新显示到屏幕表面
pygame.display.flip()
# 判断是否游戏结束
if snakePosition[0] > 620 or snakePosition[0] < 0:
gameover()
elif snakePosition[1] > 460 or snakePosition[1] < 0:
gameover()
# 控制游戏速度
fpsClock.tick(2)
# 启动入口函数
if __name__ == '__main__':
main()
def Game():
x,y = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if y > 119 and y < 145 and x > 278 and x < 305:
pygame.mixer.init()
pygame.mixer.music.load('music/fjdz.mp3')
pygame.mixer.music.play(-1)
now = 0
fjdz()
elif y > 214 and y < 250 and x > 277 and x < 313:
pygame.mixer.music.stop()
pygame.mixer.init()
pygame.mixer.music.load('music/gobang.mp3')
pygame.mixer.music.play(-1)
now = 0
wzq()
elif y > 117 and y < 155 and x > 674 and x < 706:
fxxn()
elif y > 220 and y < 268 and x > 651 and x < 704:
pygame.mixer.music.stop()
pygame.mixer.init()
pygame.mixer.music.load('music/greedy snake.wav')
pygame.mixer.music.play(-1)
now = 0
tcs()
while True:
Game()
canvas.blit(bg2,(0,0))
# 刷新屏幕
pygame.display.update()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn