import pygame, math from pygame import * DISPLAY = (800,640) DEPTH = 32 FLAGS = 0 ############################################################## # Classes! class Entity(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) ############################################################## class Player(Entity): def __init__(self, x, y): Entity.__init__(self) self.xvel = 0 self.yvel = 0 self.onGround = False self.image = Surface((32, 32)) self.image.convert() self.image.fill(Color("#FF0000")) self.rect = Rect(x, y, 32, 32) ############################################################## def update(self, up, down, left, right, platforms): if up: # Only jump if on the ground if self.onGround: self.yvel -= 10 if down: pass if left: self.xvel = -5 if right: self.xvel = 5 if not self.onGround: # Only accelerate with gravity if in the air self.yvel += 0.3 # Max falling speed if self.yvel > 30: self.yvel = 30 if not (left or right): self.xvel = 0 # Increment in x direction self.rect.left += self.xvel # Do x-axis collisions self.collide(self.xvel, 0, platforms) # Incrememnt in y direction self.rect.top += self.yvel # Assuming we're in the air self.onGround = False; # Do y-axis collisions self.collide(0, self.yvel, platforms) ############################################################## def collide(self, xvel, yvel, platforms): for p in platforms: if sprite.collide_rect(self, p): if isinstance(p, ExitBlock): event.post(event.Event(QUIT)) if xvel > 0: self.rect.right = p.rect.left if xvel < 0: self.rect.left = p.rect.right if yvel > 0: self.rect.bottom = p.rect.top self.onGround = True self.yvel = 0 if yvel < 0: self.rect.top = p.rect.bottom ############################################################## class Platform(Entity): def __init__(self, x, y): Entity.__init__(self) self.image = Surface((32, 32)) self.image.convert() self.image.fill(Color("#DDDDDD")) self.rect = Rect(x, y, 32, 32) def update(self): pass ############################################################## class ExitBlock(Platform): def __init__(self, x, y): Platform.__init__(self, x, y) self.image.fill(Color("#0033FF")) ######################################################################################################################### def main(): pygame.init() screen = display.set_mode(DISPLAY, FLAGS, DEPTH) display.set_caption('Use arrows to move!') timer = time.Clock() up = down = left = right = False bg = Surface((32, 32)) bg.convert() bg.fill(Color("#000000")) entities = pygame.sprite.Group() player = Player(32, 32) platforms = [] ############################################################## x = y = 0 level = [ "PPPPPPPPPPPPPPPPPPPPPPPPP", "P P", "PP P", "P PP P", "P PPP P", "P PPPP P", "P PPPPP P", "P PPPPP P", "P P", "P P", "P PP", "P P P", "P PPPPP P", "P PPP P", "P EEE P", "P P", "P P P", "P P", "P P", "PPPPPPPPPPPPPPPPPPPPPPPPP",] ############################################################## for row in level: for col in row: if col == "P": p = Platform(x, y) platforms.append(p) entities.add(p) if col == "E": e = ExitBlock(x, y) platforms.append(e) entities.add(e) x += 32 y += 32 x = 0 ############################################################## entities.add(player) running = True while running: timer.tick(100) for e in pygame.event.get(): if e.type == QUIT: running = False if e.type == KEYDOWN and e.key == K_UP: up = True if e.type == KEYDOWN and e.key == K_DOWN: down = True if e.type == KEYDOWN and e.key == K_LEFT: left = True if e.type == KEYDOWN and e.key == K_RIGHT: right = True if e.type == KEYUP and e.key == K_UP: up = False if e.type == KEYUP and e.key == K_DOWN: down = False if e.type == KEYUP and e.key == K_LEFT: left = False if e.type == KEYUP and e.key == K_RIGHT: right = False ############################################################## # Draw the background for y in range(20): for x in range(25): screen.blit(bg, (x * 32, y * 32)) # Update the player, draw everything else player.update(up, down, left, right, platforms) entities.draw(screen) pygame.display.flip() ############################################################## main() pygame.quit() ##############################################################
Showing posts with label game. Show all posts
Showing posts with label game. Show all posts
Python | Pygame | True
All I can say, is finally.
Python | Revision test | Chemistry
Hopefully this will help me
while 1: a = 'NaCO3 + 2HCl -> 2NaCl + H2O + CO2' b = 'Pb(NO3) + 2Kl -> Pbl2 + 2KNO3' c = 'Zn + 2HCl -> ZnHCl2 + H2' aa = input("What's the WE for Sodium carbonate + Hydrochloric acid? ") if aa == a: print('correct!') else: print('no') bb = input("What's the WE for Pottasium iodide + Lead nitrate? ") if bb == b: print('correct!') else: print('no') cc = input('What\'s the WE for Hydrochloric acid + Zinc metal? ') if cc == c: print('correct!') else: print('no')
Python | Pygame collision
This is what I did, from a tutorial :'(
import pygame pygame.init() size = (1400, 800) window = pygame.display.set_mode(size) pygame.display.set_caption('Window') white = (255, 255, 255) black = (0,0,0) red = (255,25,25) moveX, moveY = 0, 0 clock = pygame.time.Clock() def detect(x1, y1, w1, h1, x2, y2, w2, h2): if x2 + w2 >= x1 >= x2 and y2 + h2 >= y1 >= y2: return True elif x2 + w2 >= x1 + w1 >= x2 and y2 + h2 >= y1 >= y2: return True elif x2 + w2 >= x1 >= x2 and y2 + h2 >= y1 + h1 >= y2: return True elif x2 + w2 >= x1 + w1 >= x2 and y2 + h2 >= y1 + h1 >= y2: return True else: return False class Sprite: def __init__(self,x,y): self.x = x self.y = y self.width = 50 self.height = 50 def render(self, coll): if coll: pygame.draw.rect(window, red, (self.x, self.y, self.width, self.height)) elif coll is False: pygame.draw.rect(window, black, (self.x, self.y, self.width, self.height)) Sprite1 = Sprite(100,150) Sprite2 = Sprite(200, 250) run = True while run: for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: moveX = -5 if event.key == pygame.K_RIGHT: moveX = 5 if event.key == pygame.K_UP: moveY = -5 if event.key == pygame.K_DOWN: moveY = 5 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: moveX = 0 if event.key == pygame.K_RIGHT: moveX = 0 if event.key == pygame.K_UP: moveY = 0 if event.key == pygame.K_DOWN: moveY = 0 window.fill(white) Sprite1.x+=moveX Sprite1.y+=moveY coll = detect(Sprite1.x, Sprite1.y, Sprite1.width, Sprite1.height, Sprite2.x, Sprite2.y, Sprite2.width, Sprite2.height) Sprite1.render(coll) Sprite2.render(False) clock.tick(120) pygame.display.flip() pygame.quit()
Python | O.M.G lol pygame is SO GREAT.
SO I FINALLY LEARNT HOW TO ADD IMAGES, USE KEY EVENTS, AND SET BACKGROUNDS, I MADE SOMETHING THAT WILL REVOLUTIONIZE THE WORLD (aka it's useless)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | # Importing pygame import pygame # Initializing pygame pygame.init() # Setting window and title window = pygame.display.set_mode((800,450)) pygame.display.set_caption('Mover') # Defining colors black = (0,0,0) white = (255, 255, 255) # Image mario = pygame.image.load('m1.png').convert_alpha() bg = pygame.image.load('bg.jpg') bg = pygame.transform.scale(bg, (800,450)) gun = pygame.image.load('gun.png') gun = pygame.transform.scale(gun, (140, 95)) # Running running = True # Defining the clock clock = pygame.time.Clock() # Identifying coordinates x, y = 0,0 moveX, moveY = 0,0 # Main Loop while running: for event in pygame.event.get(): # If X is clicked, running = False, which causes loop to exit. if event.type == pygame.QUIT: running = False # If key is pressed, add {} to (var) if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: moveX = -5 if event.key == pygame.K_RIGHT: moveX = 5 if event.key == pygame.K_UP: moveY = -5 if event.key == pygame.K_DOWN: moveY = 5 # Same if key, except all (var) = 0 if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT: moveX = 0 if event.key == pygame.K_RIGHT: moveX = 0 if event.key == pygame.K_UP: moveY = 0 if event.key == pygame.K_DOWN: moveY = 0 # Make background color white window.blit(bg, (0,0)) # Define x and y x += moveX y += moveY # Draw a rectangle, set it to relative coordinates # pygame.draw.rect(window, black, (x, y, 25, 25)) # Set image to coordinates window.blit(gun, (x,y)) # Set frames per second clock.tick(50) # Required command pygame.display.flip() # Exit when loop is False pygame.quit() |
Python | Pygame
Now we're at the top, idk, finally looking into game development, wish me luck...
import pygame, sys from pygame.locals import * # Initialize pygame pygame.init() #Set window | Title window = pygame.display.set_mode((800,600)) pygame.display.set_caption('PyGame') # Colours white = (255, 255, 255) black = (0,0,0) # Images logo = pygame.image.load('logo.png').convert_alpha() # Set running to True running = True # Main loop while running: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() # Setting window background to white window.fill(white) # Adding an image window.blit(logo, (0,0)) # Necissary code pygame.display.flip()
Python | Typing Test
This is the coolest thing ever ok.
from time import time q = input('What do you want to type? ') a = ' ' record = [] while a != '': start = time() a = input('Type: ') end = time() v = end-start record.append(v) if a == q: print('Time taken to type chosen word: {:.2f}'.format(v)) else: break record = record[:-1] print('Your Highschore is: {:.2f} seconds.'.format(min(record)))
Python | Guess the secret number!
Upon learning about the 'random' module, I made a little guessing game, enjoy..
1 2 3 4 5 6 7 8 9 10 11 12 | import random x = random.randint(1, 100) while 1: v = int(input('Guess the number between 1 and 100!: ')) if v == x: print('That\'s right! The secret number is ', x) elif v > x: print('That\'s too high!') elif v < x: print('That\'s too low!') else: print('Please choose a number between 1 and 100!!') |
Python | Adventure Game
This is my second program I built and it's just a basic game, it asks you questions, with the optional answers and the end of the line, and results differently depending on what you chose.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | def start(): print("Welcome to Bane of Athropods") print("Do you want to fight the beast? (yes | no)") yorn = input() if yorn == "yes": print("Let the battle begin!") print(' ') elif yorn == "no": print("Too bad! Woos!") print(' ') else: print("What? Quick, the battle is about to start, survive!") print(' ') def battle(): print("How do you attack? (punch | kick)") q1 = input() if q1 == "punch": print('You punched the beast, he is too strong, he felt nothing!!') print('The beast punched you back!') print('...') print('You died.') print(' ') elif q1 == "kick": print('''You kicked the beast in the balls, that is a dog shot, but it gives you enough time to escape, so RUN!''') print('The beast is up and running! It is chasing you, and catching up!') print('The beast caught you!') print('The beast crushed you to death!') print('...') print('You died.') print(' ') else: print("You didn't PUNCH or KICK! You were too slow and now you're dead!") print("Since you are now dead, I would like to thankyou for giving me your soul.") print(' ') print("Wait.. This soul, it.. it isn't humane, what are you?") start() battle() |