当前位置:   article > 正文

python之pygame_for event in pygame.event.get

for event in pygame.event.get

目录

pygame.dispaly, pygame.event, pygame.draw:


pygame.dispaly:

pygame有且仅有一个屏幕;左上角坐标(0,0);以像素为单位。

#屏幕尺寸和模

  1. pygame.display.set_mode(r = (0,0), flag = 0)
  2. #r是游戏屏幕分辨率,以元组形式输入(weight, height)
  3. #flag用来控制显示类型,可用 | 组合使用,常用标签有:
  4. pygame.RESIZABLE #窗口大小可调
  5. pygame.NOFRAME #窗口没有边界显示
  6. pygame.FULLSCREEN #窗口全屏显示
  7. (注意每种显示要配合相应的处理机制)
  8. vinfo = pygame.display.Info()
  9. #产生一个显示信息对象VideoInfo,表达当前屏幕参数信息
  10. vinfo.current_w #当前显示模式或窗口的像素宽度
  11. vinfo.current_h #当前显示模式或窗口的像素高度
  12. pygame.VIDEORESIZE
  13. #这是一种窗口大小更改的事件
  14. #事件发生后,返回event.size元组,包含新窗口的宽度和高度
  15. .size[0] #宽度,也可以用event.w
  16. .size[1] #高度,也可以用event.h
  17. #返回参数仅在事件发生时有用
  18. #example
  19. if event.type == pygame.VIDEORESIZE:
  20. size = width, height = event.size[0], event.size[1]
  21. screen = pygame.display.set_mode(size, pygame.RESIZABLE)

#窗口标题和图标

  1. pygame.display.set_caption(title, icontitle = None)
  2. #title设置窗口的标题内容
  3. #icontitle设置图表化后的小标题,小标题可选,部分系统没有
  4. pygame.display.set_icon(surface)
  5. #设置窗口的图标效果
  6. #图标是一个Surface对象
  7. pygame.display.get_caption()
  8. #返回当前设置窗口的标题和小标题内容,(title, icontitle)

#窗口感知和刷新

  1. pygame.display.get_active()
  2. #当窗口在系统中显示(屏幕绘制/非图标化)时返回True,否则返回False
  3. #可以用来判断游戏窗口是否被最小化
  4. pygame.display.flip()
  5. #重新绘制整个窗口
  6. pygame.display.update()
  7. #仅重新绘制窗口中有变化的区域,相比.flip()执行更快

 

pygame.event:

#键盘事件

  1. pygame.event.KEYDOWN #键盘按下事件
  2. pygame.event.KEYUP #键盘释放事件
  3. #属性
  4. event.key #按键的常量名称
  5. event.mod #按键修饰符的组合值
  6. event.mod = KMOD_ALT|KMOD_SHIFT #修饰符的按位或运算

#鼠标事件

  1. #鼠标移动事件
  2. pygame.event.MOUSEMOTION
  3. #属性
  4. event.pos #鼠标当前坐标值(x,y),相对于窗口左上角
  5. event.rel #鼠标相对运动距离(x,y),相对于上次事件
  6. event.rel #鼠标按钮状态(a,b,c),对应于鼠标的三个键(左 中 右)
  7. pygame.event.MOUSEBUTTONUP #鼠标释放事件
  8. pygame.event.MOUSEBUTTONDOWN #鼠标按下事件
  9. #属性
  10. event.pos #鼠标当前坐标值(x,y)
  11. event.button #鼠标按下键编号,左 中 右对应1 2 3

#example

  1. #鼠标,键盘事件处理
  2. import pygame, sys
  3. pygame.init()
  4. screen = pygame.display.set_mode((600, 400))
  5. pygame.display.set_caption("Pygame事件处理")
  6. while True:
  7. for event in pygame.event.get():
  8. if event.type == pygame.QUIT:
  9. sys.exit()
  10. elif event.type == pygame.KEYDOWN:
  11. if event.unicode == "":
  12. print("[KEYDOWN]:", "#", event.key, event.mod)
  13. else:
  14. print("[KEYDOWN]:", event.unicode, event.key, event.mod)
  15. elif event.type == pygame.MOUSEMOTION:
  16. print("[MOUSEMOTION]:", event.pos, event.rel, event.buttons)
  17. elif event.type == pygame.MOUSEBUTTONUP:
  18. print("[MOUSEBUTTONUP]:", event.pos, event.button)
  19. elif event.type == pygame.MOUSEBUTTONDOWN:
  20. print("[MOUSEBUTTONDOWN]:", event.pos, event.button)
  21. pygame.display.update()

pygame.event.get()

从事件列表中获得事件列表

  1. for event in pygame.event.get():
  2. if event.type == pygame.QUIT:
  3. sys.exit()

可以增加参数,获得某类或某些类事件:

  1. pygame.event.get(type)
  2. pygame.event.get(typelist)

pygame.event.poll()

从事件队列中获得一个事件,事件将从事件队列中删除,如果事件队列为空,则返回event.NOEVENT

  1. while True:
  2. event = pygame.event.poll()

