Draw a Moving Circle Pygame

text to screen

Drawing objects with PyGame

For simple things, we might find that we but desire to apply the built in functionality with PyGame to draw various shapes to the surface. Drawing in PyGame is built in, so that'due south like shooting fish in a barrel enough. We tin can draw circles, squares, rectangles, or custom polygons of our choice with coordinates.

Here'due south our new code:

import pygame import time import random  pygame.init()  display_width = 800 display_height = 600  black = (0,0,0) white = (255,255,255) scarlet = (255,0,0)  car_width = 73  gameDisplay = pygame.brandish.set_mode((display_width,display_height)) pygame.display.set_caption('A bit Racey') clock = pygame.time.Clock()  carImg = pygame.image.load('racecar.png')  ####### def things(thingx, thingy, thingw, thingh, color):     pygame.describe.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) #######   def car(x,y):     gameDisplay.blit(carImg,(x,y))  def text_objects(text, font):     textSurface = font.render(text, True, black)     render textSurface, textSurface.get_rect()  def message_display(text):     largeText = pygame.font.Font('freesansbold.ttf',115)     TextSurf, TextRect = text_objects(text, largeText)     TextRect.heart = ((display_width/2),(display_height/2))     gameDisplay.blit(TextSurf, TextRect)      pygame.display.update()      time.sleep(2)      game_loop()            def crash():     message_display('You Crashed')      def game_loop():     10 = (display_width * 0.45)     y = (display_height * 0.8)      x_change = 0 ######     thing_startx = random.randrange(0, display_width)     thing_starty = -600     thing_speed = 7     thing_width = 100     thing_height = 100 ######     gameExit = False      while not gameExit:          for consequence in pygame.event.become():             if outcome.blazon == pygame.QUIT:                 pygame.quit()                 quit()              if event.type == pygame.KEYDOWN:                 if consequence.key == pygame.K_LEFT:                     x_change = -5                 if effect.key == pygame.K_RIGHT:                     x_change = 5              if consequence.type == pygame.KEYUP:                 if event.primal == pygame.K_LEFT or event.key == pygame.K_RIGHT:                     x_change = 0          ten += x_change         gameDisplay.make full(white)       ##########         # things(thingx, thingy, thingw, thingh, colour)         things(thing_startx, thing_starty, thing_width, thing_height, black)         thing_starty += thing_speed         car(ten,y)      ##########         if ten > display_width - car_width or ten < 0:             crash()          if thing_starty > display_height:             thing_starty = 0 - thing_height             thing_startx = random.randrange(0,display_width)                               pygame.display.update()         clock.tick(60)    game_loop() pygame.quit() quit()        

So, the first new affair we come across is:

def things(thingx, thingy, thingw, thingh, color):     pygame.depict.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])        

This function takes x, y starting points, width and height variables, and finally a colour.

From there, we use pygame.draw.rect() to draw the polygon to our specifications. The parameters to this part are : where, what color, and so the x,y location followed past the width and the peak.

Then, in our game_loop we see:

          thing_startx = random.randrange(0,display_width)     thing_starty = -600     thing_speed = 7      thing_width = 100     thing_height = 100        

We want the starting position of the object to be random in information technology'south 10 range, betwixt 0 and the width of our display. Call back that X is left to right, so basically this allows us to have the block start at a random position between the edges of the screen.

Then, we define the starting y position with thing_starty. We specify this as -600. We do this so the actor has a moment to go situated before the block appears on the screen to be avoided.

Then we specify the object's speed. This is how many pixels at a time will information technology move. Correct now, per frame, this block will move 7 pixels. Nosotros tin can increase or subtract this number to directly affect difficulty.

Finally, we ascertain the blocks width and height.

All of these vars are "initial" variables. As the game runs, we will alter these values accordingly. So, within the main logic of our script, we see the addition of:

          things(thing_startx, thing_starty, thing_width, thing_height, blackness)         thing_starty += thing_speed        

What happens hither is nosotros re-call the function to run with the starting variables, and nosotros also add the "thing_speed" (which is 7 pixels that information technology moves each time) to the y position, thing_starty, of the block. So, at 60 FPS, we motility the block downwardly 7 pixels each frame.

If we just stopped here, nosotros'd observe that only i cake is generated, and, once information technology has passed u.s.a., it will only proceed going forever. Nosotros need some handling, like the treatment we used with our car, to create another block once one has moved off of the screen.

That is where:

          if thing_starty > display_height:             thing_starty = 0 - thing_height             thing_startx = random.randrange(0,display_width)        

comes in handy! So, we're just asking if the thing's y location is greater than (recall in computers than x,y = 0,0 means top left!) the display_height, and so information technology has moved all the style down off the screen. If this is the case, then we want to create another cake. Offset we re-assign a y value to the cake, where we use 0-thing_height. Why is this? We do this so the block reappears "off" the screen, to requite the illusion that nosotros accept simply come up upon it, rather than it was spawned... even though it was. If information technology just popped upwardly on the screen, it would await silly. This is THEATER people!

Finally, we re-ascertain the x position of the cake, again, using a range between 0 and the unabridged width of the display. A superior choice here would exist instead to do a range between 0 and display_width - object width. This way the block wont announced in role off-screen. That's okay though, the show must go on!

At that place exists 2 quiz/question(s) for this tutorial. for admission to these, video downloads, and no ads.

The next tutorial:


lucerohateep1954.blogspot.com

Source: https://pythonprogramming.net/drawing-objects-pygame-tutorial/

0 Response to "Draw a Moving Circle Pygame"

แสดงความคิดเห็น

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel