前阵子看到国外一个12岁的孩子写的兔獾大战游戏,心生敬佩,想当年我还是12岁的时候还不知电脑为何物,连小霸王都未曾玩过。自己也未曾想去搞游戏开发,纯属自娱自乐。在此心态下,依葫芦画瓢重新架构了一下该游戏。
一、准备环境:更改设置pip 国内镜像
在使用pycharm来制作小游戏写代码的时候需要编译环境,使用pip镜像源,由于pip管理工具安装库文件时,默认使用国外的源文件,因此在国内的下载速度会比较慢,可能只有50KB/s。幸好,国内的一些顶级科研机构已经给我们准备好了各种镜像,下载速度可达2MB/s。所以需要更改设置成pip国内镜像。

国内源:
新版ubuntu要求使用https源,要注意。

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

  1. 临时使用
    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

可以在使用pip的时候加参数-i https://pypi.tuna.tsinghua.edu.cn/simple

例如:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider,这样就会从清华这边的镜像去安装pyspider库。

2.默认使用

  windows系统使用cmd(命令指示符)快速设置

1.pip install pip -U # 升级pip到最新版本
2.pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
注:在命令指示符里依次敲以上代码,以上例子都是用的清华大学的镜像

二、进行代码的编写

# -*-conding: utf-8  -*-
#如果要在python2的py文件里面写中文,则必须要添加一行声明文件编码的注释,否则python2会默认使用ASCII编码。
#如果是python3及其以上的版本就可以不写第一行注释。
# 1 - Import library
import pygame        #import语句:在模块模块定义好后,我们可以使用 import 语句来引入模块
from pygame.locals import *# from 语句从模块中导入一个指定的部分到当前命名空间中
import math
import random# 2 - Initialize the game          #initialize (隐藏摘要) 预置(初始状态bai), 初始化这个游戏
keys = [False, False, False, False]
playerpos = [100, 100]
acc = [0, 0]
arrows = []
badtimer = 100
badtimer1 = 0
badguys = [[640, 100]]
healthvalue = 194
pygame.init()
width, height = 640, 480    #设置页面宽高
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("兔鼠大战")pygame.mixer.init()# 3 - Load images      下载图片
player = pygame.image.load("resources/images/dude.png")
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
arrow = pygame.image.load("resources/images/bullet.png")
badguyimg1 = pygame.image.load("resources/images/badguy.png")
gameover = pygame.image.load("resources/images/gameover.png")
youwin = pygame.image.load("resources/images/youwin.png")
healthbar = pygame.image.load("resources/images/healthbar.png")
health = pygame.image.load("resources/images/health.png")
badguyimg = badguyimg1
# 3.1 - Load audio   下载音乐
hit = pygame.mixer.Sound("resources/audio/explode.wav")
enemy = pygame.mixer.Sound("resources/audio/enemy.wav")
shoot = pygame.mixer.Sound("resources/audio/shoot.wav")
hit.set_volume(0.05)
enemy.set_volume(0.05)
shoot.set_volume(0.05)
pygame.mixer.music.load('resources/audio/moonlight.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)
# 4 - keep looping through   保持一个圈通过
running = 1
exitcode = 0
while running:badtimer -= 1# 5 - clear the screen before drawing it again  再次绘制之前清除屏幕screen.fill(0)# 6 - draw the screen elements    绘制屏幕元素for x in range(width // grass.get_width() + 1):for y in range(height // grass.get_height() + 1):screen.blit(grass, (x * 100, y * 100))screen.blit(castle, (0, 30))screen.blit(castle, (0, 135))screen.blit(castle, (0, 240))screen.blit(castle, (0, 345))# screen.blit(player, (100,100))# screen.blit(player, playerpos)position = pygame.mouse.get_pos()angle = math.atan2(position[1] - (playerpos[1] + 32), position[0] - (playerpos[0] + 26))playerrot = pygame.transform.rotate(player, 360 - angle * 57.29)playerpos1 = (playerpos[0] - playerrot.get_rect().width / 2, playerpos[1] - playerrot.get_rect().height / 2)screen.blit(playerrot, playerpos1)# 6.2 - Draw arrowsfor bullet in arrows:index = 0velx = math.cos(bullet[0]) * 10vely = math.sin(bullet[0]) * 10bullet[1] += velxbullet[2] += velyif bullet[1] < -64 or bullet[1] > 640 or bullet[2] < -64 or bullet[2] > 480:arrows.pop(index)index += 1for projectile in arrows:arrow1 = pygame.transform.rotate(arrow, 360 - projectile[0] * 57.29)screen.blit(arrow1, (projectile[1], projectile[2]))# 6.3 - Draw badgers绘制老鼠if badtimer == 0:badguys.append([640, random.randint(50, 430)])badtimer = 100 - (badtimer1 * 2)if badtimer1 >= 35:badtimer1 = 35else:badtimer1 += 5index = 0for badguy in badguys:# 6.3.1 - Attack castle  攻击伤害badrect = pygame.Rect(badguyimg.get_rect())badrect.top = badguy[1]badrect.left = badguy[0]if badrect.left < 64:hit.play()healthvalue -= random.randint(5, 20)badguys.pop(index)# 6.3.2 - Check for collisions   检查是否有碰撞index1 = 0for bullet in arrows:bullrect = pygame.Rect(arrow.get_rect())bullrect.left = bullet[1]bullrect.top = bullet[2]if badrect.colliderect(bullrect):enemy.play()acc[0] += 1badguys.pop(index)arrows.pop(index1)index1 += 1# 6.3.3 - Next bad guy 下一个老鼠if badguy[0] < -64:badguys.pop(index)badguy[0] -= 7index += 1for badguy in badguys:screen.blit(badguyimg, badguy)# 6.4 - Draw clock  绘制计时器font = pygame.font.Font(None, 24)survivedtext = font.render(str((90000 - pygame.time.get_ticks()) / 60000) + ":" + str((90000 - pygame.time.get_ticks()) / 1000 % 60).zfill(2), True, (0, 0, 0))textRect = survivedtext.get_rect()textRect.topright = [635, 5]screen.blit(survivedtext, textRect)# 6.5 - Draw health bar  绘制生命条screen.blit(healthbar, (5, 5))for health1 in range(healthvalue):screen.blit(health, (health1 + 8, 8))# 7 - update the screen   更新屏幕pygame.display.flip()# 8 - loop through the events  循环处理事件for event in pygame.event.get():# check if the event is the X button  检查游戏是否失败`在这里插入代码片`if event.type == pygame.QUIT:# if it is quit the game   如果是,请退出游戏pygame.quit()exit(0)if event.type == pygame.KEYDOWN:   #设置移动的按键,个人认为设置成上下左右键方便操作,鼠标左键单击发动攻击。if event.key == K_UP:          # k_wkeys[0] = Trueelif event.key == K_LEFT:      # k_akeys[1] = Trueelif event.key == K_DOWN:      # k_skeys[2] = Trueelif event.key == K_RIGHT:     # k_dkeys[ 3 ] = True         #以上设置按键也可以设置成w,a,s,d  #后是对应的代码,如用这个下面也需要更换成相同的if event.type == pygame.KEYUP:if event.key == pygame.K_UP:   #这里跟上面的设置键需要设置成相同的keys[0] = Falseelif event.key == pygame.K_LEFT:keys[1] = Falseelif event.key == pygame.K_DOWN:keys[2] = Falseelif event.key == pygame.K_RIGHT:keys[3] = Falseif event.type == pygame.MOUSEBUTTONDOWN:shoot.play()position = pygame.mouse.get_pos()acc[1] += 1arrows.append([math.atan2(position[1] - (playerpos1[1] + 32), position[0] - (playerpos1[0] + 26)), playerpos1[0] + 32,playerpos1[1] + 32])# 9 - Move player  移动时播放的音频if keys[0]:playerpos[1] -= 5elif keys[2]:playerpos[1] += 5if keys[1]:playerpos[0] -= 5elif keys[3]:playerpos[0] += 5# 10 - Win/Lose check  检查是否赢得/输掉游戏if pygame.time.get_ticks() >= 90000:running = 0exitcode = 1if healthvalue <= 0:running = 0exitcode = 0if acc[1] != 0:accuracy = acc[0] * 1.0 / acc[1] * 100else:accuracy = 0
# 11 - Win/lose display  赢得/输掉游戏  不再继续游戏
if exitcode == 0:pygame.font.init()font = pygame.font.Font(None, 24)text = font.render("Accuracy: " + str(accuracy) + "%", True, (255, 0, 0))textRect = text.get_rect()textRect.centerx = screen.get_rect().centerxtextRect.centery = screen.get_rect().centery + 24screen.blit(gameover, (0, 0))screen.blit(text, textRect)
else:pygame.font.init()font = pygame.font.Font(None, 24)text = font.render("Accuracy: " + str(accuracy) + "%", True, (0, 255, 0))textRect = text.get_rect()textRect.centerx = screen.get_rect().centerxtextRect.centery = screen.get_rect().centery + 24screen.blit(youwin, (0, 0))screen.blit(text, textRect)
while 1:for event in pygame.event.get():if event.type == pygame.QUIT:pygame.quit()exit(0)pygame.display.flip()

成果显示如下

这次的分享就到这里了,如果觉得文章还不错的话,记得点赞支持一下哦

Python开发摸鱼小游戏——兔獾大战,越玩越上瘾相关推荐

  1. python摸鱼小游戏:小鸟管道游戏【送源码】

    嗨害大家好鸭~我是小熊猫❤ 国庆假期马上就要结束辽~ 相信大家和我一样不想上班 只想摸鱼吧? 刚开工嘛~那肯定是要摸一下鱼的啦 ~ ~ ~ 今天来给大家整一个小鸟管道小游戏 让大家赶快从假期的时差里倒 ...

  2. 使用Java在线编译器手搓一款摸鱼小游戏

    这其实是一篇摸鱼指南. 这篇文章主要是学习如何使用Java在线编译器在浏览器中制作一个 猜数字的小游戏 ,真的只需要有网+有浏览器就可以.想学习Java编程的小伙伴可以静下来认真学习,纯粹想摸鱼的小伙 ...

  3. Python开发一个滑雪小游戏

    擅长领域:Python开发一个小游戏 今日重点:一步步分析and越过亚马逊的反爬虫机制 一.如何搭建开发环境环境 一起来学pygame吧 游戏开发30例(开篇词)--环境搭建+游戏效果展示 windo ...

  4. Python开发接水果小游戏

    我研发的Python游戏引擎Pylash已经更新到1.4了.如今我们就来使用它完毕一个极其简单的小游戏:接水果. 下面是游戏截图: 游戏操作说明:点击屏幕左右两边或者使用键盘方向键控制人物移动.使人物 ...

  5. java摸鱼小游戏之斗地主(一)

    斗地主 一.准备工作 1.实体 (1)首先就是扑克牌实体 (2)然后就是游戏结束返回类 (3)出牌类 (4)玩家类 (5)桌面信息类 (6) ai类 2.主体与工具 (1)刷屏方法 (2)静态字段(包 ...

  6. java五子棋 · 上班摸鱼小游戏

    上班实在无聊就用java写个五子棋小游戏 编译成class文件后可在cmd窗口中运行 下面附上源码: import java.util.Scanner;public class FiveStepsCh ...

  7. python连连看小游戏_利用Python制作一个连连看小游戏,边学边玩!

    导语 今天我们将制作一个连连看小游戏,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: pygame模块: 以及一些Python自带的模块 环境搭建 安装Python并添加到环 ...

  8. 疯狂星期四;python摸鱼小游戏,重温童年经典

    前言 今天是6.1号儿童节,刚好也是星期四,疯狂星期四+六一儿童节岂不是得喂我111(50+61)

  9. python开发的连连看小游戏

    说明: 1.在网上找了一个基础版本改进而来,大概增加了200行代码,相对原版要复杂一些: 2.界面采用tkinter开发,比较简单: 3.新增了连接线功能: 4.新增了积分功能: 5.新增了陷入死局时 ...

最新文章

  1. 2017年如何在移动端优雅的使用flex
  2. 给Python的类和对象动态增加属性和方法
  3. zoj 3696 Alien's Organ(泊松分布)
  4. c malloc 头文件_C语言提高篇_malloc,realloc和calloc的区别
  5. 静观接入网易云信IM的秀品,如何在圣诞让她们疯狂剁手
  6. Win10还原被Windows Defender隔离的文件
  7. html如何添加文档,如何在HTML中添加行
  8. php期末考试题机考_phP基础知识期末考试题
  9. tc275单片机的内核_TC275开发板学习心得(一)
  10. [转载]转,Oracle中关于处理小数点位数的几个函数,取小数位数,Oracle查询函数...
  11. 词汇的积累 —— 同义反复、写景状物
  12. iZotope Neutron 3 Advanced for mac(智能混音插件包)
  13. 64位Windows7环境下,Eclipse集成svn后出现Failed to load JavaHL Library的解决办法
  14. python xlwings下载_python xlwings excel报表自动化 系列文章精讲 (一)
  15. JDK_API官方标准中文版(希望对大家有用)
  16. 微信商家收款码和个人收款码有什么区别?
  17. OpenCV每日函数 对象追踪模块 Meanshift算法
  18. sonic云真机入门教程
  19. ElasticSearch Cause: Cluster state has not been recovered yet, cannot write to the [null] index
  20. python-return_全局局部变量_函数名用法_函数嵌套

热门文章

  1. Request用法解析
  2. 2020升降机司机模拟考试系统及升降机司机操作证考试
  3. 惊艳亚洲CES展 长虹智慧家庭应用解决方案全面市场化
  4. linux系统制作系统盘,制作Linux启动盘的四种方法
  5. 传智健康-检查项管理
  6. QUIC详解(基于UDP的低延时网络传输层协议)
  7. 简单人脸识别一之使用opencv+cnn网络实现人脸识别
  8. 安装MySql8.0详细教程
  9. 风控策略精准运维的制胜点,一个重要却容易被轻视的内容
  10. Typora使用技巧(二):侧边栏大纲视图折叠