|
| 1 | +import os.path |
| 2 | + |
| 3 | +import pygame as pg |
| 4 | + |
| 5 | + |
| 6 | +def load_img(name): |
| 7 | + try: |
| 8 | + data_dir = os.path.join(os.path.dirname(__file__), "images") |
| 9 | + fullname = os.path.join(data_dir, name) |
| 10 | + image = pg.image.load(fullname) |
| 11 | + except pg.error: |
| 12 | + print("Cannot load image:", name) |
| 13 | + exit() |
| 14 | + image = image.convert() |
| 15 | + return image, image.get_rect() |
| 16 | + |
| 17 | + |
| 18 | +class Fist(pg.sprite.Sprite): |
| 19 | + """ |
| 20 | + |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__(self): |
| 24 | + super(Fist, self).__init__() |
| 25 | + self.image, self.rect = load_img("d1.png") |
| 26 | + print(self.rect) |
| 27 | + print(pg.display.get_surface().get_height()/2) |
| 28 | + |
| 29 | + |
| 30 | +def main(): |
| 31 | + pg.init() |
| 32 | + screen = pg.display.set_mode([500, 120]) |
| 33 | + pg.display.set_caption("hell oworld") |
| 34 | + |
| 35 | + background = pg.Surface(screen.get_size()) |
| 36 | + background = background.convert() |
| 37 | + background.fill((250, 250, 250)) |
| 38 | + |
| 39 | + if pg.font: |
| 40 | + # 设置文本 |
| 41 | + font = pg.font.Font(None, 30) |
| 42 | + text = font.render("hello world", True, (0, 0, 0)) |
| 43 | + background.blit(text, ((background.get_width() - text.get_width()) / 2, 0)) |
| 44 | + |
| 45 | + screen.blit(background, (0, 0)) |
| 46 | + pg.display.flip() |
| 47 | + |
| 48 | + color = pg.time.Clock() |
| 49 | + fist = Fist() |
| 50 | + groups = pg.sprite.Group((fist,)) |
| 51 | + |
| 52 | + going = True |
| 53 | + while going: |
| 54 | + color.tick(60) |
| 55 | + for event in pg.event.get(): |
| 56 | + if event.type == pg.QUIT: |
| 57 | + pg.quit() |
| 58 | + exit() |
| 59 | + pass |
| 60 | + groups.update() |
| 61 | + |
| 62 | + screen.blit(background, (0, 0)) |
| 63 | + groups.draw(screen) |
| 64 | + pg.display.update() |
| 65 | + |
| 66 | + |
| 67 | +if __name__ == '__main__': |
| 68 | + main() |
0 commit comments