- 帖子
- 15
- 精华
- 0
- 积分
- 47
- 阅读权限
- 10
- 注册时间
- 2016-4-15
- 最后登录
- 2016-4-29
|
import sys,time,random,math,pygame
class MySprite(pygame.sprite.Sprite):
def __init__(self,target):
pygame.sprite.Sprite.__init__(self)
self.master_image = None
self.frame = 0
self.old_frame = -1
self.frame_width = 1
self.frame_height = 1
self.first_frame = 0
self.last_frame = 0
self.cloumns = 1
self.last_time = 0
def _getx(self): return self.rect.x
def _setx(self,value): self.rect.x = value
x = property(_getx,_setx)
def _gety(self): return self.rect.y
def _sety(self,value): self.rect.y = value
y = property(_gety,_sety)
def _getpos(self):
return self.rect.topleft
def _setpos(self,pos):
self.rect.topleft = pos
position = property(_getpos,_setpos)
def load(self,filename,width,height,columns):
self.master_image = pygame.image.load(filename).convert_alpha()
self.frame_width =width
self.frame_height = height
self.rect = (0,0,width,height)
self.columns = columns
rect = self.master_image.get_rect()
self.last_frame = (rect.width//width) *(rect.height//height)-1
def update(self,current_time,rate= 30):
if current_time > self.last_time + rate:
self.frame +=1
if self.frame > self.last_time:
self.frame = self.first_frame
self.frame = current_time
if self.frame != self.old_frame:
frame_x = (self.frame % self.columns)* self.frame_width
frame_y = (self.frame // self.columns)* self.frame_height
rect =(frame_x,frame_y,self.frame_width,self.frame_height)
self.image = self.master_image.subsurface(rect)
self.old_frame = self.frame
def __str__(self):
return str(self.frame) +"," +str(self.first_frame)+\
","+str(self.last_frame) + "," + str(self.frame_width)+ \
","+ str(self.frame_height)+ "," + str(self.columns)+\
"," +str(self.rect)
def print_text(font,x,y,text,color=(255,255,255)):
imgtext = font.render(text,1,color)
screen.blit(imgtext,(x,y))
def reset_arrow():
y = random.randint(250,350)
arrow.position = 800,y
pygame.init()
screen =pygame.display.set_mode((800,600))
pygame.display.set_caption("Escape The Dragon Game")
font = pygame.font.Font(None,18)
tt = pygame.time.Clock()
bg = pygame.image.load("dd.png").convert_alpha()
group = pygame.sprite.Group()
dragon = MySprite(screen)
dragon.load("d.png",260,150,4)
group.add(dragon)
player = MySprite(screen)
player.load("ddd.png",50,64,8)
player.first_frame = 1
player.last_frame = 7
player.position = 400 ,303
group.add(player)
arrow =MySprite(screen)
arrow.load("fff.png",40,16,1)
arrow.position = 800,320
group.add(arrow)
arrow_vel = 8.0
game_over = False
you_win = False
player_jumping = False
jump_wel = 0.0
player_start_y = player.y
running = True
while running:
tt.tick(30)
ticks = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
running = False
elif keys[pygame.K_SPACE]:
if not player_jumping:
player_jumping = True
jump_vel = -8.0
if not game_over:
arrow.x -= arrow_vel
if arrow.x < -40:
reset_arrow()
if pygame.sprite.collide_rect(arrow,player):
reset_arrow()
player.x -= 10
if pygame.sprite.collide_rect(arrow,dragon):
reset_arrow()
dragon.x -= 10
if pygame.sprite.collide_rect(player,dragon):
game_over = True
if dragon.x < -100:
you_win = True
game_over = True
if player_jumping:
player.y += jump_vel
jump_vel += 0.5
if player.y > player_start_y:
player_jumping = False
player.y = player_start_y
jump_vel = 0.0
screen.blit(bg,(0,0))
if not game_over:
group.update(ticks,50)
group.draw(screen)
print_text(font,350,560,"Press,SPACE to jump")
if game_over:
print_text(font,360,100,"G A M E O V E R")
if you_win:
print_text(font,330,130,"YOU BEAT THE DRAGON!")
else:
print_text(font,330,130,"THE DRAGON GOT YOU!")
pygame.display.update()
pygame.quit()
运行中错误:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\dfdfdsaa.py", line 74, in <module>
player.position = 400 ,303
File "C:\Users\Administrator\Desktop\dfdfdsaa.py", line 25, in _setpos
self.rect.topleft = pos
AttributeError: 'tuple' object has no attribute 'topleft'
这个例子我是从书上复制下来的 求帮助?谢谢。
|
|