Tuesday, May 30

Problem solving with Python

Introduction

In the last few Sessions in Computing Science I have been working with Python as a language to design a game by writing code. We are using different platforms to learn about computer language  so that we can tell the computer what code to run. I understand that the coding (syntax) on python must be very precise other wise the code will not work. This means every line of code must be correct if there is even one mistake the hole code will not work.


Setting up the code 

We started by creating a template on Python IDLE that we would build our game from we created our template by following along with a video that had a step by step by step process to creating a game.I used hash tags to make reminders about what I need to do as I write my code. This is the code that sets up my window,Width, height and the rate my that the frames of the game run. The hash tags in my code is where I make my notes, if you put a hash tag before something it does not appere on the program window.

# 1 Start pygame
import pygame
import random

# 2. create a Window
WIDTH=360
HEIGHT=480
FPS=30

# 3. Define a few useful colours. This is where I created some of the colors I will use later in my code each color has a series of numbers that equal that color so instead of writing those numbers all the time I made the numbers equal the name of the color.

WHITE=(225, 255,255)
BLACK=(0,0,0)
RED=(255, 0,0)
GREEN=(0, 255,0)
BLUE=(0, 0,255)
CYAN=(0,255,255)

# create sprit movment

#setting up player

class Player(pygame.sprite.Sprite):
    #sprite for the player
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.surface((50,50))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH, HEIGHT)

def update(self):
    self.rect.x+= 5
    if self.rect.left > WIDTH:
        self.rect.right = 0
                                    

# 4. iNITIALISE GAME AND MUSIC AND CREATE WINDOW 

pygame.init()
#pygame.mixer.init() 
pygame.mixer.init()
screen = pygame.display.set_mode(WIDTH, HEIGHT)
pygame.display.set_caption('MY GAME')
clock = pygame.time.Clock()

# Creating sprites
all_sprites = pygame.sprite.Group()
player = Player()
allsprites.add(player)
                                    
all_sprites = pygame.sprite.Group()
# Game loop
running = True
while running:
# keep loop running an the right speed
 clock.tick(FPS)
#Process input
running = True
while running:
    
    # Process input (events)
    for event in pyggame.get():
        
        # cheek for closing everything
        if event.type == pygame.QUIT:
          running = False 
    # Update
    all_sprites.update()
    #Draw / render
    screen.fill(RED)
    all_sprites.draw(screen)
    # after drawing everything, flip the display
    pygame.display.flip()

    pygame.quit()
    


No comments:

Post a Comment