fork download
  1. import os
  2. import pygame
  3. from pygame.locals import *
  4. from PIL import Image, ImageTk
  5. import numpy as np
  6. import tkinter
  7.  
  8. WIDTH = 640
  9. HEIGHT = 480
  10. SCRECT = Rect(0, 0, WIDTH, HEIGHT)
  11. FPS = 300
  12. IFPS = 1000 // FPS
  13. IMG = './wx_64x64.png'
  14. XBM = '''
  15. #define image_width 16
  16. #define image_height 16
  17. static char image_bits[] = {
  18. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  19. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
  20. 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  21. 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff};
  22. '''
  23.  
  24. class Player(pygame.sprite.Sprite):
  25. def __init__(self, img, pos, vxy, angle):
  26. super().__init__()
  27. self.im = pygame.image.load(img).convert_alpha()
  28. if angle != 0: self.im = pygame.transform.rotate(self.im, angle)
  29. self.rct = self.im.get_rect(topleft=pos)
  30. self.vx, self.vy = vxy
  31. self.angle = angle
  32.  
  33. def update(self):
  34. self.rct.move_ip(self.vx, self.vy)
  35. if self.rct.left < 0 or self.rct.right > SCRECT.width: self.vx = -self.vx
  36. if self.rct.top < 0 or self.rct.bottom > SCRECT.height: self.vy = -self.vy
  37. self.rct = self.rct.clamp(SCRECT)
  38.  
  39. def draw(self, screen):
  40. screen.blit(self.im, self.rct)
  41.  
  42. class Game(object):
  43. def __init__(self):
  44. os.environ['SDL_VIDEO_CENTERED'] = ''
  45. os.environ['SDL_VIDEO_WINDOW_POS'] = f'{WIDTH+10},{30}'
  46. pygame.init()
  47. self.screen = pygame.display.set_mode(SCRECT.size, flags=pygame.DOUBLEBUF)
  48. pygame.display.set_caption('title')
  49. self.players = [
  50. Player(IMG, (0, 0), (2, 1), 0),
  51. Player(IMG, (0, 0), (1, 2), 90),
  52. Player(IMG, (0, 0), (2, 1), -45),
  53. Player(IMG, (0, 0), (1, 2), 45)]
  54. self.running = True
  55.  
  56. def loop_tick(self, w, h):
  57. self.screen.fill((0, 0, 0))
  58. for p in self.players:
  59. p.update()
  60. p.draw(self.screen)
  61. s = pygame.display.get_surface()
  62. b = s.get_buffer()
  63. a = np.ndarray((HEIGHT, WIDTH, 4), np.uint8, b.raw) # ARGB
  64. im = Image.fromarray(a[:,:,2::-1]) # as BGR (skip alpha)
  65. pygame.display.flip()
  66.  
  67. for ev in pygame.event.get():
  68. if ev.type == QUIT:
  69. self.running = False
  70. break
  71. if ev.type == KEYDOWN:
  72. if ev.key == K_ESCAPE:
  73. self.running = False
  74. break
  75.  
  76. return ImageTk.PhotoImage(im.resize((h, h)))
  77.  
  78. def __del__(self):
  79. pygame.quit()
  80.  
  81. class Rt(object):
  82. def __init__(self, g):
  83. self.g = g
  84. self.rt = tkinter.Tk()
  85. self.rt.title('title')
  86. self.rt.geometry(f'{WIDTH}x{HEIGHT}+0+0')
  87. self.canvas = tkinter.Canvas(self.rt, width=WIDTH, height=HEIGHT)
  88. self.canvas.place(x=0, y=0)
  89. w, h = [v // 2 for v in SCRECT.size]
  90. self.canvas.create_rectangle(0, 0, w, h, fill='green')
  91. self.canvas.xbm = tkinter.BitmapImage(data=XBM,
  92. foreground='red', background='lime')
  93. self.canvas.create_image(w, h, image=self.canvas.xbm, anchor='sw')
  94. self.canvas.ph = tkinter.PhotoImage(file=IMG)
  95. self.canvas.create_image(w, h, image=self.canvas.ph, anchor='ne')
  96. self.canvas.im = ImageTk.PhotoImage(Image.open(IMG).resize((h, h)))
  97. self.canvas.create_image(w, h, image=self.canvas.im, anchor='nw')
  98.  
  99. def after_callback(self):
  100. w, h = [v // 2 for v in SCRECT.size]
  101. self.canvas.im = self.g.loop_tick(w, h)
  102. self.canvas.create_image(w, h, image=self.canvas.im, anchor='nw')
  103. if self.g.running: self.rt.after(IFPS, self.after_callback)
  104. else: self.rt.quit()
  105.  
  106. def mainloop(self):
  107. self.rt.after(IFPS, self.after_callback)
  108. self.rt.mainloop()
  109. self.g.running = False
  110. del self.g
  111.  
  112. if __name__ == '__main__':
  113. rt = Rt(Game())
  114. rt.mainloop()
Runtime error #stdin #stdout #stderr 1.21s 37300KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 2, in <module>
ModuleNotFoundError: No module named 'pygame'