您的位置:首页 > 其它

How to think like a Computer Scientist: 课后习题第十七章2

2013-09-15 11:58 477 查看
#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      penglaixy
#
# Created:     08/09/2013
# Copyright:   (c) penglaixy 2013
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import pygame

gravity = 0.0001

class QueenSprite:

def __init__(self, img, target_posn):
self.image = img
self.target_posn = target_posn
(x,y) = target_posn
self.posn = (x, 0)
self.y_velocity = 0

def update(self):
self.y_velocity += gravity
(x,y) = self.posn
new_y_pos = y + self.y_velocity

(target_x, target_y) = self.target_posn
dist_to_go = target_y - new_y_pos

if dist_to_go < 0:
self.y_velocity = -0.65 * self.y_velocity
#new_y_pos = target_y + dist_to_go
new_y_pos = target_y

self.posn = (x, new_y_pos)

def draw(self, target_surface):
target_surface.blit(self.image, self.posn)
def contains_point(self, pt):
'''
Return True if my sprite rectangle contains point pt
'''
(my_x, my_y) = self.posn
my_width = self.image.get_width()
my_height = self.image.get_height()
(x,y) = pt
return ( x >= my_x - my_width/2 and x <= my_x + my_width/2 and
y >= my_y -my_height/2 and y <= my_y + my_height/2 )
def handle_click(self):
self.y_velocity += -0.3

class DukeSprite:

def __init__(self, img, target_posn):
self.image = img
self.posn = target_posn
self.current_patch_num = 0
self.anim_frame_count = 0

def update(self):
if self.anim_frame_count > 0:
self.anim_frame_count = (self.anim_frame_count + 1) % 60
self.current_patch_num = self.anim_frame_count // 6
return

def draw(self, target_surface):
patch_rect = (self.current_patch_num * 50, 0, 50, self.image.get_height())
target_surface.blit(self.image, self.posn, patch_rect)
return

def contains_point(self, pt):
(my_x, my_y) = self.posn
my_width = 50
my_height = self.image.get_height()
(x,y) = pt
return ( x >= my_x and x <= my_x + my_width and
y >= my_y and y <= my_y + my_height )
return

def handle_click(self):
if self.anim_frame_count == 0:
self.anim_frame_count = 1
return

def draw_board(the_board):
'''
Draw a chess board with queens, from the borad.
'''
pygame.init()
colors = [(0,0,255), (0,0,0)]
ball = pygame.image.load("love.png")

n = len(the_board)
surface_sz = 480
sq_sz = surface_sz//n
surface_sz = n*sq_sz

surface = pygame.display.set_mode((surface_sz, surface_sz))

ball_offset = (sq_sz - ball.get_width())//2

all_sprites = []

for (col, row) in enumerate(the_board):
a_queen = QueenSprite(ball, (col*sq_sz + ball_offset, row*sq_sz + ball_offset))
all_sprites.append(a_queen)

duke_sprite_sheet = pygame.image.load("duke_spritesheet.png")

duke1 = DukeSprite(duke_sprite_sheet, (sq_sz*2, 0))
duke2 = DukeSprite(duke_sprite_sheet, (sq_sz*5, sq_sz))

all_sprites.append(duke1)
all_sprites.append(duke2)
my_clock = pygame.time.Clock()

while True:

ev = pygame.event.poll()

if ev.type == pygame.KEYDOWN:
key = ev.dict['key']
if key == 27:
break
if key == ord('r'):
colors[0] = (255,0,0)
elif key == ord('g'):
colors[0] = (0,255,0)
else:
colors[0] = (0,0,255)

if ev.type == pygame.MOUSEBUTTONDOWN:
posn_of_click = ev.dict['pos']
for sprite in all_sprites:
if sprite.contains_point(posn_of_click):
sprite.handle_click()
break

if ev.type == pygame.QUIT:
break

for sprite in all_sprites:
sprite.update()

for row in range(n):
c_indx = row%2
for col in range(n):
the_squuare = (col*sq_sz, row*sq_sz, sq_sz, sq_sz)
surface.fill(colors[c_indx], the_squuare)
c_indx = (c_indx + 1)%2

for sprite in all_sprites:
sprite.draw(surface)

pygame.display.flip()
my_clock.tick(60)

pygame.quit()

def main():
'''
set up the game and run the main game loop
'''
draw_board([0,5,3,1,6,4,2])
#draw_board([6,4,2,0,5,7,1,3])
#draw_board([9,6,0,3,10,7,2,4,12,8,11,5,1])
#draw_board([11,4,8,12,2,7,3,15,0,14,10,6,13,1,5,9])

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