消除方块+不同颜色score加减分+player是否出界+鼠标控制+窗口显示得分
来源:互联网 发布:讲故事软件哪个好 编辑:程序博客网 时间:2024/05/22 10:22
要求:
http://programarcadegames.com/index.php?chapter=lab_sprite_collecting
# Sample Python/Pygame Programs# Simpson College Computer Science# http://programarcadegames.com/# http://simpson.edu/computer-science/ # Explanation video: http://youtu.be/4W2AqUetBi4 import pygameimport random # Define some colorsblack = ( 0, 0, 0)white = ( 255, 255, 255)red = ( 255, 0, 0)green = ( 0, 255, 0)blue = (0,0,255) # This class represents the ball # It derives from the "Sprite" class in Pygameclass Block(pygame.sprite.Sprite): # Constructor. Pass in the color of the block, # and its x and y position def __init__(self, color, width, height): # Call the parent class (Sprite) constructor pygame.sprite.Sprite.__init__(self) # Create an image of the block, and fill it with a color. # This could also be an image loaded from the disk. self.image = pygame.Surface([width, height]) self.image.fill(color) # Fetch the rectangle object that has the dimensions of the image # image. # Update the position of this object by setting the values # of rect.x and rect.y self.rect = self.image.get_rect()class Player(pygame.sprite.Sprite): change_x = 0 change_y = 0 def __init__(self,x,y): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface([15,15]) self.image.fill(blue) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y def changespeed(self,x,y): self.change_x += x self.change_y += y def update(self): self.rect.x += self.change_x self.rect.y += self.change_y if self.rect.x>700: self.rect.x = 0 if self.rect.x<0: self.rect.x = 700 if self.rect.y>500: self.rect.y = 0 if self.rect.y<0: self.rect.y = 500 # Initialize Pygamepygame.init() # Set the height and width of the screenscreen_width=700screen_height=400screen=pygame.display.set_mode([screen_width,screen_height]) # This is a list of 'sprites.' Each block in the program is# added to this list. The list is managed by a class called 'Group.'good_list = pygame.sprite.Group()bad_list = pygame.sprite.Group() # This is a list of every sprite. All blocks and the player block as well.all_sprites_list = pygame.sprite.Group() for i in range(50): # This represents a block block = Block(green, 20, 15) # Set a random location for the block block.rect.x = random.randrange(screen_width) block.rect.y = random.randrange(screen_height) # Add the block to the list of objects good_list.add(block) all_sprites_list.add(block)for i in range(50): # This represents a block block = Block(red, 20, 15) # Set a random location for the block block.rect.x = random.randrange(screen_width) block.rect.y = random.randrange(screen_height) # Add the block to the list of objects bad_list.add(block) all_sprites_list.add(block) # Create a red player blockplayer = Player(20, 15)all_sprites_list.add(player) #Loop until the user clicks the close button.done = False # Used to manage how fast the screen updatesclock = pygame.time.Clock() score = 0 # -------- Main Program Loop -----------while done==False: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop # Set the speed based on the key pressed elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.changespeed(-3,0) elif event.key == pygame.K_RIGHT: player.changespeed(3,0) elif event.key == pygame.K_UP: player.changespeed(0,-3) elif event.key == pygame.K_DOWN: player.changespeed(0,3) # Reset speed when key goes up elif event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: player.changespeed(3,0) elif event.key == pygame.K_RIGHT: player.changespeed(-3,0) elif event.key == pygame.K_UP: player.changespeed(0,3) elif event.key == pygame.K_DOWN: player.changespeed(0,-3) player.update() # Clear the screen screen.fill(white) # See if the player block has collided with anything. blocks_good_list = pygame.sprite.spritecollide(player, good_list, True) blocks_bad_list = pygame.sprite.spritecollide(player, bad_list, True) # Check the list of collisions. for block in blocks_good_list: score += 1 for block in blocks_bad_list: score += -1 font = pygame.font.Font(None,25) text = font.render("The score is "+str(score),True,black) screen.blit(text,[5,5]) # Draw all the spites all_sprites_list.draw(screen) # Limit to 20 frames per second clock.tick(20) # Go ahead and update the screen with what we've drawn. pygame.display.flip() pygame.quit()
- 消除方块+不同颜色score加减分+player是否出界+鼠标控制+窗口显示得分
- 鼠标控制方块
- 得分(score)
- uva 得分Score 1585
- 得分SCORE UVA1585
- elasticsearch score 得分 自定义
- 得分(Score,UVa1585)
- UVA1585 得分score
- DataGrid鼠标指向时显示不同颜色
- js 函数 隔行鼠标划过显示不同颜色
- DataGrid鼠标指向时显示不同颜色
- DataGrid鼠标指向时显示不同颜色
- 通过style来控制隔行显示不同颜色
- 习题3-1 得分(Score)
- UVa 1585 Score(得分)
- 消除pdf乱码方块
- poj1390 方块消除 dp
- POJ 1390 方块消除
- bnu 33971 Funny Car Racing (spfa)
- 面向对象的设计(二)——C++高级编程
- android monitor tool (8.0 模拟发送按键及触摸屏事件实现)
- 今天博客抽风了,我也抽风了
- <jsp:userbean>的用法
- 消除方块+不同颜色score加减分+player是否出界+鼠标控制+窗口显示得分
- android中两种上下文的区别
- Oracle删除表的几种方法及其比较
- 2013.11.07
- 内核的定时机制实验
- 隐马尔科夫模型HMM
- 黑马程序员.Net学习笔记 06篇.类、属性、构造函数
- HTML学习笔记
- 无线Trace的格式