Pygame实战之实现扎气球游戏
作者:顾木子吖 时间:2023-06-17 10:17:43
导语
前几天,有人私信小编:
说陪女朋友在小广场上面逛街玩儿扎气球:结果一个都没扎破,扎心了老铁。
女朋友都要离家出走了~让我给想想办法:小编只想给你一个表情。
哈哈哈,开玩笑的~于是,为了满足需求,小编做了一个重大决定:熬夜给他做了一款扎气球的小
游戏,可以拿去哄哄女朋友啦~
这游戏做完之后木子已经替大家玩儿过了,这个很棒,不信的话你自己试试?
本文的扎气球小游戏原型就是路边的扎气球的游戏撒,基于Pygame做的!
就准备好射的箭、不同颜色的气球、一张背景图片、然后 * 的特效就可。哦~对了音乐还是要准备,游戏的话有音乐背景才更有趣哦~
一、准备中
1)素材资料
首先是准备好需要的素材、图片、背景音乐:
2)运行环境
环境安装 本文用到的运行环境:Python3.7、Pycharm社区版2020、Pygame游戏模块部分自带
模块直 接导入不需要安装。
模块安装:
pip install -i https://pypi.douban.com/simple/ +模块名
二、代码演示
这款小游戏总的有6个.py文件组成的,代码比较都啦,这里就只放一点点哈!
主程序运行:
from game import *
def main() :
intro = True
game = Game()
game.loadMusic()
game.readHighScore()
pygame.mixer.music.play(loops=-1)
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
intro = False
game.screen.fill(SKY_BLUE)
game.screen.blit(game.background, game.background_rect)
game.draw.Button(200, 2*game.HEIGHT/3, "PLAY", BRIGHT_GREEN, GREEN, game.gameloop, 150, 100)
game.draw.Button(game.WIDTH/2 - 75, 2*game.HEIGHT/3, "PLAY TIMED", BRIGHT_RED, RED, game.time_restricted, 150, 100)
game.draw.Button(game.WIDTH-350, 2*game.HEIGHT/3, "QUIT", BRIGHT_GREEN, GREEN, quit, 150, 100)
game.draw.draw_text("__ArcuS__", game.WIDTH/2, game.HEIGHT/3, 200, BLUE)
game.draw.draw_text("HIGH SCORE:%d" % (game.highscore), game.WIDTH-400, 50, 30, BLACK)
pygame.display.flip()
game.clock.tick(FPS)
main()
定义的一些常量:桌面背景、音乐等等。
FPS = 60
GRAVITY = 0.15
PI = 3.142
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
BRIGHT_RED = (255, 0, 0)
GREEN = (0, 200, 0)
BRIGHT_GREEN = (0, 255, 0)
SKY_BLUE = (0, 255, 255)
BLUE = (0, 0, 255)
GREEN_YELLOW=(181,255,98)
BROWN=(204,102,0)
DARK_BROWN=(204,76,0)
HIGHSCORE_FILE="highscore.txt"
ARROW_IMAGE = "assets/arrow_1.png"
BACKGROUND_IMAGE = "assets/background.png"
EXPLOSION_SOUND = "assets/boom.wav"
CLICK_SOUND = "assets/select.wav"
MUSIC_FILE = "assets/tgfcoder-FrozenJam-SeamlessLoop.ogg"
VOLUME = 0.2
ARROW_SIZE = (16, 150)
BALOON_SIZE = (100, 100)
HIT_RADIUS = 15
MISSES = 15
GAME_TIME = 60
定义游戏精灵类等:
import pygame
import math
import random
from os import path
from constants import *
#游戏精灵类
class Arrow(pygame.sprite.Sprite):
def __init__(self,game):
pygame.sprite.Sprite.__init__(self)
self.WIDTH = game.WIDTH
self.HEIGHT = game.HEIGHT
self.image_orig = pygame.transform.scale(game.arrow_img, ARROW_SIZE)
self.image_orig.set_colorkey(BLACK)
self.image = self.image_orig
self.rect = self.image.get_rect()
self.rect.centerx = self.WIDTH/2
self.rect.bottom = self.HEIGHT-100
self.rot = 0
self.speedx = 0
self.speedy = 0
self.range = 0
self.max_height = 0
self.release_angle = 0
self.set_vel = False
self.Released = False
self.releasex = self.rect.centerx
self.releasey = self.rect.bottom
self.cy = self.rect.centery
self.game = game
def update(self):
if self.Released:
self.speedy -= GRAVITY
self.rect.bottom -= self.speedy
self.rect.centerx += self.speedx
self.rot = (-math.atan2(self.speedx, self.speedy)*180/3.14) % 360
new_image = pygame.transform.rotate(self.image_orig, self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
# print "moving"
if self.rect.bottom < 0 or self.rect.left > self.WIDTH + 10 or self.rect.right < -10:
self.kill()
else:
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if mouse[1] > self.rect.centery and click[0] == 1:
self.set_vel = True
dist = math.sqrt(
math.pow(self.rect.centerx-mouse[0], 2)+math.pow(self.rect.bottom-mouse[1], 2))
# print dist
self.rect.centerx = mouse[0]
self.rect.centery = mouse[1]
# print(2*GRAVITY*(self.rect.centery-mouse[1]))
self.speedy = math.sqrt(2*GRAVITY*(-self.cy+mouse[1]))*4
self.speedx = self.speedy * \
(mouse[0]-self.releasex)/(self.cy-mouse[1])
self.rot = (-math.atan2(self.speedx, self.speedy)
* 180/3.14*0.5) % 360
new_image = pygame.transform.rotate(self.image_orig, self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
# print "setting velocity"
else:
if self.set_vel:
self.Released = True
self.game.last_arrow_time = pygame.time.get_ticks()
self.max_height = (self.rect.bottom-mouse[1])
self.range = (mouse[0]-self.rect.centerx)*2
# print "releasing"
# math.sqrt(math.pow(mouse[0]-self.rect.centerx,2)+math.pow(mouse[1]-self.rect.centery,2)) < 200:
else:
if (mouse[0]-self.rect.centerx) != 0:
theta = math.atan(
(mouse[1]-self.rect.bottom)/(self.rect.centerx-mouse[0]))
else:
theta = PI
move = theta-self.rot
self.rot = math.degrees(theta)
new_image = pygame.transform.rotate(
self.image_orig, self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
# print "rotating"
# print self.rot
# print theta
class Baloon(pygame.sprite.Sprite):
def __init__(self,game):
pygame.sprite.Sprite.__init__(self)
self.WIDTH = game.WIDTH
self.HEIGHT = game.HEIGHT
bcolor = random.choice(game.baloon_color)
temp = "assets/balloon_{}.png".format(bcolor)
self.image_orig = pygame.image.load(
path.join(path.dirname(__file__), temp))
if bcolor == "blue":
self.image_orig.set_colorkey(BLUE)
elif bcolor == "black":
self.image_orig.set_colorkey(BLACK)
elif bcolor == "green":
self.image_orig.set_colorkey(BRIGHT_GREEN)
elif bcolor == "red":
self.image_orig.set_colorkey(BRIGHT_RED)
self.image_orig = pygame.transform.scale(self.image_orig, BALOON_SIZE)
self.image = self.image_orig.copy()
self.rect = self.image.get_rect()
self.radius = HIT_RADIUS
temp = random.randrange(self.WIDTH - self.rect.width)
while (-150 < temp-self.WIDTH/2 < 150):
temp = random.randrange(self.WIDTH - self.rect.width)
self.rect.x = temp
self.rect.y = random.randrange(self.HEIGHT+100, self.HEIGHT+150)
self.speedy = random.randrange(-4, -1)
self.speedx = random.randrange(-3, 3)
self.game = game
self.last_update = pygame.time.get_ticks()
# print "baloon"
def update(self):
self.rect.y += self.speedy
if self.rect.top < -20 or self.rect.left < -25 or self.rect.right > self.WIDTH + 20:
self.kill()
self.game.misses += 1
三、效果展示
游戏规则的话:箭会出现在游戏界面底部中间位置,拉动它:即是鼠标左键拉动方向可自己调整,
直接向下拉动然后放箭射中气球即可。射中的越多积累的分数越高哦!
1)截图展示效果——
游戏开始界面如下:
游戏开始界面如下:
扎中气球效果如下:
游戏结束总成绩13分:
2)视频展示效果——
到此这篇关于Pygame实战之实现扎气球游戏的文章就介绍到这了,更多相关Pygame扎气球游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
来源:https://blog.csdn.net/weixin_55822277/article/details/122086118
标签:Pygame,扎气球,游戏
0
投稿
猜你喜欢
python实现比较文件内容异同
2022-11-10 19:25:27
Python 操作SQLite数据库详情
2024-01-18 04:45:55
MySQL如何查询当前正在运行的SQL语句
2009-02-13 13:40:00
Vue侦测相关api的实现方法
2024-05-09 09:52:46
使用Python开发游戏运行脚本成功调用大漠插件
2021-03-09 21:05:53
Python配置虚拟环境图文步骤
2023-10-13 01:37:40
ASP Framework_1_简介
2009-10-12 11:35:00
golang/python实现归并排序实例代码
2023-12-13 04:19:01
MySQL内连接和外连接及七种SQL JOINS的实现
2024-01-21 09:23:16
PHP实现批量生成App各种尺寸Logo
2023-07-23 03:59:59
实例详解JavaScript中setTimeout函数的执行顺序
2024-04-22 13:25:09
在Django的模板中使用认证数据的方法
2022-09-08 00:29:45
oracle 服务启动,关闭脚本(windows系统下)
2009-07-26 08:57:00
Utf-8和Gb2312乱码问题的终结
2008-04-05 14:04:00
python3+PyQt5 实现Rich文本的行编辑方法
2023-09-29 14:37:47
透彻掌握ASP分页技术
2009-03-09 18:26:00
不要使用@import[译]
2009-05-01 12:01:00
Python找出微信上删除你好友的人脚本写法
2023-11-09 15:55:58
Django发送邮件和itsdangerous模块的配合使用解析
2023-09-11 04:29:00
python3+selenium自动化测试框架详解
2022-01-29 18:26:01