0 Star 1 Fork 0

Maoxx/Bunny

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
itgame.py 3.76 KB
一键复制 编辑 原始数据 按行查看 历史
Maoxx 提交于 2014-01-03 12:06 . first commit
import pygame
from pygame.locals import *
import math
pygame.init()
pygame.key.set_repeat(75, 0)
width,height = 640,480
screen= pygame.display.set_mode((width, height))
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
player = pygame.image.load("resources/images/dude.png")
arrow = pygame.image.load("resources/images/bullet.png")
badguyimg1 = pygame.image.load("resources/images/badguy.png")
badguyimg=badguyimg1
keys = [False,False,False,False]
playerpos = [100,100]
playerpos1 = [100,100]
acc = [0,0]
arrows = []
badtimer=100
badtimer1=0
badguys=[[640,100]]
healthvalue=194
def display():
#draw background
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 ))
#draw player
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(playerpos[1]),position[0]-(playerpos[0]))
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)
#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] < 0 or bullet[1] > width or bullet[2] < 0 or bullet[2] > height:
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]))
#draw badguy
global badtimer
if badtimer == 0:
badguys.append([width,random.randint(50,height-50)])
badtimer = 100 - (badtimer1*2)
if badtimer1 >= 35:
badtimer1 = 35
else:
badtimer1 += 5
index = 0
for badguy in badguys:
if badguy[0]<-64:
badguys.pop(index)
badguy[0] -=7
index += 1
for badguy in badguys:
screen.blit(badguyimg,badguy)
pygame.display.flip()
def key_down_event():
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
def key_up_event():
if event.key == K_w:
keys[0] = False
elif event.key ==K_a:
keys[1] = False
elif event.key == K_s:
keys[2] = False
elif event.key == K_d:
keys[3] = False
def player_move():
if keys[0]:
playerpos[1]-=5
elif keys[1]:
playerpos[0]-=5
elif keys[2]:
playerpos[1]+=5
elif keys[3]:
playerpos[0]+=5
if playerpos[0] < 0:
playerpos[0] = 0
elif playerpos[0] > width - player.get_width():
playerpos[0] = width - player.get_width()
if playerpos[1] < 0:
playerpos[1] = 0
elif playerpos[1] > height - player.get_height():
playerpos[1] = height - player.get_height()
while 1:
display()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
elif event.type == pygame.KEYDOWN :
key_down_event()
elif event.type == pygame.KEYUP:
key_up_event()
elif event.type == pygame.MOUSEBUTTONDOWN:
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])
player_move()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/RelaxCat/Bunny.git
git@gitee.com:RelaxCat/Bunny.git
RelaxCat
Bunny
Bunny
master

搜索帮助