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]<-64 or bullet[1]>640 or bullet[2]<-64 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]<-64:
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基础代码大全
[实例简介] python代码大全,适用于基础python学习者,里面的代码基本上是基础学习者必经过程. Python学习入门很快,但学习之路任重道远 [实例截图] [核心代码] python代码大全 ...
- python手机版做小游戏代码大全-Python大牛手把手教你做一个小游戏,萌新福利!...
原标题:Python大牛手把手教你做一个小游戏,萌新福利! 引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规 ...
- python小游戏代码大全-Python编写的点灯小游戏代码
Python语言编写的点灯小游戏代码及思路如下: 点灯游戏及其求解的方法, 点灯游戏的游戏规则: (1)有个N行N列的灯板,当你开关其中一盏灯: (2)它和上下左右的灯的状态全部反转,目标是将全部的灯 ...
- python小游戏代码大全-Python小游戏之300行代码实现俄罗斯方块
前言 本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很 ...
- python程序编程代码大全,python编程代码详解
大家好,本文将围绕python程序编程代码大全展开说明,python编程游戏代码是一个很多人都想弄明白的事情,想搞清楚python代码大全简单需要先了解以下几个事情. 1.python编程例子有哪些? ...
- python有趣的图案代码20行,有趣的代码大全python
python画五角星代码 python是一种强大的编程语言,通过使用python,我们可以进行各种各样的图案.语句.动画等等编程.你知道用python画五角星的代码是什么吗?今天小编就来为大家详细演示 ...
- python常用代码大全-Python常用库大全及简要说明
环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具.官网 pyenv:简单的 Python 版本管理工具.官网 Vex:可以在虚拟环境中执行命令.官网 v ...
- python常用代码大全-Python常用库大全
Python常用库大全,看看有没有你需要的. 环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具 ...
- python常用代码大全-Python常用库大全,看看有没有你需要的
环境管理 管理 Python 版本和环境的工具 p – 非常简单的交互式 python 版本管理工具. pyenv – 简单的 Python 版本管理工具. Vex – 可以在虚拟环境中执行命令. v ...
- python画图代码大全-Python实现画图软件功能方法详解
概述 虽然Python的强项在人工智能,数据处理方面,但是对于日常简单的应用,Python也提供了非常友好的支持(如:Tkinter),本文主要一个简单的画图小软件,简述Python在GUI(图形用户 ...
最新文章
- MindSpore张量mindspore::tensor
- 使用Python,OpenCV实现简单的场景边界/拍摄转换检测器
- addEventListener()与removeEventListener()
- 内核与ramdisk到底是什么关系?
- java多态替换switch_第1章 重构,第一个案例(3):运用多态取代switch
- 根据 dba_errors 制定 数据库报警邮件
- 腾讯测试鸿蒙系统,爆料:荣耀 30 Pro已开始测试华为鸿蒙系统
- jdk 细粒度锁_使用JDK 8轻松进行细粒度排序
- 源码网络-推荐精品×××站
- srsLTE源码学习:NAS非接入层、PDCP分组数据汇聚协议、PDU 协议数据单元 头文件
- html5视频播放器使用,视频站启用html5播放器
- 使用NoSQL Manager for MongoDB客户端连接mongodb
- Python基础-循环
- Silverlight 5 RC新特性探索系列:15.Silverlight 5 RC 对OpenType字体属性的支持
- HDOJ_ACM_统计问题
- MATLAB更改初始工作路径
- 相见恨晚,真的很喜欢Udacity
- 国内外优秀程序员的博客全在这了,请查收
- 手把手教你用 Java 实现word、excel、ppt、txt等办公文件在线预览功能!
- ConvMixer:Patches Are All You Need
热门文章
- SaltStack介绍——SaltStack是一种新的基础设施管理方法开发软件,简单易部署,可伸缩的足以管理成千上万的服务器,和足够快的速度控制,与他们交流...
- python set过滤
- ios - 使用@try、catch捕获异常:
- 支付宝分库分表中间件--zdal简介
- C++ 虚函数、多态
- MapInfo常见数据格式
- wordpress评论插件:多说
- 登录之验证码相关实现
- 【php】php5.0以上,instanceof 用法
- delphi ---break,exit,continue等跳出操作的区别