pygame.event.clear()

从事件队列中删除事件,默认删除所有事件,可以增加参数,删除某类或某些类事件

  1. pygame.event.clear(type)
  2. pygame.event.clear(typelist)

pygame.draw:

官方文档

pygame库用来绘制形状的类

参数列表中的Surface是当前绘制屏幕的名称,color是RGB色彩模式下的颜色,例如 黑色(0, 0, 0),白色(255, 255, 255),width = 0是绘制形状的边的宽度,如果不传入的话width=0默认为填充

pygame.draw.rect(Surface, color, Rect, width=0)

绘制一个矩形

参数Rect是矩形参数,格式为[x, y, width, height]

examples for pygame.draw.rect

pygame.draw.circle(Surface, color, pos, radius, width=0)

以某点为圆心绘制一个圆形

参数pos是圆心的位置,radius是半径大小

examples for pygame.draw.circle

pygame.draw.ellipse(Surface, color, Rect, width=0)

绘制一个椭圆

通过给出矩形的参数,从而绘制一个内切于矩形的椭圆

examples fo pygame.draw.ellipse

pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)

绘制椭圆的一部分

两个角度分别是起始和结束角度,最右边为0度

examples for pygame.draw.arc

pygame.draw.line(Surface, color, start_pos, end_pos, width=1)

绘制一条直线

参数两个pos分别是起始位置和结束位置

examples for pygame.draw.line

pygame.draw.lines(Surface, color, closed, pointlist, width=1)

绘制多条连续直线

参数closed为True时首尾相连构成封闭,pointlist为顶点坐标

examples fo pygame.draw.lines

A big example for this module

  1. # Import a library of functions called 'pygame'
  2. import pygame
  3. from math import pi
  4. # Initialize the game engine
  5. pygame.init()
  6. # Define the colors we will use in RGB format
  7. BLACK = (0, 0, 0)
  8. WHITE = (255, 255, 255)
  9. BLUE = (0, 0, 255)
  10. GREEN = (0, 255, 0)
  11. RED = (255, 0, 0)
  12. # Set the height and width of the screen
  13. size = [400, 300]
  14. screen = pygame.display.set_mode(size)
  15. pygame.display.set_caption("Example code for the draw module")
  16. # Loop until the user clicks the close button.
  17. done = False
  18. clock = pygame.time.Clock()
  19. while not done:
  20. # This limits the while loop to a max of 10 times per second.
  21. # Leave this out and we will use all CPU we can.
  22. clock.tick(10)
  23. for event in pygame.event.get(): # User did something
  24. if event.type == pygame.QUIT: # If user clicked close
  25. done = True # Flag that we are done so we exit this loop
  26. # All drawing code happens after the for loop and but
  27. # inside the main while done==False loop.
  28. # Clear the screen and set the screen background
  29. screen.fill(WHITE)
  30. # Draw on the screen a GREEN line from (0,0) to (50.75)
  31. # 5 pixels wide.
  32. pygame.draw.line(screen, GREEN, [0, 0], [50, 30], 5)
  33. # Draw on the screen a GREEN line from (0,0) to (50.75)
  34. # 5 pixels wide.
  35. pygame.draw.lines(screen, BLACK, False, [[0, 80], [50, 90], [200, 80], [220, 30]], 5)
  36. # Draw on the screen a GREEN line from (0,0) to (50.75)
  37. # 5 pixels wide.
  38. pygame.draw.aaline(screen, GREEN, [0, 50], [50, 80], True)
  39. # Draw a rectangle outline
  40. pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
  41. # Draw a solid rectangle
  42. pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
  43. # Draw an ellipse outline, using a rectangle as the outside boundaries
  44. pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)
  45. # Draw an solid ellipse, using a rectangle as the outside boundaries
  46. pygame.draw.ellipse(screen, RED, [300, 10, 50, 20])
  47. # This draws a triangle using the polygon command
  48. pygame.draw.polygon(screen, BLACK, [[100, 100], [0, 200], [200, 200]], 5)
  49. # Draw an arc as part of an ellipse.
  50. # Use radians to determine what angle to draw.
  51. pygame.draw.arc(screen, BLACK, [210, 75, 150, 125], 0, pi / 2, 2)
  52. pygame.draw.arc(screen, GREEN, [210, 75, 150, 125], pi / 2, pi, 2)
  53. pygame.draw.arc(screen, BLUE, [210, 75, 150, 125], pi, 3 * pi / 2, 2)
  54. pygame.draw.arc(screen, RED, [210, 75, 150, 125], 3 * pi / 2, 2 * pi, 2)
  55. # Draw a circle
  56. pygame.draw.circle(screen, BLUE, [60, 250], 40)
  57. # Go ahead and update the screen with what we've drawn.
  58. # This MUST happen after all the other drawing commands.
  59. pygame.display.flip()
  60. # Be IDLE friendly
  61. pygame.quit()

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/984658
推荐阅读
相关标签
  

闽ICP备14008679号