📄 listing29-1.py
字号:
import sys, pygamefrom pygame.locals import *from random import randrangeclass Weight(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) # image and rect used when drawing sprite: self.image = weight_image self.rect = self.image.get_rect() self.reset() def reset(self): """ Move the weight to a random position at the top of the screen. """ self.rect.top = -self.rect.height self.rect.centerx = randrange(screen_size[0]) def update(self): """ Update the weight for display in the next frame. """ self.rect.top += 1 if self.rect.top > screen_size[1]: self.reset()# Initialize thingspygame.init()screen_size = 800, 600pygame.display.set_mode(screen_size, FULLSCREEN)pygame.mouse.set_visible(0)# Load the weight imageweight_image = pygame.image.load('weight.png')weight_image = weight_image.convert() # ...to match the display# Create a sprite group and add a Weightsprites = pygame.sprite.RenderUpdates()sprites.add(Weight())# Get the screen surface and fill itscreen = pygame.display.get_surface()bg = (255, 255, 255) # Whitescreen.fill(bg)pygame.display.flip()# Used to erase the sprites:def clear_callback(surf, rect): surf.fill(bg, rect)while True: # Check for quit events: for event in pygame.event.get(): if event.type == QUIT: sys.exit() if event.type == KEYDOWN and event.key == K_ESCAPE: sys.exit() # Erase previous positions: sprites.clear(screen, clear_callback) # Update all sprites: sprites.update() # Draw all sprites: updates = sprites.draw(screen) # Update the necessary parts of the display: pygame.display.update(updates)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -