Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created December 7, 2015 16:03
Show Gist options
  • Save AndyNovo/c0d3c78185dedc13afe6 to your computer and use it in GitHub Desktop.
Save AndyNovo/c0d3c78185dedc13afe6 to your computer and use it in GitHub Desktop.
import pygame
import random
BLACK = (0,0,0)
pygame.init()
WIDTH = 450
HEIGHT = 600
screen = pygame.display.set_mode([WIDTH, HEIGHT])
another_loop = True
clock = pygame.time.Clock()
#Assumes an image names bwclear1.gif is in the current directory
lsprite = pygame.image.load("bwclear1.gif") #load an image (blit it onto screen later)
rsprite = pygame.transform.flip(lsprite, True, False) #this is a right facing copy
sprite = lsprite #this variable will store the current directioned version
wx = 100
wy = 100
scale = 1.0 #I'm going to also allow scaling up and down
wiz_w, wiz_h = lsprite.get_size() #We can get the original dimensions this way
game_x = 1
game_y = 1
class Wizard:
def __init__(self):
self.game_x = 0
self.game_y = 0
self.pix_x = 20
self.pix_y = 20
self.speed = 2
self.x_speed = 0
self.y_speed = 0
self.image = rsprite
def game_xy_to_pixel_xy(self):
return (20 + self.game_x*100, 20 + self.game_y*100)
def move_down(self, amount):
if amount != 0:
self.y_speed = 2
self.game_y += amount
def move_right(self, amount):
if amount != 0:
self.x_speed = 2
if amount > 0:
self.image = rsprite
elif amount < 0:
self.image = lsprite
self.game_x += amount
def update(self):
target = self.game_xy_to_pixel_xy()
if target == (self.pix_x, self.pix_y):
self.x_speed = 0
self.y_speed = 0
else:
if target[0] > self.pix_x:
self.pix_x += 2
else:
self.pix_x -= 2
if target[1] > self.pix_y:
self.pix_y += 2
else:
self.pix_y -= 2
def draw(self):
screen.blit(self.image, (self.pix_x, self.pix_y))
character = Wizard()
while another_loop:
# --- Basic Event Processing
for event in pygame.event.get():
if event.type == pygame.QUIT:
another_loop = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
character.move_right(1)
elif event.key == pygame.K_DOWN:
character.move_down(1)
elif event.key == pygame.K_LEFT:
character.move_right(-1)
elif event.key == pygame.K_UP:
character.move_down(-1)
# Every loop we will wipe the screen and redraw images
screen.fill(BLACK)
character.update()
character.draw()
#This sets the frames per second
clock.tick(60)
#This puts the new stuff you've drawn on screen
pygame.display.flip()
#When we've left the while loop exit pygame
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment