Last active
August 31, 2018 05:15
-
-
Save lamegaton/7665bc7db9665476a9917fdd19142bce to your computer and use it in GitHub Desktop.
#python This script is basic pygame code to move an object back and forth-ward with background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''============================================ | |
For this tutorial, i'm gonna make a simple moving object | |
using pygame. This is based from the chimp tutorial on | |
pygame website | |
editor: Son Pham | |
date: 8/30/2018 | |
files: https://goo.gl/7he7kG | |
python 3.7.0 | |
==============================================''' | |
# import modules | |
import os, sys | |
import pygame | |
from pygame.locals import * | |
# load image and convert to surface object | |
def load_image(name): | |
path = os.path.join('data', name) | |
try: | |
image = pygame.image.load(path) | |
except pygame.error: | |
print('Cannot Load Image', path) | |
image = image.convert() | |
return image, image.get_rect() | |
# sprite is a moving element | |
class bike(pygame.sprite.Sprite): | |
def __init__(self): | |
pygame.sprite.Sprite.__init__(self) | |
self.image, self.rect = load_image('motorcycleCouple.png') | |
screen = pygame.display.get_surface() | |
self.area = screen.get_rect() | |
self.rect.topleft = 10, 150 | |
self.move = 4 | |
def update(self): | |
self._run() | |
# single under score is used for "internal use" | |
#_run function will move a sprite from left to right | |
# and flip | |
def _run(self): | |
newpos = self.rect.move(self.move,0) | |
if self.rect.left < self.area.left or \ | |
self.rect.right > self.area.right: | |
self.move = -self.move | |
newpos = self.rect.move(self.move,0) | |
self.image = pygame.transform.flip(self.image,1,0) | |
self.rect = newpos | |
def main(): | |
#initialize | |
pygame.init() | |
screen = pygame.display.set_mode((500,400)) | |
pygame.display.set_caption('Motorcycle Couple') | |
#create the background using background img | |
background = pygame.image.load('data//background.png').convert() | |
#(optional) create the background using solid color | |
# background = pygame.Surface(screen.get_size()) | |
# background = background.convert() | |
# background.fill((128,128,128)) | |
#put text on background | |
font = pygame.font.Font(None, 36) | |
text = font.render('Press ESC to exit', 1, (10,10,10)) | |
textpos = 280,10 | |
background.blit(text, textpos) #draw text on background | |
#display the background | |
screen.blit(background,(0,0)) | |
#game object | |
clock = pygame.time.Clock() | |
Bike = bike() | |
sprite = pygame.sprite.GroupSingle(Bike) | |
#main loop | |
going = True | |
while going: | |
clock.tick(60) | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
going = False | |
elif event.type == KEYDOWN and event.key == K_ESCAPE: | |
going = False | |
Bike.update() | |
screen.blit(background,(0,0)) | |
sprite.draw(screen) | |
pygame.display.flip() | |
pygame.quit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment