基于Python怎样制作个水果忍者游戏
Admin 2022-09-03 群英技术资讯 1060 次浏览
在实际应用中,我们有时候会遇到“基于Python怎样制作个水果忍者游戏”这样的问题,我们该怎样来处理呢?下文给大家介绍了解决方法,希望这篇“基于Python怎样制作个水果忍者游戏”文章能帮助大家解决问题。今天就用python简单的模拟一下这个游戏。在这个简单的项目中,我们用鼠标选择水果来切割,同时炸弹也会隐藏在水果中,如果切开了三次炸弹,玩家就会失败。
import pygame, sys import os import random
# 游戏窗口
WIDTH = 800
HEIGHT = 500
FPS = 15 # gameDisplay的帧率,1/12秒刷新一次
pygame.init()
pygame.display.set_caption('水果忍者') # 标题
gameDisplay = pygame.display.set_mode((WIDTH, HEIGHT)) # 固定窗口大小
clock = pygame.time.Clock()
# 用到的颜色
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
background = pygame.image.load('背景.jpg') # 背景
font = pygame.font.Font(os.path.join(os.getcwd(), 'comic.ttf'), 42) # 字体
score_text = font.render('Score : ' + str(score), True, (255, 255, 255)) # 得分字体样式
def generate_random_fruits(fruit):
fruit_path = "images/" + fruit + ".png"
data[fruit] = {
'img': pygame.image.load(fruit_path),
'x' : random.randint(100,500),
'y' : 800,
'speed_x': random.randint(-10,10),
'speed_y': random.randint(-80, -60),
'throw': False,
't': 0,
'hit': False,
}
if random.random() >= 0.75:
data[fruit]['throw'] = True
else:
data[fruit]['throw'] = False
data = {}
for fruit in fruits:
generate_random_fruits(fruit)
•这个函数用于随机生成水果和保存水果的数据
•'x’和’y’存储水果在x坐标和y坐标上的位置。
•Speed_x和speed_y是存储水果在x和y方向的移动速度。它也控制水果的对角线移动。
•throw,用于判断生成的水果坐标是否在游戏之外。如果在外面,那么将被丢弃。
•data字典用于存放随机生成的水果的数据。
font_name = pygame.font.match_font('comic.ttf')
def draw_text(display, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
gameDisplay.blit(text_surface, text_rect)
•Draw_text函数可以在屏幕上绘制文字。
•get_rect() 返回 Rect 对象。
•X和y是X方向和Y方向的位置。
•blit()在屏幕上的指定位置绘制图像或写入文字。
# 绘制玩家的生命
def draw_lives(display, x, y, lives, image) :
for i in range(lives) :
img = pygame.image.load(image)
img_rect = img.get_rect()
img_rect.x = int(x + 35 * i)
img_rect.y = y
display.blit(img, img_rect)
def hide_cross_lives(x, y):
gameDisplay.blit(pygame.image.load("images/red_lives.png"), (x, y))
•img_rect获取十字图标的(x,y)坐标(位于最右上方)
•img_rect .x 设置下一个十字图标距离前一个图标35像素。
•img_rect.y负责确定十字图标从屏幕顶部开始的位置。
•另外,搜索公众号Linux就该这样学后台回复“git书籍”,获取一份惊喜礼包。
def show_gameover_screen():
gameDisplay.blit(background, (0,0))
draw_text(gameDisplay, "FRUIT NINJA!", 90, WIDTH / 2, HEIGHT / 4)
if not game_over :
draw_text(gameDisplay,"Score : " + str(score), 50, WIDTH / 2, HEIGHT /2)
draw_text(gameDisplay, "Press a key to begin!", 64, WIDTH / 2, HEIGHT * 3 / 4)
pygame.display.flip()
waiting = True
while waiting:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP:
waiting = False
•show_gameover_screen()函数显示初始游戏画面和游戏结束画面。
•pygame.display.flip()将只更新屏幕的一部分,但如果没有参数传递,则会更新整个屏幕。
•pygame.event.get()将返回存储在pygame事件队列中的所有事件。
•如果事件类型等于quit,那么pygame将退出。
•event.KEYUP事件,当按键被按下和释放时发生的事件。
first_round = True
game_over = True
game_running = True
while game_running :
if game_over :
if first_round :
show_gameover_screen()
first_round = False
game_over = False
player_lives = 3
draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
score = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
gameDisplay.blit(background, (0, 0))
gameDisplay.blit(score_text, (0, 0))
draw_lives(gameDisplay, 690, 5, player_lives, 'images/red_lives.png')
for key, value in data.items():
if value['throw']:
value['x'] += value['speed_x']
value['y'] += value['speed_y']
value['speed_y'] += (1 * value['t'])
value['t'] += 1
if value['y'] <= 800:
gameDisplay.blit(value['img'], (value['x'], value['y']))
else:
generate_random_fruits(key)
current_position = pygame.mouse.get_pos()
if not value['hit'] and current_position[0] > value['x'] and current_position[0] < value['x']+60 \
and current_position[1] > value['y'] and current_position[1] < value['y']+60:
if key == 'bomb':
player_lives -= 1
if player_lives == 0:
elif player_lives == 1 :
hide_cross_lives(725, 15)
elif player_lives == 2 :
hide_cross_lives(760, 15)
if player_lives < 0 :
show_gameover_screen()
game_over = True
half_fruit_path = "images/explosion.png"
else:
half_fruit_path = "images/" + "half_" + key + ".png"
value['img'] = pygame.image.load(half_fruit_path)
value['speed_x'] += 10
if key != 'bomb' :
score += 1
score_text = font.render('Score : ' + str(score), True, (255, 255, 255))
value['hit'] = True
else:
generate_random_fruits(key)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
•这是游戏的主循环
•如果超过3个炸弹被切掉,game_over终止游戏,同时循环。
•game_running 用于管理游戏循环。
•如果事件类型是退出,那么游戏窗口将被关闭。
•在这个游戏循环中,我们动态显示屏幕内的水果。
•如果一个水果没有被切开,那么它将不会发生任何事情。如果水果被切开,那么一个半切开的水果图像应该出现在该水果的地方
•如果用户点击了三次炸弹,将显示GAME OVER信息,并重置窗口。
•clock.tick()将保持循环以正确的速度运行。循环应该在每1/12秒后更新一次
大家都动手玩起来,这个小游戏还是特别不错的,看看谁的手速快
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文详细讲解了Python中requests库的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Python是一种知道如何不妨碍你编写程序的编程语言。它易于学习,功能强大,足以构建Web应用程序并自动化无聊的东西。本文是对常用字符串操作进行了详细的总结分析,希望对您有所帮助。
这篇文章主要介绍了Python游戏推箱子的实现,推箱子游戏是一款可玩性极高的策略解谜手游,游戏中玩家将扮演一名可爱Q萌的角色,下面我们就看看看具体的实现过程吧,需要的小伙伴可以参考一下
为什么要用numpy? Python中提供了list容器,可以当作数组使用。但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3]。就需要三个指针和三个整数对象。对于数值运算来说,这种结构显然不够高效。
对于中文乱码的情况很多朋友在编程过程中都有遇到,因此这篇文章就主要给大家分享的是有关python出现中文乱码怎样解决的方法。小编觉得挺实用的,因此分享给大家做个参考,感兴趣的朋友就继续往下看吧。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008