python学习之乌龟吃鱼and思聪吃热狗游戏
乌龟吃鱼游戏
游戏规则:
1). 假设游戏场景为范围(x,y)为0<=x<=10,0<=y<=10
2). 游戏生成1只乌龟和10条鱼, 它们的移动方向均随机
3). 乌龟的最大移动能力为2(它可以随机选择1还是2移动),
鱼儿的最大移动能力是1当移动到场景边缘,自动向反方向移动
4). 乌龟初始化体力为100(上限), 乌龟每移动一次,体力消耗1
当乌龟和鱼坐标重叠,乌龟吃掉鱼,乌龟体力增加20, 鱼暂不计算体力
5). 当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
专注于为中小企业提供成都做网站、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业上饶免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
01_turtle_fish.py
import random
class Turtle(object):
"""
乌龟类
属性: (x,y), power
方法: move(), eat()
"""
def __init__(self):
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
self.power = 100
def move(self):
"""乌龟移动的方法"""
#乌龟的最大移动能力是2当移动到场景边缘,
move_skills = [-2, -1, 1, 2]
#计算鱼最新的x轴坐标;()
new_x = self.x + random.choice(move_skills)
new_y = self.y + random.choice(move_skills)
#当移动到场景边缘如何处理?
#(10, 0) ---- (11, 1) ----- (1, 1)
#(10, 0) --- (9, -1) -----(9, 9)
self.x = new_x % 10
self.y = new_y % 10
def eat(self):
"""乌龟吃鱼"""
self.power += 20
print("乌龟吃鱼, 能量+20!")
class Fish(object):
"""
鱼类
属性: (x,y)
方法: move()
"""
def __init__(self):
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
def move(self):
"""
鱼儿的最大移动能力是1当移动到场景边缘,
"""
#鱼儿的最大移动能力是1当移动到场景边缘,
move_skills = [-1, 1]
#计算鱼最新的x轴坐标;()
new_x = self.x + random.choice(move_skills)
new_y = self.y + random.choice(move_skills)
#当移动到场景边缘如何处理?
self.x = new_x % 10
self.y = new_y % 10
if __name__ == '__main__':
# 游戏生成1只乌龟和10条鱼, 它们的移动方向均随机.
tur = Turtle()
fishes = [Fish() for item in range(10)]"""
01_turtle_fish.py
import random
class BaseAnimal(object):
def __init__(self):
self.x = random.randint(0, 10)
self.y = random.randint(0, 10)
def move(self, move_skills=(-1, 1)):
new_x = self.x + random.choice(move_skills)
new_y = self.y + random.choice(move_skills)
#当移动到场景边缘如何处理?
#(10, 0) ---- (11, 1) ----- (1, 1)
#(10, 0) --- (9, -1) -----(9, 9)
self.x = new_x % 10
self.y = new_y % 10
class Turtle(BaseAnimal):
"""
乌龟类
属性: (x,y), power
方法: move(), eat()
"""
def __init__(self):
super(Turtle, self).__init__()
self.power = 100
def eat(self):
"""乌龟吃鱼"""
self.power += 20
print("乌龟吃鱼, 能量+20!")
class Fish(BaseAnimal):
pass
if __name__ == '__main__':
# 游戏生成1只乌龟和10条鱼, 它们的移动方向均随机.
tur = Turtle()
fishes = [Fish() for item in range(10)]"""
03_pygame游戏基本操作.py
import pygame
#1). 初始化pygame
pygame.init()
#2). 显示游戏界面
pygame.display.set_mode((800, 800))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("游戏结束......")
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
print("UP")
elif event.key == pygame.K_DOWN:
print('DOWN')"""
04_game.py #同理思聪吃热狗游戏
import random
import pygame
class SiCong(object):
"""
思聪类
属性: (x,y), power
方法: move(), eat()
"""
def __init__(self):
self.x = random.randint(50, width - 50)
self.y = random.randint(50, height - 50)
self.power = 100
def move(self, new_x, new_y):
"""思聪移动的方法"""
self.x = new_x % width
self.y = new_y % height
def eat(self):
"""思聪吃热狗"""
self.power += 20
print("思聪吃热狗, 能量+20!")
class HotDog(object):
"""
热狗类
属性: (x,y)
方法: move()
"""
def __init__(self):
self.x = random.randint(50, width - 50)
self.y = random.randint(50, height - 50)
def move(self):
"""
鱼儿的最大移动能力是1当移动到场景边缘,
"""
#鱼儿的最大移动能力是1当移动到场景边缘,
move_skills = [-10]
#计算鱼最新的x轴坐标;()
new_x = self.x + random.choice(move_skills)
#当移动到场景边缘如何处理?
self.x = new_x % width
def main():
pygame.init()
#显示游戏界面
screen = pygame.display.set_mode((width, height))
#设置界面标题
pygame.display.set_caption("吃热狗游戏")
#加载游戏中需要的图片
bg = pygame.image.load('./img/bigger_bg1.jpg').convert()
hotdogImg = pygame.image.load('./img/hot-dog.png').convert_alpha()
sicongImg = pygame.image.load('./img/sicong.png').convert_alpha()
hd_width, hd_height = hotdogImg.get_width(), hotdogImg.get_height()
sc_width, sc_height = sicongImg.get_width(), sicongImg.get_height()
#加载游戏音乐(背景音乐和吃掉热狗的音乐)
pygame.mixer.music.load('./img/game_music.mp3')
pygame.mixer.music.play(loops=0, start=0.0) # 播放设置, 不循环且从0.0s开始播放
#设置分数显示参数信息(显示位置、字体颜色、字体大小)
scoreCount = 0
font = pygame.font.SysFont('arial', 20) # 系统设置字体的类型和大小
#颜色表示法: RGB (255, 0, 0)-红色 (255, 255, 255)-白色 (0, 0, 0)-黑色
score = font.render("Score: %s" % (scoreCount), True, (0, 0, 0))
#创建一个Clock对象,跟踪游戏运行时间
fpsClock = pygame.time.Clock()
#创建一个思聪和10个热狗
sicong = SiCong()
hotdogs = [HotDog() for item in range(10)]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print("游戏结束......")
exit(0)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
# 移动人物向上多少个像素
sicong.move(sicong.x, sicong.y - 10)
elif event.key == pygame.K_DOWN:
sicong.move(sicong.x, sicong.y + 10)
if event.key == pygame.K_LEFT:
# 移动人物向上多少个像素
sicong.move(sicong.x - 10, sicong.y)
elif event.key == pygame.K_RIGHT:
sicong.move(sicong.x + 10, sicong.y)
#绘制背景和分数
screen.blit(bg, (0, 0))
screen.blit(score, (200, 20))
#绘制热狗,并实现热狗的移动
for hd in hotdogs:
screen.blit(hotdogImg, (hd.x, hd.y))
hd.move()
#绘制sicong
screen.blit(sicongImg, (sicong.x, sicong.y))
#判断游戏是否结束: 当人物体力值为0(挂掉)或者热狗的数量为0游戏结束
if sicong.power == 0:
print("Game Over: Sicong Poer is 0")
exit(1)
if len(hotdogs) == 0:
print("Game Over: hot-dog count is 0")
exit(2)
#判断人物是否吃到热狗:人物和热狗的坐标值相同, 则认为吃掉
for hd in hotdogs:
if 0 < sicong.x - hd.x < 50 and 0 < sicong.y - hd.y < 50:
# 增加人物的能量值
sicong.eat()
#移除被吃掉的热狗
hotdogs.remove(hd)
#增加得分
scoreCount += 10
#重新设置得分信息
score = font.render("Score: %s" % (scoreCount), True, (0, 0, 0))
#更新内容到游戏窗口
pygame.display.update()
fpsClock.tick(10) # 每秒更新10帧
if __name__ == '__main__':
width =1000
height = 666
main()
05_乌龟吃鱼游戏.py
import random
import time
import pygame
import sys
from pygame.locals import * # 导入一些常用的函数
width = 474
height = 233
pygame.init()
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption('乌龟吃鱼') # 定义窗口的标题为'乌龟吃鱼'
background = pygame.image.load("./img/bg.jpg").convert()
fishImg = pygame.image.load("./img/hot-dog.png").convert_alpha()
wuguiImg = pygame.image.load("./img/sicong.png").convert_alpha()
#乌龟吃掉小鱼的音乐 mp3格式的不行,wav格式的
#eatsound = pygame.mixer.Sound("achievement.wav")
#背景音乐
pygame.mixer.music.load("./img/game_music.mp3")
pygame.mixer.music.play(loops=0, start=0.0)
#成绩文字显示
count = 0
font = pygame.font.SysFont("arial", 20)
score = font.render("score %d" % count, True, (255, 255, 255))
#显示游戏状态
status = font.render("Gaming", True, (255, 255, 255))
w_width = wuguiImg.get_width() - 5 # 得到乌龟图片的宽度,后面留着吃鱼的时候用
w_height = wuguiImg.get_height() - 5 # 得到乌龟图片的高度
y_width = fishImg.get_width() - 5 # 得到鱼图片的宽度
y_height = fishImg.get_height() - 5 # 得到鱼图片的高度
fpsClock = pygame.time.Clock() # 创建一个新的Clock对象,可以用来跟踪总共的时间
#乌龟类
class Turtle:
def __init__(self):
self.power = 100 # 体力
#乌龟坐标
self.x = random.randint(0, width - w_width)
self.y = random.randint(0, height - w_height)
#乌龟移动的方法:移动方向均随机 第四条
def move(self, new_x, new_y):
self.x = new_x % width
self.y = new_y % height
self.power -= 1 # 乌龟每移动一次,体力消耗1
def eat(self):
self.power += 20 # 乌龟吃掉鱼,乌龟体力增加20
if self.power > 100:
self.power = 100 # 乌龟体力100(上限)
#鱼类
class Fish:
def __init__(self):
# 鱼坐标
self.x = random.randint(0, width - y_width)
self.y = random.randint(0, height - y_height)
def move(self):
new_x = self.x + random.choice([-10])
self.x = new_x % width
tur = Turtle() # 生成1只乌龟
fish = [] # 生成10条鱼
for item in range(10):
newfish = Fish()
fish.append(newfish) # 把生成的鱼放到鱼缸里
#pygame有一个事件循环,不断检查用户在做什么。事件循环中,如何让循环中断下来(pygame形成的窗口中右边的插号在未定义前是不起作用的)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
# 通过上下左右方向键控制乌龟的动向
if event.key == pygame.K_LEFT:
tur.move(tur.x - 10, tur.y)
if event.key == pygame.K_RIGHT:
tur.move(tur.x + 10, tur.y)
if event.key == pygame.K_UP:
tur.move(tur.x, tur.y - 10)
if event.key == pygame.K_DOWN:
tur.move(tur.x, tur.y + 10)
screen.blit(background, (0, 0)) # 绘制背景图片
screen.blit(score, (400, 20)) # 绘制分数
screen.blit(status, (0, 20)) # 绘制分数
#绘制鱼
for item in fish:
screen.blit(fishImg, (item.x, item.y))
#pygame.time.delay(100)
item.move() # 鱼移动
screen.blit(wuguiImg, (tur.x, tur.y)) # 绘制乌龟
#判断游戏是否结束:当乌龟体力值为0(挂掉)或者鱼儿的数量为0游戏结束
if tur.power < 0:
print("Game Over: Turtle power is 0")
#显示游戏状态
status = font.render("Game Over: Turtle power is 0", True, (255, 255, 255))
pygame.display.update() # 更新到游戏窗口
time.sleep(1)
sys.exit(0)
elif len(fish) == 0:
status = font.render("Game Over: Fish is empty", True, (255, 255, 255))
pygame.display.update() # 更新到游戏窗口
sys.exit(0)
for item in fish:
# print("鱼", item.x, item.y, y_width, y_height)
#print("乌龟", tur.x, tur.y, w_width, w_height)
#判断鱼和乌龟是否碰撞?
if ((tur.x < item.x + y_width) and (tur.x + w_width > item.x)
and (tur.y < item.y + y_height) and (w_height + tur.y > item.y)):
fish.remove(item) # 鱼死掉
#吃鱼音乐
#eatsound.play()
count = count + 1 # 累加
score = font.render("score %d" % count, True, (255, 255, 255))
#print("死了一只鱼")
#print("乌龟最新体力值为 %d" % tur.power)
pygame.display.update() # 更新到游戏窗口
fpsClock.tick(10) # 通过每帧调用一次fpsClock.tick(10),这个程序就永远不会以超过每秒10帧的速度运行
文章标题:python学习之乌龟吃鱼and思聪吃热狗游戏
网页地址:http://scyanting.com/article/gjspod.html