Over the last two weeks I have been working Python as a language. I am learning to code in a different way than scratch because with Python you write your code not drag and drop.
I have been using three different platforms so I can can get a better understanding of the language that we use to code on computers.
Setting up a game in the Python IDLE window has been a long process, I had open up the IDLE window than import pygame which is already downloaded on to the computers, but I had to write import pygame to get the computer to bring it to the IDLE window when I could then work with it to make a game. I had to write how big I wanted the height and width and how FPS (frames per second). When writing notes while programming I had to put a hashtag in front of it so it dosen't show up while running the code.
This is the code I ended up writing.
# 1.Start Pygame
import pygame
import random
# 2.Create a window
WIDTH = 360
HEIGHT = 480
FPS = 30
# 3. Define a few useful colors
WHITE=(255, 255, 255)
BLACK=(0, 0, 0)
RED=(255, 0, 0)
GREEN=(0, 255, 0)
BLUE=(0, 0, 255)
# Setting up the 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 / 2, HEIGHT / 2)
def update(self):
self.rect.x += 5
if self.rect.left > WIDTH:
self.rect.right = 0
# 4. Initialise pygame and create window
pygame.init()
#pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My game")
clock = pygame.time.Clock()
#creating spirtes
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# Game loop
running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
# Procees input (events)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
# Update
all_sprites.update()
# Draw / render
screen.fill(BLACK)
all_sprites.draw(screen)
# *after* drawing everything, flip the display
pygame.display.flip()
pygame.quit()
Comments
Post a Comment