该游戏加入声音和图片等资源,使得游戏效果更加丰富,运行游戏前请下载兔鼠大战游戏资源包,提取码在公众号中回复:兔鼠大战提取码,下载地址:https://pan.baidu.com/s/1n8enu4fL3b6Qc5T2RUbcVA


# -*- coding: utf-8 -*-
# 1 - Import library
import pygame
from pygame.locals import *
import math
import random# 2 - Initialize the game
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 againscreen.fill(0)# 6 - draw the screen elementsfor 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 badgersif 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 castlebadrect = 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 collisionsindex1 = 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 guyif badguy[0] < -64:badguys.pop(index)badguy[0] -= 7index += 1for badguy in badguys:screen.blit(badguyimg, badguy)# 6.4 - Draw clockfont = 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 barscreen.blit(healthbar, (5, 5))for health1 in range(healthvalue):screen.blit(health, (health1 + 8, 8))# 7 - update the screenpygame.display.flip()# 8 - loop through the eventsfor event in pygame.event.get():# check if the event is the X buttonif event.type == pygame.QUIT:# if it is quit the gamepygame.quit()exit(0)if event.type == pygame.KEYDOWN:if event.key == K_w:keys[0] = Trueelif event.key == K_a:keys[1] = Trueelif event.key == K_s:keys[2] = Trueelif event.key == K_d:keys[3] = Trueif event.type == pygame.KEYUP:if event.key == pygame.K_w:keys[0] = Falseelif event.key == pygame.K_a:keys[1] = Falseelif event.key == pygame.K_s:keys[2] = Falseelif event.key == pygame.K_d: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 playerif keys[0]:playerpos[1] -= 5elif keys[2]:playerpos[1] += 5if keys[1]:playerpos[0] -= 5elif keys[3]:playerpos[0] += 5# 10 - Win/Lose checkif 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源代码,请关注公众号:Python代码大全

Python兔鼠大战游戏源代码相关推荐

  1. python炫酷动画源代码_python_红心大战游戏源代码_满分原创作业

    [实例简介] python编写红心大战游戏,满分作业,音效动画传牌等效果酷炫. [实例截图] [核心代码] python_红心大战游戏源代码_满分原创作业 └── python_红心大战游戏_满分原创 ...

  2. Python版警察抓小偷游戏源代码,有多个难度级别

    Python版警察抓小偷游戏源代码,有多个难度级别,直接运行game.py,输入难度级别(1-13).不同的难度等级对应不同的图形. game.py """ Header ...

  3. Python飞行棋游戏源代码,基于socket网络通信的小游戏,可设置多个游戏房间及参与飞行棋游戏的玩家

    直接运行ludoServer.py即可,然后用浏览器打开http://127.0.0.1:4399/,完成房间创建.房间设置及玩家设备即可开始游戏 完整程序代码下载地址:Python飞行棋游戏源代码 ...

  4. Python 飞机大战游戏中 获取被击中飞机的坐标位置信息

    Python 飞机大战游戏中 获取被击中飞机的坐标位置信息 在参考现有的飞机大战游戏代码,写第一个python游戏,子弹击中飞机后,飞机消失,想着如果能有爆照效果就好了. 于是新建了一个爆炸效果的sp ...

  5. Python五子棋小游戏源代码,支持人机对战和局域网对战两模式

    Python五子棋小游戏源代码,支持人机对战和局域网对战两模式,程序运行截图: 核心程序代码 WuZi.py ''' Function:五子棋小游戏-支持人机和局域网对战 Author:Charles ...

  6. Python跳跳兔小游戏源代码,兔年必玩小游戏,兔年大吉

    Python跳跳兔小游戏源代码,兔年必玩小游戏,兔年大吉,小兔子跳跳,按空格键向上跳跃,按键盘方向键进行左右移动,以避开飞弹,以防被炸,还可以捡到火箭道具哦. 完整程序下载地址:Python跳跳兔小游 ...

  7. python飞机大战游戏高级_05.python实现飞机大战游戏

    python的核心编程跟着学习完了,终于到了第一个实战项目的演练,很是激动. 按着黑马老师指导的思路,代码主要分成两个模块主模块plane_main.py和工具模块plane_sprites.py. ...

  8. python坦克大战游戏_python实现简单坦克大战

    基于对面向对象编程的思想完成简单的坦克大战游戏.主要目的锻炼面相对象编程思想 同样的在使用python进行游戏编写时需要安装pygame模块 安装方法: pycharm安装方式:File --> ...

  9. Python小鸟管道游戏源代码及素材

    Python小鸟管道游戏源程序包含一个Vipgame2.py及一个图片素材包,小鸟管道游戏的素材包请在百度网盘下载,https://pan.baidu.com/s/1agk5zgre7CZHJ0-pd ...

最新文章

  1. usermod 命令、mkpasswd命令及用户密码管理
  2. 处理南通一客户:Fortigate 310B Firmware丢失
  3. 深入理解Java:注解(Annotation)自定义注解入门
  4. matlab相机畸变校正csdn,android广角相机畸变校正算法和实现示例
  5. 数据结构树的基本操作_树的各类基本操作(数据结构)
  6. Spring Boot干货系列:(二)配置文件解析
  7. Django源代码写DetailView与ListView
  8. jquery.cookie中的操作
  9. 【T-SQL基础】02.联接查询
  10. 关闭SSMS的事务自动提交,改为手动提交
  11. calc(~,mac电脑set-cookies要域名和请求域名相同
  12. 调整Poker 在Mac、win下通用键位,解决mac的复制粘贴问题
  13. 双因素方差分析 matlab,MATLAB的双因素有交互效应的方差分析
  14. 中国移动mda移动桌面助手
  15. springboot毕设项目线上跳蚤市场平台iy7e7(java+VUE+Mybatis+Maven+Mysql)
  16. 关于如何免费下载专利、英文文献等?
  17. 如何压缩图片200k以下?
  18. 阿里云centos镜像下载
  19. 删除 linux的ln文件夹,详解Linux ln 命令
  20. ODOO开发教程之图表

热门文章

  1. 三极管 NPN 开关电路
  2. MOJITO 发布一周,爬一波弹幕分析下
  3. c624芯片组的服务器,技嘉另类的服务器主板C422芯片组
  4. MobileNet相关知识整理
  5. PerfDog使用说明书
  6. Cordova开机画面
  7. unity做一个小游戏(适合零基础或者巩固加深unity中的工具类的用法)
  8. 27-java String 之间比较的幺蛾子
  9. iOS中调用短信、电话、邮件、Safari浏览器API
  10. 我国卫星导航工程将推动“羲和系统”建设