最近一边学习pygame模块一边做了个小游戏,完成功能如下:

  1. 鼠标右键控制移动
  2. 人物跟随鼠标方向转动
  3. 鼠标左键控制攻击
  4. 动画效果
  5. 血量计数效果

    画面比较乱入,请自行过滤。直接上代码:
# coding: utf-8
import pygame
from pygame.locals import *     # 导入pygame库中的一些常量
from sys import exit            # 导入sys库中的exit函数
from gameobjects.vector2 import Vector2
from math import *# 定义窗口的分辨率
SCREEN_WIDTH = 603
SCREEN_HEIGHT = 603# 初始化游戏
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT], 0, 32)     # 初始化一个用于显示的窗口
pygame.display.set_caption('LOL')       # 设置窗口标题#加载并转换图像
background = pygame.image.load("background.jpg").convert()  # 加载图片资源
major_cursor = pygame.image.load("major.png").convert_alpha()
mouse_cursor = pygame.image.load("cursor.png").convert_alpha()
fire_cursor = pygame.image.load("fire.png").convert_alpha()
boom_cursor = pygame.image.load("boom.png").convert_alpha()major_pos_new = major_pos_old = (20.0, 550.0)
mouse_pos = (300.0, 300.0)
sunflower_pos = (35, 420)
bullets = []def load_image(file, width=None, number=None):try:surface = pygame.image.load(file).convert_alpha()except pygame.error:raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error()))if width == None:return surfaceheight = surface.get_height()return [surface.subsurface(Rect((i * width, 0), (width, height))) for i in range(number)]   # 返回切片后的图片列表,按帧控制显示class SunFlower(pygame.sprite.Sprite):_rate = 100_width = 82_height = 77_number = 18_life = 100images = []def __init__(self):self.order = 0pygame.sprite.Sprite.__init__(self)if len(self.images) == 0:self.images = load_image("sunflower.png", self._width, self._number)self.image = self.images[self.order]self.rect = Rect(0, 0, self._width, self._height)self.life = self._lifeself.passed_time = 0def update(self, passed_time):self.passed_time += passed_timeself.order = ( self.passed_time // self._rate ) % self._numberif self.order == 0 and self.passed_time > self._rate:self.passed_time = 0return self.images[self.order]if __name__ == '__main__':sunflower = SunFlower()clock = pygame.time.Clock()screen.blit(major_cursor, major_pos_old) # 主角#游戏主循环while True:screen.blit(background, (0,0)) # 背景# major_pos_old = straight_move(major_pos_old, major_pos_new)for event in pygame.event.get():#接收到退出事件后退出程序if event.type == QUIT:exit()elif event.type ==  MOUSEBUTTONDOWN:pressed_array = pygame.mouse.get_pressed()for index in range(len(pressed_array)):if pressed_array[index]:if index == 0:major_pos_new = major_pos_oldbullet_pos = (major_pos_old[0], major_pos_old[1])vector_direction = Vector2.from_points(bullet_pos, pygame.mouse.get_pos())vector_direction.normalize()bullets.append([bullet_pos, vector_direction]) # 添加子弹位置及方向elif index == 1:passelif index == 2:major_pos_new = pygame.mouse.get_pos()              time_passed = clock.tick()for bullet in bullets:print(bullet[0][0], bullet[0][1])if (int(bullet[0][0]) in range(20, 70)) and (int(bullet[0][1]) in range(410, 460)):sunflower.life -= 10bullets.remove(bullet)elif (bullet[0][0] > 0 and bullet[0][0] < 603) and (bullet[0][1] > 0 or bullet[0][1] < 603):bullet[0] += bullet[1] * time_passed * 0.5  # 设置子弹按帧移动screen.blit(fire_cursor, bullet[0])else:bullets.remove(bullet)vector_to_new = Vector2.from_points(major_pos_old, major_pos_new)vector_to_new.normalize()major_pos_old += vector_to_new * time_passed * 0.1vector_mouse = Vector2.from_points(major_pos_old, pygame.mouse.get_pos())vector_mouse.normalize()# print(degrees(acos(vector_mouse[0])))if vector_mouse[1] >= 0:derection = -1else:derection = 1if vector_mouse[0] >= 0:major_sufurce = major_cursorelse:major_sufurce = pygame.transform.flip(major_cursor, False, True)   # 根据Y轴翻转major_cursor_ratation = pygame.transform.rotate(major_sufurce, derection * degrees(acos(vector_mouse[0]))) # 设置偏移角度screen.blit(major_cursor_ratation, (major_pos_old[0]-30, major_pos_old[1]-15))if sunflower.life > 0:img = sunflower.update(time_passed)screen.blit(img, sunflower_pos)my_font = pygame.font.SysFont("arial", 15) # 设置血量text_surface = my_font.render(str(sunflower.life), True, (255,0,0))screen.blit(text_surface, (sunflower_pos[0]+25, sunflower_pos[1]-15))else:screen.blit(boom_cursor, sunflower_pos)screen.blit(mouse_cursor, pygame.mouse.get_pos())pygame.mouse.set_visible(False)  # 设置鼠标不可见pygame.display.update() #刷新画面

