当前位置:   article > 正文

【Python】使用Pycharm IDE在线导入python库操作_pycharm导入keys

pycharm导入keys

各种Python库安装包的下载路径; 
1、非官方库:https://www.lfd.uci.edu/~gohlke/pythonlibs/ 
2、官方库:https://pypi.python.org/pypi 

首先在线的库下载地址,听说要用pip导入,可是我的用不了,还是搜了一下,Pycharm也可以导入

1.点击settings

2.Project Interpreter, 然后点击+号

3.有很多,搜索安装就可以了

比如开发游戏,必备pygame啦,等待,在installing 

 引入一下,就可以测试了,可以看到版本号

很强大的把py打包成exe的Pyinstaller好像也是这样 

 学习Python100实例

  1. #列举由1、2、3、4组成不同数值的三位数
  2. l=0
  3. for i in range(1,5):
  4. for j in range(1,5):
  5. for k in range(1, 5):
  6. if(i!=k) and (i!=j) and (j!=k):
  7. l+=1
  8. print("第%d种:"%l,i,j,k)

列表尝试操作 

  1. list1=[1,2,3,5,7,9,10]
  2. list2=[2,4,6,8]
  3. list1.append(13)
  4. list2.append("append")
  5. del list1[2]
  6. a=len(list1)
  7. print(a,list1[0],list2,list1)

 Python贪吃蛇游戏

  1. import random
  2. import pygame
  3. import sys
  4. from pygame.locals import *
  5. Snakespeed = 10
  6. Window_Width = 800
  7. Window_Height = 500
  8. Cell_Size = 20 # Width and height of the cells
  9. # Ensuring that the cells fit perfectly in the window. eg if cell size was
  10. # 10 and window width or windowheight were 15 only 1.5 cells would
  11. # fit.
  12. assert Window_Width % Cell_Size == 0, "Window width must be a multiple of cell size."
  13. # Ensuring that only whole integer number of cells fit perfectly in the window.
  14. assert Window_Height % Cell_Size == 0, "Window height must be a multiple of cell size."
  15. Cell_W = int(Window_Width / Cell_Size) # Cell Width
  16. Cell_H = int(Window_Height / Cell_Size) # Cellc Height
  17. White = (255, 255, 255)
  18. Black = (0, 0, 0)
  19. Red = (255, 0, 0) # Defining element colors for the program.
  20. Green = (0, 255, 0)
  21. DARKGreen = (0, 155, 0)
  22. DARKGRAY = (40, 40, 40)
  23. YELLOW = (255, 255, 0)
  24. Red_DARK = (150, 0, 0)
  25. BLUE = (0, 0, 255)
  26. BLUE_DARK = (0, 0, 150)
  27. BGCOLOR = Black # Background color
  28. UP = 'up'
  29. DOWN = 'down' # Defining keyboard keys.
  30. LEFT = 'left'
  31. RIGHT = 'right'
  32. HEAD = 0 # Syntactic sugar: index of the snake's head
  33. def main():
  34. global SnakespeedCLOCK, DISPLAYSURF, BASICFONT
  35. pygame.init()
  36. SnakespeedCLOCK = pygame.time.Clock()
  37. DISPLAYSURF = pygame.display.set_mode((Window_Width, Window_Height))
  38. BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
  39. pygame.display.set_caption('Snake')
  40. showStartScreen()
  41. while True:
  42. runGame()
  43. showGameOverScreen()
  44. def runGame():
  45. # Set a random start point.
  46. startx = random.randint(5, Cell_W - 6)
  47. starty = random.randint(5, Cell_H - 6)
  48. wormCoords = [{'x': startx, 'y': starty},
  49. {'x': startx - 1, 'y': starty},
  50. {'x': startx - 2, 'y': starty}]
  51. direction = RIGHT
  52. # Start the apple in a random place.
  53. apple = getRandomLocation()
  54. while True: # main game loop
  55. for event in pygame.event.get(): # event handling loop
  56. if event.type == QUIT:
  57. terminate()
  58. elif event.type == KEYDOWN:
  59. if (event.key == K_LEFT) and direction != RIGHT:
  60. direction = LEFT
  61. elif (event.key == K_RIGHT) and direction != LEFT:
  62. direction = RIGHT
  63. elif (event.key == K_UP) and direction != DOWN:
  64. direction = UP
  65. elif (event.key == K_DOWN) and direction != UP:
  66. direction = DOWN
  67. elif event.key == K_ESCAPE:
  68. terminate()
  69. # check if the Snake has hit itself or the edge
  70. if wormCoords[HEAD]['x'] == -1 or wormCoords[HEAD]['x'] == Cell_W or wormCoords[HEAD]['y'] == -1 or \
  71. wormCoords[HEAD]['y'] == Cell_H:
  72. return # game over
  73. for wormBody in wormCoords[1:]:
  74. if wormBody['x'] == wormCoords[HEAD]['x'] and wormBody['y'] == wormCoords[HEAD]['y']:
  75. return # game over
  76. # check if Snake has eaten an apply
  77. if wormCoords[HEAD]['x'] == apple['x'] and wormCoords[HEAD]['y'] == apple['y']:
  78. # don't remove worm's tail segment
  79. apple = getRandomLocation() # set a new apple somewhere
  80. else:
  81. del wormCoords[-1] # remove worm's tail segment
  82. # move the worm by adding a segment in the direction it is moving
  83. if direction == UP:
  84. newHead = {'x': wormCoords[HEAD]['x'],
  85. 'y': wormCoords[HEAD]['y'] - 1}
  86. elif direction == DOWN:
  87. newHead = {'x': wormCoords[HEAD]['x'],
  88. 'y': wormCoords[HEAD]['y'] + 1}
  89. elif direction == LEFT:
  90. newHead = {'x': wormCoords[HEAD][
  91. 'x'] - 1, 'y': wormCoords[HEAD]['y']}
  92. elif direction == RIGHT:
  93. newHead = {'x': wormCoords[HEAD][
  94. 'x'] + 1, 'y': wormCoords[HEAD]['y']}
  95. wormCoords.insert(0, newHead)
  96. DISPLAYSURF.fill(BGCOLOR)
  97. drawGrid()
  98. drawWorm(wormCoords)
  99. drawApple(apple)
  100. drawScore(len(wormCoords) - 3)
  101. pygame.display.update()
  102. SnakespeedCLOCK.tick(Snakespeed)
  103. def drawPressKeyMsg():
  104. pressKeySurf = BASICFONT.render('Press a key to play.', True, White)
  105. pressKeyRect = pressKeySurf.get_rect()
  106. pressKeyRect.topleft = (Window_Width - 200, Window_Height - 30)
  107. DISPLAYSURF.blit(pressKeySurf, pressKeyRect)
  108. def checkForKeyPress():
  109. if len(pygame.event.get(QUIT)) > 0:
  110. terminate()
  111. keyUpEvents = pygame.event.get(KEYUP)
  112. if len(keyUpEvents) == 0:
  113. return None
  114. if keyUpEvents[0].key == K_ESCAPE:
  115. terminate()
  116. return keyUpEvents[0].key
  117. def showStartScreen():
  118. titleFont = pygame.font.Font('freesansbold.ttf', 100)
  119. titleSurf1 = titleFont.render('Snake!', True, White, DARKGreen)
  120. degrees1 = 0
  121. degrees2 = 0
  122. while True:
  123. DISPLAYSURF.fill(BGCOLOR)
  124. rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1)
  125. rotatedRect1 = rotatedSurf1.get_rect()
  126. rotatedRect1.center = (Window_Width / 2, Window_Height / 2)
  127. DISPLAYSURF.blit(rotatedSurf1, rotatedRect1)
  128. drawPressKeyMsg()
  129. if checkForKeyPress():
  130. pygame.event.get() # clear event queue
  131. return
  132. pygame.display.update()
  133. SnakespeedCLOCK.tick(Snakespeed)
  134. degrees1 += 3 # rotate by 3 degrees each frame
  135. degrees2 += 7 # rotate by 7 degrees each frame
  136. def terminate():
  137. pygame.quit()
  138. sys.exit()
  139. def getRandomLocation():
  140. return {'x': random.randint(0, Cell_W - 1), 'y': random.randint(0, Cell_H - 1)}
  141. def showGameOverScreen():
  142. gameOverFont = pygame.font.Font('freesansbold.ttf', 100)
  143. gameSurf = gameOverFont.render('Game', True, White)
  144. overSurf = gameOverFont.render('Over', True, White)
  145. gameRect = gameSurf.get_rect()
  146. overRect = overSurf.get_rect()
  147. gameRect.midtop = (Window_Width / 2, 10)
  148. overRect.midtop = (Window_Width / 2, gameRect.height + 10 + 25)
  149. DISPLAYSURF.blit(gameSurf, gameRect)
  150. DISPLAYSURF.blit(overSurf, overRect)
  151. drawPressKeyMsg()
  152. pygame.display.update()
  153. pygame.time.wait(500)
  154. checkForKeyPress() # clear out any key presses in the event queue
  155. while True:
  156. if checkForKeyPress():
  157. pygame.event.get() # clear event queue
  158. return
  159. def drawScore(score):
  160. scoreSurf = BASICFONT.render('Score: %s' % (score), True, White)
  161. scoreRect = scoreSurf.get_rect()
  162. scoreRect.topleft = (Window_Width - 120, 10)
  163. DISPLAYSURF.blit(scoreSurf, scoreRect)
  164. def drawWorm(wormCoords):
  165. for coord in wormCoords:
  166. x = coord['x'] * Cell_Size
  167. y = coord['y'] * Cell_Size
  168. wormSegmentRect = pygame.Rect(x, y, Cell_Size, Cell_Size)
  169. pygame.draw.rect(DISPLAYSURF, DARKGreen, wormSegmentRect)
  170. wormInnerSegmentRect = pygame.Rect(
  171. x + 4, y + 4, Cell_Size - 8, Cell_Size - 8)
  172. pygame.draw.rect(DISPLAYSURF, Green, wormInnerSegmentRect)
  173. def drawApple(coord):
  174. x = coord['x'] * Cell_Size
  175. y = coord['y'] * Cell_Size
  176. appleRect = pygame.Rect(x, y, Cell_Size, Cell_Size)
  177. pygame.draw.rect(DISPLAYSURF, Red, appleRect)
  178. def drawGrid():
  179. for x in range(0, Window_Width, Cell_Size): # draw vertical lines
  180. pygame.draw.line(DISPLAYSURF, DARKGRAY, (x, 0), (x, Window_Height))
  181. for y in range(0, Window_Height, Cell_Size): # draw horizontal lines
  182. pygame.draw.line(DISPLAYSURF, DARKGRAY, (0, y), (Window_Width, y))
  183. if __name__ == '__main__':
  184. try:
  185. main()
  186. except SystemExit:
  187. pass

 

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

闽ICP备14008679号