您的位置:首页 > 编程语言 > Python开发

写简单游戏,学编程语言-python篇:传说哥大战剧毒术士

2014-01-03 19:05 453 查看
  上篇写的动画示例其实算不上一个游戏,顶多算是利用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()


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: