Category: Uncategorized

A Good Tutorial

After searching the internet for a very long time I finally found a Pygame tutorial for my version of python. http://www.nerdparadise.com/programming/pygame/part1

It’s fairly simple, and it’s just for basic Pygame functions, but it will work for the short period of time in which I will be doing this project.

The first part of the tutorial shows how to use the Pygame event handlers to get keyborad inputs, and move the square on the screen using the arrow keys.

Here’s what it looks like, even though it’s hard to demonstrate motion with a picture:

Capture.JPGCapture.JPG

It does move quite smoothly.

The next step is following the next part of the tutorial to see where it will lead.

Here’s the code if you’re interested:

import pygame

pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
is_blue = True
x = 30
y = 30

clock = pygame.time.Clock()

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            is_blue = not is_blue

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]: y -= 3
    if pressed[pygame.K_DOWN]: y += 3
    if pressed[pygame.K_LEFT]: x -= 3
    if pressed[pygame.K_RIGHT]: x += 3

    screen.fill((0, 0, 0))
    if is_blue:
        color = (0, 128, 255)
    else:
        color = (255, 100, 0)
    pygame.draw.rect(screen, color, pygame.Rect(x, y, 60, 60))

    pygame.display.flip()
    clock.tick(60)

Something to do for the last week

Today, I have to come up with something to do for the last few days of school.

I haven’t thought about it a lot however I think I’m going to do something with the module pygame. It has a lot of functionality built in for making video games, however it also has a lot for doing object collision detection, and overall graphics. I think it will be a good thing to practice with for a few days.

A few things I can try would be:

  • playing sounds
  • detecting collisions
  • fractals (maybe)
  • particle simulations

pygame is a great library and can be used for a lot of things. If you are advanced you can even do some things in 3d https://www.youtube.com/watch?v=JHYAWtnOJuk

for now I will just familiarize myself with the library using the documentation https://www.pygame.org/docs/

Map Generator [Final]

Today I added the finishing touches onto my map generation software.

Here’s a list of the changes I made:

  • Added a luck value to determine special biomes
    • ice age
    • desert
    • alien planet
  • Made the map randomly choose a season
  • Added a size option at runtime
  • Made the whole program loop indefinitely for continual map generation

The program now accomplishes what I had in mind at the start of the project.

Here is an example of a few outputs of differing sizes:

MapOutput0.png

MapOutput2.png

MapOutput3.png

And here’s the final code (2 parts):

from GridMapGen import Grid
import Perlin
from PIL import Image, ImageDraw
import os
from GridMapGen import  HeatMapGenerator
import random


def copy_image(img_1, img_2, width, height):

    for x in range(width):
        for y in range(height):
            got_pixel = img_1.getpixel((x, y))
            img_2.putpixel((x, y), (got_pixel[0], got_pixel[1], got_pixel[2]))


def main(image_index):

    luck = random.randint(0, 1000)

    imgx = int(input("Enter width"))
    imgy = int(input("Enter height"))

    path = os.path.dirname(os.path.abspath(__file__))

    HeatMapGenerator.create_map(imgx, imgy)

    Perlin.generate(imgx, imgy)

    perlin_map = Image.open(path+'/PerlinNoise.png', mode='r')

    temp_heat_map = Image.open(path+'/HeatMap.png', mode='r')

    heat_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))

    copy_image(temp_heat_map, heat_map, imgx, imgy)

    final_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))

    humidity_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))

    copy_image(perlin_map, humidity_map, imgx, imgy)

    Perlin.generate(imgx, imgy)

    perlin_map = Image.open(path+'/PerlinNoise.png', mode='r')

    height_map = Image.new('RGB', (imgx, imgy), (0, 0, 0))

    copy_image(perlin_map, height_map, imgx, imgy)

    grid_size = (1, 1)

    season = random.randint(0, 3)

    for x in range(int(imgx/grid_size[0])):
        for y in range(int(imgy/grid_size[1])):
            test_grid = Grid.Grid(grid_size[0], grid_size[1], x*grid_size[0], y*grid_size[1], season+1, luck)
            test_grid.assign_temperature(heat_map)
            test_grid.assign_altitude(height_map)
            test_grid.assign_humidity(humidity_map)
            test_grid.draw(final_map)
    final_map.show()
    final_map.save(path+'/MapOutput' + str(image_index) + '.png')