sunflower.png下载地址:
(https://img-blog.csdn.net/20180307103256329?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvR2l2ZU1lRml2ZV9Z/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

基于pygame做的小游戏相关推荐

  1. 基于pygame的射击小游戏制作(一)让飞船动起来

    基于pygame的射击小游戏制作(一)让飞船动起来 一.文件结构 alien_invasion.py 是整个系统的主文件,用来创建游戏中的一系列对象,ai_settings存储设置.screen存储显 ...

  2. 基于Pygame的Python小游戏,《这是一场滑稽与阴险的较量》

    选用贴吧最火的表情包滑稽和阴险表情包,肯定都认得出吧,背景由胡大佬提供,添加了很多细节,一款射击闯关类小游戏,滑稽会不断逼近阴险,在这之前你必须把阴险全部击落,左上角是你的生命,被滑稽撞到则减少一生命 ...

  3. 基于pygame的滑雪小游戏

    import pygame import sys import random from pygame.locals import * class SkierClass(pygame.sprite.Sp ...

  4. 基于pygame的射击小游戏制作(二)射击子弹

    在本篇文章中,将学习如何创建子弹移动的元素,如何在屏幕中让子弹向上飞驰,如何删除超过屏幕边界的子弹. 1.编程思路 1.1 创建一个子弹 功能描述:玩家将按空格键时发射子弹,子弹在屏幕中向上穿行,抵达 ...

  5. 基于pygame的射击小游戏制作(五)绘制开始按钮

    在本篇文章中,主要学习如何在Pygame中绘制按钮,并点击按钮开始游 一.编程思路 1.1 修改游戏活动状态 在上篇文章中,我们通过game_active标志来控制游戏的进行与停止.我们需要在点击&q ...

  6. 基于pygame的射击小游戏制作(四)击杀外星人

    在本篇文章中,主要学习射击子弹时外星人消失,达到击杀外星人的效果 一.编程思路 1.1击杀 我们需要在碰撞发生后让外星人立即消失,故在更新子弹的位置后检测碰撞.我们创建一个字典,这个字典的每一个键都是 ...

  7. 基于pygame的射击小游戏制作(三)让外星人动起来

    在本篇文章中,将学习如何创建一群外星人,如何使用嵌套循环来创建元素网格,如何移动外星人 1.编程思路 1.1 创建外星人 创建Alien.py 文件,该文件配置与Ship.py类似,每个外星人最初都出 ...

  8. eclipse如何做java游戏,:#急需基于eclipse的JAVA小游戏源代码!!!#-南开游戏网...

    #急需基于eclipse的JAVA小游戏源代码!!!# 2020-11-07 10:21:13 广告 急需基于eclipse的JAVA小游戏源代码!!! maxueruby001@163.COM 楼主 ...

  9. python手机版做小游戏代码大全-Python大牛手把手教你做一个小游戏,萌新福利!...

    原标题:Python大牛手把手教你做一个小游戏,萌新福利! 引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规 ...

最新文章

  1. JAVA Static方法与单例模式的理解
  2. 组合特征(四)linearsvm-tfidf(word)+lr-tfidf(article)
  3. Kafka 基本原理(8000 字小结)
  4. DISPLAY环境变量的作用
  5. select2的一些特性效果
  6. hibernate注解方式来处理映射关系
  7. mysql8解决区分大小写问题_球磨机“包球”和“饱磨”分不清?点进来快速区分,解决问题...
  8. 怎样呵护友谊_呵护真正的友情,助力漫长的人生
  9. Java蓝桥杯--基础练习(7)特殊回文数
  10. CDH Sentry介绍
  11. 赌一把:整合了30家独立报告后,我发现了2020年的五大安全趋势
  12. Python3中的GBK、UTF-8和Unicode
  13. 有效压缩量子数据的量子自动编码器——Quantum autoencoders for efficient compression of quantum data论文翻译
  14. 使用片段嵌入进行文档搜索
  15. 2023最新自动采集影视CMS程序源码+全开源的/功能也强大
  16. matlab中在图像上划线 画圈的方法
  17. Lytro光场相机的原理,科普,简单地进行了解
  18. CSS(b站学习记录)
  19. python傻瓜瓜入门
  20. “战”在新高地,星沙加“数”前行

热门文章

  1. 东京奥运会:创意可以接地气,但别接“阴气”
  2. Oracle 12c 的 hr_main.sql
  3. Blockchain for Internet of Energy management: Review, solutions, and challenges
  4. 计算机教师继续教育心得,教师继续教育心得体会
  5. 约数的一些定理——数论
  6. 会计学原理学习笔记——第三章——账户与复式记账(3.4生产准备业务核算——材料采购业务核算)
  7. 2021数模国赛A题
  8. C++基本数据类型的字节数、范围大小、溢出处理
  9. React.js -学习总结1
  10. 计算机网络-自顶向下(学习笔记)