写简单游戏,学编程语言-python篇:传说哥大战剧毒术士
上篇写的动画示例其实算不上一个游戏,顶多算是利用pygame进行的图形操作,今天着手实现一个小游戏:传说哥大战剧毒术士。名字很玄乎,其实就是最简单的一个射击游戏。好了废话不多说,先上截图吧:

一、初始化程序和定义变量
import pygame
import math
import random # 1 - Initialize the game
pygame.init()
# prepare the variables
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
playerpos=[100,100]
speed_x=5
speed_y=5
speed_bullet=10
speed_shit=3
shot=0
alive=True
bullet=[]
shit=[]
green=[0,255,0]
# 2 - Load images
player = pygame.image.load("1.bmp")
badguy = pygame.image.load("2.bmp")
backgroud= pygame.image.load("3.bmp")
bullet_bmp=pygame.image.load("4.bmp")
shit_bmp=pygame.image.load("5.bmp")
player.set_colorkey([0,0,0])
badguy.set_colorkey([0,0,0])
shit_bmp.set_colorkey([255,255,255])
第一部分声明的变量中主要是传说哥的移动速度,子弹的数据,剧毒术士喷出的便便速度等。第二部分预载入图片资源:角色,背景图等。注意设置set_colorkey进行透明色处理(书上有介绍)。
二、响应事件处理相关
# 4 - clear the screen before drawing it again
screen.fill(0)
# 5 - draw the backgroud
for x in range(width/backgroud.get_width()+1):
for y in range(height/backgroud.get_height()+1):
screen.blit(backgroud,(x*90,y*65))
# 6 - 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 == pygame.K_w:
playerpos[1]-=speed_y
elif event.key == pygame.K_s:
playerpos[1]+=speed_y
elif event.key == pygame.K_a:
playerpos[0]-=speed_x
elif event.key == pygame.K_d:
playerpos[0]+=speed_x
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+32))
playerrot = pygame.transform.rotate(player, 360-angle*57.29)
playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
if event.type==pygame.MOUSEBUTTONDOWN:
position=pygame.mouse.get_pos()
bullet.append([math.atan2(position[1]-(playerpos1[1]+32),
position[0]-(playerpos1[0]+32)),playerpos1[0]+32,playerpos1[1]+32])
这一部分首先将加载进的背景图片利用blit函数贴到窗口里。随后进入事件循环:这里主要是两种事件:WSAD控制人物运动,鼠标点击射出子弹,由于是用鼠标控制射击方向,因此需要获取图片的新位置,简单的数学知识,偏转角计算如下图,这块的处理具体参照一篇不错的文章http://bbs.chinaunix.net/thread-4094150-1-2.html,这个小东东借鉴了里面很多知识。

三、处理对战逻辑
# 7 - process fight
screen.blit(playerrot, playerpos1)
for oneshot in bullet:
index=0
velx=math.cos(oneshot[0])*speed_bullet
vely=math.sin(oneshot[0])*speed_bullet
oneshot[1]+=velx
oneshot[2]+=vely
if oneshot[1]<-64 or oneshot[1]>640 or oneshot[2]<-64 or oneshot[2]>480:
bullet.pop(index)
index+=1
for eachone in bullet:
pos_x=int(eachone[1])
pos_y=int(eachone[2])
pos=[pos_x,pos_y]
bullet_bmp_new=pygame.transform.rotate(bullet_bmp,360-eachone[0]*57.29)
screen.blit(bullet_bmp_new,pos)
pygame.display.flip()
for oneshot in bullet:
if oneshot[1]<badguy_pos[0]+57 and oneshot[1]>badguy_pos[0] and\
oneshot[2]<badguy_pos[1]+70 and oneshot[2]>badguy_pos[1]:
shot+=1
alive=False
break
if shot<5 and alive==False:
badguy_x=random.randrange(0,640-57)
badguy_y=random.randrange(0,480-70)
badguy_pos=[badguy_x,badguy_y]
alive=True
if alive ==True:
screen.blit(badguy, badguy_pos)
else:
pygame.quit()
exit(0)
if len(shit)<2:
shit.append([math.atan2(playerpos1[1]-(badguy_pos[1]+32),
playerpos1[0]-(badguy_pos[0]+32)),badguy_pos[0]+32,badguy_pos[1]+32])
for oneshit in shit:
index=0
velx=math.cos(oneshit[0])*speed_shit
vely=math.sin(oneshit[0])*speed_shit
oneshit[1]+=velx
oneshit[2]+=vely
if oneshit[1]<-64 or oneshit[1]>640 or oneshit[2]<-64 or oneshit[2]>480:
shit.pop(index)
index+=1
for eachshit in shit:
pos_x=int(eachshit[1])
pos_y=int(eachshit[2])
pos=[pos_x,pos_y]
shit_bmp_new=pygame.transform.rotate(shit_bmp,360-eachshit[0]*57.29)
screen.blit(shit_bmp_new,pos)
pygame.display.flip()
for oneshit in shit:
if oneshit[1]<playerpos1[0]+66 and oneshit[1]>playerpos1[0] and\
oneshit[2]<playerpos1[1]+66 and oneshit[2]>playerpos1[1]:
pygame.quit()
exit(0)
pygame.display.flip()
每次点击一次鼠标,子弹发射,保存到列表中,但是当子弹离开屏幕需要删除掉改子弹,同时记得由第二部分保存的偏转角计算子弹的偏转。剧毒喷出的大便处理类似,也要由传说哥的位置计算出偏转角利用设定的速度甩向我们的主人公火枪哥。
这里的输赢判定为火枪打死5个剧毒即获取胜利,扔出的便便有一次砸到我们的主人公,游戏失败即结束,不管胜利还是失败,游戏均退出处理。
以上便是整个游戏的大体逻辑,可以看出利用python 100行左右的代码即可以实现一个最简单的人机对战,不过代码写的比较乱得说,也没有用函数编程和面向对象的东西。另外提一下写游戏这块效率性能确实不敢恭维(明显的迟钝)。。不管怎么样,利用pygame实现小游戏还是比较方便的。
这个小游戏写的确实虽然简单和幼稚,不过花了半天时间就能从零搞起来,确实有所收获。有时间的话可以进一步增加敌方的难度和可玩性,先这样吧。
附下代码吧:
import pygame
import math
import random # 1 - Initialize the game
pygame.init()
# prepare the variables
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
playerpos=[100,100]
speed_x=5
speed_y=5
speed_bullet=10
speed_shit=3
shot=0
alive=True
bullet=[]
shit=[]
badguy_x=random.randrange(0,640)
badguy_y=random.randrange(0,480)
badguy_pos=[badguy_x,badguy_y]
green=[0,255,0]
# 2 - Load images
player = pygame.image.load("1.bmp")
badguy = pygame.image.load("2.bmp")
backgroud= pygame.image.load("3.bmp")
bullet_bmp=pygame.image.load("4.bmp")
shit_bmp=pygame.image.load("5.bmp")
player.set_colorkey([0,0,0])
badguy.set_colorkey([0,0,0])
shit_bmp.set_colorkey([255,255,255])
# 3 - main loop
while 1:
# 4 - clear the screen before drawing it again
screen.fill(0)
# 5 - draw the backgroud
for x in range(width/backgroud.get_width()+1):
for y in range(height/backgroud.get_height()+1):
screen.blit(backgroud,(x*90,y*65)) # 6 - 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 == pygame.K_w:
playerpos[1]-=speed_y
elif event.key == pygame.K_s:
playerpos[1]+=speed_y
elif event.key == pygame.K_a:
playerpos[0]-=speed_x
elif event.key == pygame.K_d:
playerpos[0]+=speed_x
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+32))
playerrot = pygame.transform.rotate(player, 360-angle*57.29)
playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
if event.type==pygame.MOUSEBUTTONDOWN:
position=pygame.mouse.get_pos()
bullet.append([math.atan2(position[1]-(playerpos1[1]+32),
position[0]-(playerpos1[0]+32)),playerpos1[0]+32,playerpos1[1]+32])
# 7 - process fight
screen.blit(playerrot, playerpos1)
for oneshot in bullet:
index=0
velx=math.cos(oneshot[0])*speed_bullet
vely=math.sin(oneshot[0])*speed_bullet
oneshot[1]+=velx
oneshot[2]+=vely
if oneshot[1]<-64 or oneshot[1]>640 or oneshot[2]<-64 or oneshot[2]>480:
bullet.pop(index)
index+=1
for eachone in bullet:
pos_x=int(eachone[1])
pos_y=int(eachone[2])
pos=[pos_x,pos_y]
bullet_bmp_new=pygame.transform.rotate(bullet_bmp,360-eachone[0]*57.29)
screen.blit(bullet_bmp_new,pos)
pygame.display.flip()
for oneshot in bullet:
if oneshot[1]<badguy_pos[0]+57 and oneshot[1]>badguy_pos[0] and\
oneshot[2]<badguy_pos[1]+70 and oneshot[2]>badguy_pos[1]:
shot+=1
alive=False
break
if shot<5 and alive==False:
badguy_x=random.randrange(0,640-57)
badguy_y=random.randrange(0,480-70)
badguy_pos=[badguy_x,badguy_y]
alive=True
if alive ==True:
screen.blit(badguy, badguy_pos)
else:
pygame.quit()
exit(0)
if len(shit)<2:
shit.append([math.atan2(playerpos1[1]-(badguy_pos[1]+32),
playerpos1[0]-(badguy_pos[0]+32)),badguy_pos[0]+32,badguy_pos[1]+32])
for oneshit in shit:
index=0
velx=math.cos(oneshit[0])*speed_shit
vely=math.sin(oneshit[0])*speed_shit
oneshit[1]+=velx
oneshit[2]+=vely
if oneshit[1]<-64 or oneshit[1]>640 or oneshit[2]<-64 or oneshit[2]>480:
shit.pop(index)
index+=1
for eachshit in shit:
pos_x=int(eachshit[1])
pos_y=int(eachshit[2])
pos=[pos_x,pos_y]
shit_bmp_new=pygame.transform.rotate(shit_bmp,360-eachshit[0]*57.29)
screen.blit(shit_bmp_new,pos)
pygame.display.flip()
for oneshit in shit:
if oneshit[1]<playerpos1[0]+66 and oneshit[1]>playerpos1[0] and\
oneshit[2]<playerpos1[1]+66 and oneshit[2]>playerpos1[1]:
pygame.quit()
exit(0)
pygame.display.flip()
写简单游戏,学编程语言-python篇:传说哥大战剧毒术士的更多相关文章
- 写简单游戏,学编程语言-python篇:大鱼吃小鱼
很常见的游戏之一,实现原理并不复杂,并且参考了几个相关的代码.这边主要还是以学习编程语言和学习编程思路为重点记录一下吧.最近时间有点吃紧,只能匆忙记录一下.用pygame做的大鱼吃小鱼的游戏截图如下: ...
- 写简单游戏,学编程语言-python篇
好吧, 首先得承认这个题目写的夸大了,人才菜鸟一枚,游戏相关编程也是知道点概念.但是本人对游戏开发比较感兴趣,相信大多数喜欢玩玩游戏,因为它给人确实带来很多乐趣,而编程语言的学习最少对于我来说比较乏味 ...
- 56岁潘石屹生日当天宣布要学编程语言Python,网友:地产商来抢码农饭碗了!
最近在码农界里,一个比较轰动的事情,就是地产大佬潘石屹,在56岁生日当天宣布要学习编程语言Python. 可能部分老铁不认识潘石屹,简单介绍下大佬背景: 潘石屹,1963年11月14日出生于甘肃天水, ...
- 功能强大而又简单易学的编程语言Python
Python是一种面向对象.直译式计算机程序设计语言,也是一种功能强大的通用型语言.首先,Python非常简单,以Hello World为例: Java的Hello World程序一般这么写: pub ...
- 闲扯游戏编程之html5篇--山寨版《flappy bird》源码
新年新气象,最近事情不多,继续闲暇学习记点随笔,欢迎拍砖.之前的〈简单游戏学编程语言python篇〉写的比较幼稚和粗糙,且告一段落.开启新的一篇关于javascript+html5的从零开始的学习.仍 ...
- 通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数--菜单功能'menufile
通过游戏学python 3.6 第一季 第九章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁 ...
- 通过游戏学python 3.6 第一季 第八章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账号--锁定次数
通过游戏学python 3.6 第一季 第八章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆--账号的注册查询和密码的找回修改--锁定账 ...
- 通过游戏学python 3.6 第一季 第二章 实例项目 猜数字游戏--核心代码--猜测次数 可复制直接使用 娱乐 可封装 函数
猜数字游戏--核心代码--猜测次数 #猜数字--核心代码--猜测次数 number=33 amount=3 count=0 while count<=amount: conversion ...
- 人工智能时代,是时候学点Python了!
“是时候学点Python了”.作为一名不怎么安分的程序员,你或许觉得,产生这样的想法并不奇怪,但学习Python却是出于自己对工作现状以及如何应对未来挑战所作出的思考.读过我以前博客的朋友,可能都知道 ...
随机推荐
- google书签找回
解决办法: 1.查找google文件夹,win7为例子:C:\Users\ZhangSan\AppData\Local\Google\Chrome\User Data 找到这个文件夹,ZhangSan ...
- NoSQL生态系统——类似Bigtable列存储,或者Dynamo的key存储(kv存储如BDB,结构化存储如redis,文档存储如mongoDB)
摘自:http://www.ituring.com.cn/article/4002# NoSQL系统的数据操作接口应该是非SQL类型的.但在NoSQL社区,NoSQL被赋予了更具有包容性的含义,其意为 ...
- 矩阵k次幂 采用三重循环
#include<iostream> using namespace std; int main() { int n,k; ][],b[][],c[][]; while(cin>&g ...
- http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html(转载)(原作者:AstralWind)
Python正则表达式指南 本文介绍了Python对于正则表达式的支持,包括正则表达式基础以及Python正则表达式标准库的完整介绍及使用示例.本文的内容不包括如何编写高效的正则表达式.如何优 ...
- PLSQL大数据生成规则
数据定义 数据定义决定了被生成的数据.如果要创建简单的字符,可以在两个方括号之间输入字符定义:[数据] 数据可以是下列预先确定的集的混合体: • a: a..z (小写字符) ...
- blade and soul zone overview
The world of Blade and Soul, is a vast extension of land containing two continents (the Southern Con ...
- C#微信公众号接口开发,灵活利用网页授权、带参数二维码、模板消息,提升用户体验之完成用户绑定个人微信及验证码获取
一.前言 当下微信公众号几乎已经是每个公司必备的,但是大部分微信公众账号用户体验都欠佳,特别是涉及到用户绑定等,需要用户进行复杂的操作才可以和网站绑定,或者很多公司直接不绑定,而是每次都让用户填写账号 ...
- ajax的探究与使用
前端必须掌握ajax,这是几乎所有前端招聘都会要求的一项. 但其实ajax也就是一种异步请求的技术,没有什么很深的东西,不过接触ajax很长一段时间了,早该整理下ajax的学习和使用: PART1: ...
- AppFog免费云空间申请及安装wordpress(图文教程)
AppFog是一家提供运算平台的服务,用户可以在上面搭建自己的Web App.原本它的名字为PHPFog,但在采用了Cloud Foundry的代码作为核心,支持多个编程语言后,选择了更名.AppFo ...
- 4412开发板升级4.2之后改了logo开机后屏幕闪解决办法
荣品4412开发板升级到4.2请注意增加虚拟机内存. 问:荣品4412开发板升级到Android4.2之后,改了logo.4412板子开机后,过一会屏幕就一闪一闪,是什么原因? Android4.2编 ...