python控制台小游戏代码_python小游戏实现代码
早上逛CSDN首页就见到这么一篇教程。看了一下很有意思,就马上动手实现了一下。看看效果吧:
完整代码:
# -*- 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.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 arrows
for bullet in arrows:
index=0
velx=math.cos(bullet[0])*10
vely=math.sin(bullet[0])*10
bullet[1]+=velx
bullet[2]+=vely
if bullet[1]640 or bullet[2]480:
arrows.pop(index)
index+=1
for 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=35
else:
badtimer1+=5
index=0
for 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=0
for 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]+=1
badguys.pop(index)
arrows.pop(index1)
index1+=1
# 6.3.3 - Next bad guy
if badguy[0]
badguys.pop(index)
badguy[0]-=7
index+=1
for 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_w:
keys[0]=True
elif event.key==K_a:
keys[1]=True
elif event.key==K_s:
keys[2]=True
elif event.key==K_d:
keys[3]=True
if event.type == pygame.KEYUP:
if event.key==pygame.K_w:
keys[0]=False
elif event.key==pygame.K_a:
keys[1]=False
elif event.key==pygame.K_s:
keys[2]=False
elif event.key==pygame.K_d:
keys[3]=False
if event.type==pygame.MOUSEBUTTONDOWN:
shoot.play()
position=pygame.mouse.get_pos()
acc[1]+=1
arrows.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]-=5
elif keys[2]:
playerpos[1]+=5
if keys[1]:
playerpos[0]-=5
elif keys[3]:
playerpos[0]+=5
#10 - Win/Lose check
if pygame.time.get_ticks()>=90000:
running=0
exitcode=1
if healthvalue<=0:
running=0
exitcode=0
if acc[1]!=0:
accuracy=acc[0]*1.0/acc[1]*100
else:
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().centerx
textRect.centery = screen.get_rect().centery+24
screen.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().centerx
textRect.centery = screen.get_rect().centery+24
screen.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()
运行成功的时候有点小激动,不等不感叹,国外一个13岁的小孩都这么给力,让我情何以堪?
我是看着原文实现的代码,因为译文里面有些地方有问题,游戏资源的下载连接也没给,原文里面有。
由于pygame的首页上不去没法上它的官网下载模块,ubuntu下其实很方便,一个命令就搞定了:
sudo apt-get install python-pygame
这个游戏是用的python2.7.3做的,我的系统默认安装的就是这个版本,我也就没有改了。
python控制台小游戏代码_python小游戏实现代码相关推荐
- python编写小游戏代码_Python小游戏之300行代码实现俄罗斯方块
Python小游戏之300行代码实现俄罗斯方块 来源:中文源码网 浏览: 次 日期:2019年11月5日 [下载文档: Python小游戏之300行代码实现俄罗斯方块.txt ] (友情提示:右键点上 ...
- python游戏设计_python小游戏设计入门1-了解游戏
从小到大玩过很多的游戏,在我小时候,能玩游戏的地方不多,那时玩游戏都是偷摸玩的,只要是个游戏就觉得非常有趣,比较经典的有魂斗罗,拳皇,超级玛丽,贪吃蛇,俄罗斯方块等游戏:发展到现在,玩游戏已经成为生活 ...
- python 小甲鱼 代码_Python小代码
先自我介绍一下,本人是正在自学Python的小白,没事分享一下自己写的小代码,欢迎在评论区补充. 游戏管理系统: 代码如下: def healthe(m):if m=="Y"or ...
- python300行代码_Python:游戏:300行代码实现俄罗斯方块
本文代码基于 python3.6 和 pygame1.9.4.python 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.可是想到旋转,停靠,消除等操做,感受 ...
- python外星人入侵游戏图片_Python外星人入侵游戏编程完整版
PYTHON游戏编程外星人入侵的完整实现思路,具体内容如下 准备工作:下载python,比如Anaconda3(64 bit),导入pygame游戏包 1.外星人设置,alien.py,代码: imp ...
- python编写小程序实例_python小程序开发实例
商品和服务质量,是用户最关心的,运营者要保证质量,并把用户的完整信息的质量传达给用户,将影响用户的留存与转化的. 再小的店也有自己的品牌!一张小程序码可以让消费者看到你店里的详细经营情况和折扣优惠卷, ...
- python模拟全部代码_Python模拟登陆实现代码
下面分享一个使用Python进行网站模拟登陆的小例子. 原理 使用Cookie技术,绕开网站登录验证.要使用到cookielib库.流程: 创建一个保存Cookie的容器,可选的有CookieJar, ...
- python使用mysql实例教程_Python操作Mysql实例代码教程在线版(查询手册)_python
实例1.取得MYSQL的版本 在windows环境下安装mysql模块用于python开发 MySQL-python Windows下EXE安装文件下载 复制代码 代码如下: # -*- coding ...
- python雷达图怎么做_PYTHON绘制雷达图代码实例
这篇文章主要介绍了PYTHON绘制雷达图代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.雷达图 import matplotlib.py ...
- python中formatter的用法_Python pyplot.FuncFormatter方法代码示例
本文整理汇总了Python中matplotlib.pyplot.FuncFormatter方法的典型用法代码示例.如果您正苦于以下问题:Python pyplot.FuncFormatter方法的具体 ...
最新文章
- 模拟线程切换 C++
- 阿里巴巴陈武:通过亿级用户App的实践验证,锤炼高质量APM体系
- 119.CSMA/CD
- Winform中跨窗体设置ZedGraph的属性并刷新曲线图
- this.$nextTick()的使用场景
- sqoop实现Mysql、Oracle与hdfs之间数据的互导
- 最高调恋爱方式,简直“公开处刑”......
- fullpage常用配置
- Java的四种引用——强软弱虚
- 单源最短路径(最短路)
- wkhtmltopdf生成的pdf分页后文字重叠
- 产品流程图的制作方法详解
- ubuntu显卡驱动下载安装
- 解决System进程占用80端口,关闭IIS服务
- 【新手村专属】亚太杯数模参赛经验
- .NET Framework各个版本(3.5 - 4.0)
- 移动端可移动小图标(vue版)
- 面试专题——Redis
- CMS垃圾收集器详解
- 以Base64字符串形式上传文件——服务器端Java/Servlet接受请求数据中的字符串内容并转换成文件