if __name__ == '__main__':
    for i in range(4):
        main(i)
from PIL import Image, ImageDraw


class Grid:
    humidity = 0
    altitude = 0
    temperature = 0
    grid_type = 0
    x_position = 0
    y_position = 0
    width = 0
    height = 0
    season = 1
    luck = 0

    def assign_humidity(self, image):

        humidity_counter = 0

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                pixel_value = image.getpixel((scan_x, scan_y))[0]
                humidity_counter += pixel_value

        humidity_counter /= (self.width * self.height)

        self.humidity = int(humidity_counter)

    def assign_altitude(self, image):

        altitude_counter = 0

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                pixel_value = image.getpixel((scan_x, scan_y))[0]
                altitude_counter += pixel_value

        altitude_counter /= (self.width * self.height)

        self.altitude = int(altitude_counter)

    def assign_temperature(self, image):

        temperature_counter = 0

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                pixel_value = image.getpixel((scan_x, scan_y))[0]
                temperature_counter += pixel_value

        temperature_counter /= (self.width * self.height)

        self.temperature = int(temperature_counter)

    def __init__(self, width, height, x_pos, y_pos, season, luck):
        self.width = width
        self.height = height
        self.x_position = x_pos
        self.y_position = y_pos
        self.season = season
        self.luck = luck

    def draw(self, image):
        _deep_ocean = (0, 0, 120)
        _shallows = (54, 56, 209)
        _ice = (186, 224, 246)
        _ocean = (35, 35, 185)
        _snow = (255, 255, 255)
        _sand = (219, 205, 116)
        _plains = (195, 229, 117)
        _swamp = (91, 101, 2)
        _grass = (29, 186, 81)
        _desert = (255, 229, 129)
        _rock = (159, 156, 142)
        _cold_sand = (162, 201, 163)
        _wet_sand = (118, 141, 83)
        _tundra = (131, 134, 102)
        _boreal = (25, 125, 75)
        _depths = (0, 0, 50)
        _dirty_water = (64, 110, 141)
        _icy_depths = (191, 199, 236)

        if 150 < self.luck < 250 and self.season == 3:
            self.season = 1

        elif 150 < self.luck < 250 and self.season == 4:
            self.season = 2

        if self.season == 1:
            self.temperature += 10

        elif self.season == 2:
            self.temperature += 50
            self.altitude += 5
            self.humidity -= 10
            _snow = (166, 161, 150)

        elif self.season == 3:
            self.temperature -= 5
            _plains = (230, 171, 68)
            _grass = (181, 213, 107)

        elif self.season == 4:
            self.temperature -= 35
            self.humidity += 10
            _grass = (241, 255, 236)
            _boreal = (180, 232, 204)
            _shallows = _ice
            _desert = _snow
            _plains = _grass
            _dirty_water = (218, 225, 196)

        if 100 < self.luck < 150:
            self.temperature -= 40
            self.humidity += 25

        if self.luck < 100:
            self.temperature += 75
            self.humidity -= 40
            self.altitude += 12

        if 150 < self.luck < 250:
            self.humidity += 50
            self.temperature += 20
            self.altitude -= 2
            _grass = _boreal
            _rock = (101, 76, 25)

        if self.luck > 900:
            _grass = (198, 190, 89)
            _rock = (70, 69, 57)
            _ocean = (70, 69, 92)
            _shallows = (86, 85, 118)
            _deep_ocean = (34, 33, 58)
            _snow = (240, 211, 246)

        if self.luck > 990:
            _grass = (199, 177, 144)
            _ocean = (168, 199, 94)
            _deep_ocean = (116, 134, 75)
            _shallows = (179, 233, 52)
            _rock = (119, 122, 173)
            _snow = (255, 255, 255)
            _desert = (105, 191, 142)
            _plains = (119, 152, 133)
            _swamp = (211, 216, 68)
            _boreal = (22, 233, 29)

        if self.altitude <= 60 and self.temperature < 50:
            self.grid_type = _icy_depths

        if self.altitude <= 60:
            self.grid_type = _depths

        elif self.altitude <= 95 and self.temperature < 50:
            self.grid_type = _snow

        elif self.altitude < 119 and self.temperature < 50:
            self.grid_type = _ice

        elif self.altitude <= 95:
            self.grid_type = _deep_ocean

        elif self.altitude <= 110:
            self.grid_type = _ocean

        elif self.altitude < 119:
            self.grid_type = _shallows

        elif self.altitude == 120 or self.altitude == 119 and 150 > self.temperature > 100:
            self.grid_type = _sand

        elif self.altitude == 120 or self.altitude == 119 and self.temperature <= 100:
            self.grid_type = _cold_sand

        elif self.altitude == 120 or self.altitude == 119 and self.humidity > 100:
            self.grid_type = _wet_sand

        elif self.altitude == 120 or self.altitude == 119:
            self.grid_type = _sand

        elif 151 > self.altitude > 120 and self.temperature > 125 and self.humidity < 95:
            self.grid_type = _desert

        elif 130 > self.altitude > 120 and self.temperature > 100 and self.humidity > 160:
            self.grid_type = _swamp

        elif 141 > self.altitude > 120 and self.temperature > 100 and self.humidity > 160:
            self.grid_type = _dirty_water

        elif self.altitude > 175 and self.temperature < 175:
            self.grid_type = _snow

        elif 175 >= self.altitude >= 150:
            self.grid_type = _rock

        elif 140 > self.altitude > 130 and self.humidity < 130 and self.temperature > 100:
            self.grid_type = _plains

        elif 150 > self.altitude > 145 and self.temperature < 100:
            self.grid_type = _tundra

        elif 145 >= self.altitude >= 140 and self.temperature < 105:
            self.grid_type = _boreal

        elif self.altitude > 120:
            self.grid_type = _grass

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                image.putpixel((scan_x, scan_y), self.grid_type)


def main():
    grid = Grid(0, 0, 0, 0)


if __name__ == '__main__':
    main()

Map Generator [7]

Today I expanded upon the systems I built last time for the generation of temperature, and humidity.

I decided that it would be interesting to add seasons into my map generator, so I did a simple bit of code that slightly changes the humidity, and temperature values of each season, as well as adding a few other colour differences to make it seem more like the season it’s going for. Overall the result is pretty decent.

Below I have an example of one map for all four seasons.

Here is Spring:MapOutput0.png

 

Summer:MapOutput1.png

Fall:MapOutput2.png

And Winter:MapOutput3.png

And here’s the new code:

from PIL import Image, ImageDraw


class Grid:

    humidity = 0
    altitude = 0
    temperature = 0
    grid_type = 0
    x_position = 0
    y_position = 0
    width = 0
    height = 0
    season = 1

    def assign_humidity(self, image):

        humidity_counter = 0

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                pixel_value = image.getpixel((scan_x, scan_y))[0]
                humidity_counter += pixel_value

        humidity_counter /= (self.width*self.height)

        self.humidity = int(humidity_counter)

    def assign_altitude(self, image):

        altitude_counter = 0

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                pixel_value = image.getpixel((scan_x, scan_y))[0]
                altitude_counter += pixel_value

        altitude_counter /= (self.width*self.height)

        self.altitude = int(altitude_counter)

    def assign_temperature(self, image):

        temperature_counter = 0

        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                pixel_value = image.getpixel((scan_x, scan_y))[0]
                temperature_counter += pixel_value

        temperature_counter /= (self.width*self.height)

        self.temperature = int(temperature_counter)

    def __init__(self, width, height, x_pos, y_pos, season):
        self.width = width
        self.height = height
        self.x_position = x_pos
        self.y_position = y_pos
        self.season = season

    def draw(self, image):
        _deep_ocean = (0, 0, 120)
        _shallows = (54, 56, 209)
        _ice = (186, 224, 246)
        _ocean = (35, 35, 185)
        _snow = (255, 255, 255)
        _sand = (219, 205, 116)
        _plains = (195, 229, 117)
        _swamp = (91, 101, 2)
        _grass = (29, 186, 81)
        _desert = (255, 229, 129)
        _rock = (159, 156, 142)
        _cold_sand = (162, 201, 163)
        _wet_sand = (118, 141, 83)
        _tundra = (131, 134, 102)
        _boreal = (25, 125, 75)
        _depths = (0, 0, 50)
        _dirty_water = (64, 110, 141)
        _icy_depths = (191, 199, 236)

        if self.season == 1:
            self.temperature += 10

        elif self.season == 2:
            self.temperature += 35
            self.altitude += 5
            self.humidity -= 10
            _snow = (166, 161, 150)

        elif self.season == 3:
            self.temperature -= 5
            _plains = (230, 171, 68)
            _grass = (181, 213, 107)

        elif self.season == 4:
            self.temperature -= 35
            self.humidity += 10
            _grass = (241, 255, 236)
            _boreal = (180, 232, 204)
            _shallows = _ice
            _desert = _snow
            _plains = _grass


        if self.altitude <= 60 and self.temperature < 50:
            self.grid_type = _icy_depths

        if self.altitude <= 60:
            self.grid_type = _depths

        elif self.altitude <= 95 and self.temperature < 50:
            self.grid_type = _snow

        elif self.altitude < 119 and self.temperature < 50:
            self.grid_type = _ice

        elif self.altitude <= 95:
            self.grid_type = _deep_ocean

        elif self.altitude <= 110:
            self.grid_type = _ocean

        elif self.altitude < 119:
            self.grid_type = _shallows

        elif self.altitude == 120 or self.altitude == 119 and 150 > self.temperature > 100:
            self.grid_type = _sand

        elif self.altitude == 120 or self.altitude == 119 and self.temperature <= 100:
            self.grid_type = _cold_sand

        elif self.altitude == 120 or self.altitude == 119 and self.humidity > 100:
            self.grid_type = _wet_sand

        elif self.altitude == 120 or self.altitude == 119:
            self.grid_type = _sand

        elif 151 > self.altitude > 120 and self.temperature > 125 and self.humidity < 95:
            self.grid_type = _desert

        elif 130 > self.altitude > 120 and self.temperature > 100 and self.humidity > 160:
            self.grid_type = _swamp

        elif 141 > self.altitude > 120 and self.temperature > 100 and self.humidity > 160:
            self.grid_type = _dirty_water

        elif self.altitude > 175 and self.temperature < 175:
            self.grid_type = _snow

        elif 175 >= self.altitude >= 150:
            self.grid_type = _rock

        elif 140 > self.altitude > 130 and self.humidity < 130 and self.temperature > 100:
            self.grid_type = _plains

        elif 150 > self.altitude > 145 and self.temperature < 100:
            self.grid_type = _tundra

        elif 145 >= self.altitude >= 140 and self.temperature < 105:
            self.grid_type = _boreal

        elif self.altitude > 120:
            self.grid_type = _grass

        
        for x in range(self.width):
            for y in range(self.height):
                scan_x = x + self.x_position
                scan_y = y + self.y_position
                image.putpixel((scan_x, scan_y), self.grid_type)


def main():
    grid = Grid(0, 0, 0, 0)


if __name__ == '__main__':
    main()