Browse Source

Add files via upload

PythonCoder8 4 years ago
parent
commit
2aa2dc8334
3 changed files with 116 additions and 0 deletions
  1. 50 0
      bounce_text.py
  2. 27 0
      hello_world.py
  3. 39 0
      move_hw_text.py

+ 50 - 0
bounce_text.py

@@ -0,0 +1,50 @@
+import pygame, sys
+from keyboard import is_pressed
+
+#Initialize pygame and set up window
+pygame.init()
+winSize = (800, 600)
+screen = pygame.display.set_mode(winSize)
+pygame.display.set_caption('Bounce text inside boundaries')
+
+#Load font and render text
+myriadProFont = pygame.font.SysFont('Myriad Pro', 48)
+helloWorld = myriadProFont.render('Hello World!', 1, (255, 0, 255), (255, 255, 255))
+
+helloWorldSize = helloWorld.get_size()
+
+x, y = 0, 0
+
+directionX, directionY = 1, 1
+
+#Add clock so that the next frame is displayed at an exact time
+clock = pygame.time.Clock()
+
+while True:
+    
+    if is_pressed('q'):
+        sys.exit()
+    
+    clock.tick(40)
+
+    #Keep screen open and let user exit window using for loop
+    for ev in pygame.event.get():
+        if ev.type == pygame.QUIT:
+            sys.exit()
+    
+    screen.fill((0,0,0))
+
+    screen.blit(helloWorld, (x, y))
+
+    x += 6 * directionX
+    
+    y += 6 * directionY
+
+    if x + helloWorldSize[0] > 800 or x <= 0:
+        directionX *= -1
+        
+    if y + helloWorldSize[0] > 760 or y <= 0:
+        directionY *= -1
+    
+    #Update window    
+    pygame.display.update()

+ 27 - 0
hello_world.py

@@ -0,0 +1,27 @@
+import pygame, sys
+from keyboard import is_pressed
+
+#Initialize pygame and set up window
+pygame.init()
+winSize = (800, 600)
+screen = pygame.display.set_mode(winSize)
+pygame.display.set_caption('Display text')
+
+#Load font and render text
+myriadProFont = pygame.font.SysFont('Myriad Pro', 48)
+helloWorld = myriadProFont.render('Hello World!', 1, (255, 0, 255), (255, 255, 255))
+
+while True:
+    
+    if is_pressed('q'):
+        sys.exit()
+    
+    #Keep screen open and let user exit window using for loop
+    for ev in pygame.event.get():
+        if ev.type == pygame.QUIT:
+            sys.exit()
+
+    screen.blit(helloWorld, (0, 0))
+
+    #Update window
+    pygame.display.update()

+ 39 - 0
move_hw_text.py

@@ -0,0 +1,39 @@
+import pygame, sys
+from keyboard import is_pressed
+
+#Initialize pygame and set up window
+pygame.init()
+winSize = (800, 600)
+screen = pygame.display.set_mode(winSize)
+pygame.display.set_caption('Move text')
+
+#Load font and render text
+myriadProFont = pygame.font.SysFont('Myriad Pro', 48)
+helloWorld = myriadProFont.render('Hello World!', 1, (255, 0, 255), (255, 255, 255))
+
+#Create x and y coordinate variables for displaying text on screen
+x, y = 0, 0
+
+#Add clock so that the next frame is displayed at an exact time
+clock = pygame.time.Clock()
+
+while True:
+    
+    if is_pressed('q'):
+        sys.exit()
+
+    clock.tick(40)
+
+    #Keep screen open and let user exit window using for loop
+    for ev in pygame.event.get():
+        if ev.type == pygame.QUIT:
+            sys.exit()
+    
+    screen.fill((0,0,0))
+
+    screen.blit(helloWorld, (x, y))
+
+    x += 6
+
+    #Update window
+    pygame.display.update()