Python 3 game 2


SUBMITTED BY: Wormlee2010

DATE: May 27, 2021, 6:53 p.m.

FORMAT: Python 3

SIZE: 2.4 kB

HITS: 461

  1. run = True
  2. while run:
  3. for event in pygame.event.get(): # Loop through a list of events
  4. if event.type == pygame.QUIT: # See if the user clicks the red x
  5. run = False # End the loop
  6. pygame.quit() # Quit the game
  7. quit()
  8. run = True
  9. speed = 30 # NEW
  10. while run:
  11. clock.tick(speed) # NEW
  12. bgX -= 1.4 # Move both background images back
  13. bgX2 -= 1.4
  14. if bgX < bg.get_width() * -1: # If our bg is at the -width then reset its position
  15. bgX = bg.get_width()
  16. if bgX2 < bg.get_width() * -1:
  17. bgX2 = bg.get_width()
  18. for event in pygame.event.get():
  19. if event.type == pygame.QUIT:
  20. run = False
  21. pygame.quit()
  22. quit()
  23. def redrawWindow():
  24. win.blit(bg, (bgX, 0)) # draws our first bg image
  25. win.blit(bg, (bgX2, 0)) # draws the seconf bg image
  26. pygame.display.update() # updates the screen
  27. # Call this from the game loop!
  28. pygame.time.set_timer(USEREVENT+1, 500) # Sets the timer for 0.5 seconds
  29. # This should go above the game loop
  30. while run:
  31. redrawWindow()
  32. bgX -= 1.4
  33. bgX2 -= 1.4
  34. if bgX < bg.get_width() * -1:
  35. bgX = bg.get_width()
  36. if bgX2 < bg.get_width() * -1:
  37. bgX2 = bg.get_width()
  38. for event in pygame.event.get():
  39. if event.type == pygame.QUIT:
  40. run = False
  41. pygame.quit()
  42. quit()
  43. if event.type == USEREVENT+1: # Checks if timer goes off
  44. speed += 1 # Increases speed
  45. clock.tick(speed)
  46. runner = player(200, 313, 64, 64)
  47. # This should go above our game loop
  48. def redrawWindow():
  49. win.blit(bg, (bgX, 0))
  50. win.blit(bg, (bgX2, 0))
  51. runner.draw(win) # NEW
  52. pygame.display.update()
  53. # Should go inside the game loop
  54. keys = pygame.key.get_pressed()
  55. if keys[pygame.K_SPACE] or keys[pygame.K_UP]: # If user hits space or up arrow key
  56. if not(runner.jumping): # If we are not already jumping
  57. runner.jumping = True
  58. if keys[pygame.K_DOWN]: # If user hits down arrow key
  59. if not(runner.sliding): # If we are not already sliding
  60. runner.sliding = True
  61. # Because we have a starter file this is all we have to do to move our character.
  62. # The physics and math behind the movement has been coded for you.

comments powered by Disqus