View on GitHub

Making a Game of IT

Michigan State University - Summer 2019

Student-submitted Example Programs

painter.py

'''
This game acts like a painter tool. The first line changes color. The second line changes the background.
'''

import sys
import random
import pygame

# Initialize pygame
pygame.init()

# Some constants
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
FPS = 60

LIGHT_GREY = [230, 230, 230]

# Create the display surface
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("Collider Demo")

# Make a clock to manage frames per second
clock = pygame.time.Clock()

mouse_rect = pygame.Rect([0,0], [30, 30])
wall_rect = pygame.Rect([400, 400], [30, 600])
sec_wall_rect = pygame.Rect([700,200], [30, 900])
# Game loop
mouse_rect_color = [0,0,0]

already_changed_color = False
screen.fill(LIGHT_GREY)
while True:
    # Set the background color
    
    
    mouse_position = pygame.mouse.get_pos()
    mouse_rect.centerx = mouse_position[0]
    mouse_rect.centery = mouse_position[1]
    
    if mouse_rect.colliderect(wall_rect):
        R = random.randint(0,255)
        G = random.randint(0, 255)
        B = random.randint(0, 255)
        
        mouse_rect_color=[R, G, B]
    if mouse_rect.colliderect(sec_wall_rect):
        if not already_changed_color:
            R = random.randint(0,255)
            G = random.randint(0, 255)
            B = random.randint(0, 255)
            screen.fill([R, G, B])
            already_changed_color = True
    else:
        already_changed_color = False

    pygame.draw.rect(screen, [0,0,0], wall_rect)
    pygame.draw.rect(screen, mouse_rect_color, mouse_rect)
    pygame.draw.rect(screen, [0,0,0], sec_wall_rect)

    # Event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update the display, tick clock
    pygame.display.update()
    clock.tick(FPS)

# CHALLENGE: Add a few more obstacles to the scene and test for collisions between
#            the mouse and those obstacles
#             - bonus: trigger different reactions depending on which obstacle you
#                       hit with the mouse-following rectangle

|>> download student-examples/painter.py

colorpicker.py

'''
Hover your mouse over one of the 6 squares to adjust the background color.
'''

import sys
import random
import pygame

# Initialize pygame
pygame.init()

# Some constants
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
FPS = 60

LIGHT_GREY = [230, 230, 230]

# Create the display surface
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("Collider Demo")

# Make a clock to manage frames per second
clock = pygame.time.Clock()

mouse_rect = pygame.Rect([0, 0], [30, 30])
whitered_wall_rect = pygame.Rect([250, 200], [100, 100])
whitegreen_wall_rect = pygame.Rect([550, 200], [100, 100])
whiteblue_wall_rect = pygame.Rect([850, 200], [100, 100])
blackred_wall_rect = pygame.Rect([250, 350], [100, 100])
blackgreen_wall_rect = pygame.Rect([550, 350], [100, 100])
blackblue_wall_rect = pygame.Rect([850, 350], [100, 100])

# Game loop
w_color = [0, 0, 0]
while True:
    # Set the background color
    screen.fill(w_color)

    mouse_position = pygame.mouse.get_pos()
    mouse_rect.centerx = mouse_position[0]
    mouse_rect.centery = mouse_position[1]

    if mouse_rect.colliderect(whitered_wall_rect):
        if w_color[0] <= 254:
            w_color[0] = (w_color[0] + 1)
    if mouse_rect.colliderect(whitegreen_wall_rect):
        if w_color[1] <= 254:
            w_color[1] = (w_color[1] + 1)
    if mouse_rect.colliderect(whiteblue_wall_rect):
        if w_color[2] <= 254:
            w_color[2] = (w_color[2] + 1)
    if mouse_rect.colliderect(blackred_wall_rect):
        if w_color[0] >= 1:
            w_color[0] = (w_color[0] - 1)
    if mouse_rect.colliderect(blackgreen_wall_rect):
        if w_color[1] >= 1:
            w_color[1] = (w_color[1] - 1)
    if mouse_rect.colliderect(blackblue_wall_rect):
        if w_color[2] >= 1:
            w_color[2] = (w_color[2] - 1)


    pygame.draw.rect(screen, [255, 100, 100], whitered_wall_rect)
    pygame.draw.rect(screen, [100, 255, 100], whitegreen_wall_rect)
    pygame.draw.rect(screen, [100, 100, 255], whiteblue_wall_rect)
    pygame.draw.rect(screen, [155, 0, 0], blackred_wall_rect)
    pygame.draw.rect(screen, [0, 155, 0], blackgreen_wall_rect)
    pygame.draw.rect(screen, [0, 0, 155], blackblue_wall_rect)
    pygame.draw.rect(screen, [0, 0, 0], mouse_rect)

    # Event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Update the display, tick clock
    pygame.display.update()
    clock.tick(FPS)

|>> download student-examples/colorpicker.py

enemies_following.py

'''
Enemies (red rectangles) follow the character (green rectangle) around. Move character
with WASD.
'''

import pygame
import random
import sys

char_start_x = 450
char_start_y = 450
char_x_val = 450    #this is the x cordinate value
char_y_val = 450    #this is the y cordinate value
char_width = 30
char_height = 30
char_speed = 5      # How fast does character move?
cur_char_x_vel = 0  # What's the character's current x speed?
cur_char_y_vel = 0  # What's the character's current y speed?

FPS = 60

LIGHT_GREY = [230, 230, 230]
GREEN = [0,255,0]
RED = [255,0,0]

screen_width = 900
screen_height = 900

health = 500

character = pygame.Rect([char_x_val, char_y_val], [char_width, char_height])
screen = pygame.display.set_mode([screen_width, screen_height])
safe_space = pygame.Rect(250,250,450,450)

def get_enemy_rect():
    while True:
        enemy_width = 25
        enemy_height = 25
        enemy_x_val = random.randint(0,900)
        enemy_y_val = random.randint(0,900)

        enemy_rect = pygame.Rect([enemy_x_val,enemy_y_val],[enemy_width,enemy_height])
        if not enemy_rect.colliderect(safe_space):
            return enemy_rect

enemies = []
amount_enemies = 3
place_holder_enemies = 3

clock = pygame.time.Clock()

for i in range(amount_enemies):
    enemies.append(get_enemy_rect())

while True:
    screen.fill(LIGHT_GREY)

    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[pygame.K_w]:
        #print("up arrow")
        cur_char_y_vel = -1*char_speed
        char_y_val -= 5
        char_start_y -= 5
    elif pressed_keys[pygame.K_s]:
        #print("down arrow")
        cur_char_y_vel = char_speed
        char_y_val += 5
        char_start_y += 5
    else:
        cur_char_y_vel = 0
    # - Is player moving horizontally?
    if pressed_keys[pygame.K_d]:
        #print("down arrow")
        cur_char_x_vel = char_speed
        char_x_val += 5
        char_start_x += 5
    elif pressed_keys[pygame.K_a]:
        #print("down arrow")
        cur_char_x_vel = -1*char_speed
        char_x_val -= 5
        char_start_x -= 5
    else:
        cur_char_x_vel = 0

    # Draw the character!
    character.left += cur_char_x_vel
    character.top += cur_char_y_vel
    pygame.draw.rect(screen, GREEN, character)

    # Draw enemies
    for enemy in enemies:
        pygame.draw.rect(screen, RED, enemy)

    # Are any enemies colliding with the character?
    for enemy in enemies:
        if character.colliderect(enemy):
            health = health - 1

    # Have the enemies move toward the character
    for enemy in enemies:
        if char_x_val > enemy.left:
            enemy.left += 2
        if char_x_val < enemy.left:
            enemy.left -= 2
        if char_y_val > enemy.top:
            enemy.top += 2
        if char_y_val < enemy.top:
            enemy.top -= 2

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.update()
    clock.tick(FPS)

|>> download student-examples/enemies_following.py

opengl_demo.py

'''
3D asteroid shooter w/OpenGL
'''
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from random import *
import numpy
import math


class Enemy1:
    vertices = (
        (0, 0, 1),
        (0, 0, -1),
        (0, 1, 0),
        (0, -1, 0),
        (1, 0, 0),
        (-1, 0, 0),

    )

    enemylist = []

    edges = (
        (0, 2),
        (0, 3),
        (0, 4),
        (0, 5),
        (1, 2),
        (1, 3),
        (1, 4),
        (1, 5),
        (2, 4),
        (2, 5),
        (3, 4),
        (3, 5),
    )

    surfaces = (
        (0, 2, 4),
        (0, 3, 4),
        (0, 2, 5),
        (0, 3, 5),
        (1, 2, 4),
        (1, 3, 4),
        (1, 2, 5),
        (1, 3, 5),
    )

    def __init__(self):
        self.edges = Enemy1.edges
        self.vertices = Enemy1.vertices
        self.enemylist = Enemy1.enemylist
        self.surfaces = Enemy1.surfaces

    def draw_sides(self, enemyno):
        tempb = tuple(self.enemylist[enemyno])
        glLineWidth(10)
        glBegin(GL_TRIANGLES)
        for surface in self.surfaces:
            for vertex in surface:
                glColor3f(0.5, 0.5, 0.5)
                glVertex3fv(tempb[vertex])
        glEnd()
        del tempb

    def draw(self, enemyno):
        self.draw_sides(enemyno)
        tempb = tuple(self.enemylist[enemyno])
        glLineWidth(5)
        glBegin(GL_LINES)
        for edge in self.edges:
            for vertex in edge:
                glColor3f(0.3, 0.3, 0.3)
                glVertex3fv(tempb[vertex])
        glEnd()
        del tempb

    def setvertices(self, x, y, z):
        newvertices = []
        for vert in self.vertices:
            newvert = []
            newx = vert[0] + x
            newy = vert[1] + y
            newz = vert[2] + z
            newvert.extend([newx, newy, newz])
            newvertices.append(newvert)
        self.enemylist.append(newvertices)

    def delete(self, enemyno):
        self.enemylist.pop(enemyno)

    def move(self, x, y, z):
        for i in range(len(self.enemylist)):
            self.enemylist[i] = list(
                map(lambda vert: (vert[0] + x, vert[1] + y, vert[2] + z), self.enemylist[i]))


class Enemy2:
    vertices = (
        (0, 1, 0),
        (0, 0.25, -1),
        (math.sqrt(3)/2, 0.25, 0.5),
        (math.sqrt(3)/-2, 0.25, 0.5),
        (0, -0.25, 1),
        (math.sqrt(3)/2, -0.25, -0.5),
        (math.sqrt(3)/-2, -0.25, -0.5),
        (0, -1, 0),
    )

    enemylist = []

    edges = (
        (0, 1),
        (0, 2),
        (0, 3),
        (7, 4),
        (7, 5),
        (7, 6),
        (1, 5),
        (1, 6),
        (2, 4),
        (2, 5),
        (3, 4),
        (3, 6),
    )

    surfaces = (
        (0, 1, 5, 2),
        (0, 1, 6, 3),
        (0, 3, 4, 2),
        (7, 5, 2, 4),
        (7, 4, 3, 6),
        (7, 6, 1, 5),
    )

    def __init__(self):
        self.edges = Enemy2.edges
        self.vertices = Enemy2.vertices
        self.enemylist = Enemy2.enemylist
        self.surfaces = Enemy2.surfaces

    def draw_sides(self, enemyno):
        tempb = tuple(self.enemylist[enemyno])
        glLineWidth(5)
        glBegin(GL_QUADS)
        for surface in self.surfaces:
            for vertex in surface:
                glColor3f(0.5, 0.5, 0.5)
                glVertex3fv(tempb[vertex])
        glEnd()
        del tempb

    def draw(self, enemyno):
        self.draw_sides(enemyno)
        tempb = tuple(self.enemylist[enemyno])
        glLineWidth(5)
        glBegin(GL_LINES)
        for edge in self.edges:
            for vertex in edge:
                glColor3f(0.3, 0.3, 0.3)
                glVertex3fv(tempb[vertex])
        glEnd()
        del tempb

    def setvertices(self, x, y, z):
        newvertices = []
        for vert in self.vertices:
            newvert = []
            newx = vert[0] + x
            newy = vert[1] + y
            newz = vert[2] + z
            newvert.extend([newx, newy, newz])
            newvertices.append(newvert)
        self.enemylist.append(newvertices)

    def delete(self, enemyno):
        self.enemylist.pop(enemyno)

    def move(self, x, y, z):
        for i in range(len(self.enemylist)):
            self.enemylist[i] = list(
                map(lambda vert: (vert[0] + x, vert[1] + y, vert[2] + z), self.enemylist[i]))


class Player:
    vertices = (
        (0, 0, 2),
        (0, 3, 0),
        (1, -1, 0),
        (-1, -1, 0),
        (0, -2, 0),
        (0, 0, -1),

        (2, 1, 0),
        (2, -2, 1),
        (2, -2, -1),

        (-2, 1, 0),
        (-2, -2, 1),
        (-2, -2, -1),
    )

    edges = (
        (0, 1),
        (0, 2),
        (0, 3),
        (0, 4),
        (5, 1),
        (5, 2),
        (5, 3),
        (5, 4),
        (1, 2),
        (1, 3),
        (4, 2),
        (4, 3),
        (2, 6),
        (2, 7),
        (2, 8),
        (6, 7),
        (6, 8),
        (7, 8),
        (3, 9),
        (3, 10),
        (3, 11),
        (9, 10),
        (9, 11),
        (10, 11)
    )

    surfaces = (
        (0, 1, 2),
        (0, 1, 3),
        (0, 4, 2),
        (0, 4, 3),
        (5, 1, 2),
        (5, 1, 3),
        (5, 4, 2),
        (5, 4, 3),
        (2, 6, 7),
        (2, 6, 8),
        (2, 7, 8),
        (3, 9, 10),
        (3, 9, 11),
        (3, 10, 11)
    )

    def __init__(self):
        self.edges = Player.edges
        self.vertices = Player.vertices
        self.surfaces = Player.surfaces

    def draw(self, x, y, z):
        self.move(x, y, z)
        self.draw_sides()
        glLineWidth(10)
        glBegin(GL_LINES)
        for edge in self.edges:
            for vertex in edge:
                glColor3f(randint(0, 1), randint(0, 1), randint(0, 1))
                glVertex3fv(self.vertices[vertex])
        glEnd()

    def draw_sides(self):
        glLineWidth(10)
        glBegin(GL_TRIANGLES)
        for surface in self.surfaces:
            for vertex in surface:
                glColor3f(0, 0, 0)
                glVertex3fv(self.vertices[vertex])
        glEnd()

    def move(self, x, y, z):
        self.vertices = list(
            map(lambda vert: (vert[0] + x, vert[1] + y, vert[2] + z), self.vertices))


class Bullet:
    vertices = (
        (0, 0.5, 0),
        (0.2, -0.5, 0),
        (-0.2, -0.5, 0),
        (0, -0.5, 0.2),
        (0, -0.5, -0.2),
    )

    edges = (
        (0, 1),
        (0, 2),
        (0, 3),
        (0, 4),
    )

    bulletlist = []

    def __init__(self):
        self.edges = Bullet.edges
        self.vertices = Bullet.vertices
        self.bulletlist = Bullet.bulletlist

    def draw(self, bulletno):
        tempb = tuple(self.bulletlist[bulletno])
        glLineWidth(5)
        glBegin(GL_LINES)
        for edge in self.edges:
            for vertex in edge:
                glColor3f(1, 1, 1)
                glVertex3fv(tempb[vertex])
        glEnd()
        del tempb

    def setvertices(self, x, z):
        newvertices = []
        for vert in self.vertices:
            newvert = []
            newx = vert[0] + x
            newz = vert[2] + z
            newvert.extend([newx, vert[1], newz])
            newvertices.append(newvert)
        self.bulletlist.append(newvertices)

    def delete(self):
        self.bulletlist.pop(0)

    def move(self, speed):
        for bullet in range(len(self.bulletlist)):
            self.bulletlist[bullet] = list(map(lambda b: (
                b[0], b[1] + speed, b[2]), self.bulletlist[bullet]))
            bullet += 1


def render():
    pygame.init()
    display = (1400, 780)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    pygame.display.set_caption("Space Video Game", "Space Video Game")
    gluPerspective(60, (display[0]/display[1]), 0.1, 50)

    glTranslatef(0, 0, -15)
    glRotatef(-90, 1, 0, 0)
    glEnable(GL_DEPTH_TEST)

    def spawnenemy(depth):
        if randint(0, 10) >= 5:
            e2.setvertices(randrange(-10, 10, 1),
                           depth, randrange(-6, 6, 1))
        else:
            e1.setvertices(randrange(-10, 10, 1),
                           depth, randrange(-6, 6, 1))

    gamespeed = 0.1
    b1 = False
    b2 = False
    b3 = False
    ppos = [0, 0]
    bstatus = [False, False, 1]
    p = Player()
    e1 = Enemy1()
    e2 = Enemy2()
    b = Bullet()
    o = True
    vel = 0.25
    clock = pygame.time.Clock()
    while True:
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        clock.tick(60)
        currenttime = pygame.time.get_ticks()

        if currenttime > 7500:
            gamespeed = 0.15
            if currenttime > 12500:
                gamespeed = 0.2
                if currenttime > 20000:
                    gamespeed = 0.25

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if bstatus[0] == False:
                        bstatus[2] = currenttime
                        bstatus[0] = True
                        b1 = True
                        b2 = True
                        b3 = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    bstatus[0] = False
                    for i in range(len(b.bulletlist)):
                        b.delete()
                        i += 1

        keys = pygame.key.get_pressed()
        if ppos[0] >= -10.5:
            if keys[pygame.K_a]:
                ppos[0] -= vel
                p.draw(-vel, 0, 0)
        if ppos[0] <= 10.5:
            if keys[pygame.K_d]:
                ppos[0] += vel
                p.draw(vel, 0, 0)
        if ppos[1] <= 6.25:
            if keys[pygame.K_w]:
                ppos[1] += vel
                p.draw(0, 0, vel)
        if ppos[1] >= -6.25:
            if keys[pygame.K_s]:
                ppos[1] -= vel
                p.draw(0, 0, -vel)

        if bstatus[0] == True:
            if bstatus[2] <= currenttime:
                if b1 == True:
                    b.setvertices(ppos[0], ppos[1])
                    b1 = False
                b.draw(len(b.bulletlist) - 1)
            if (bstatus[2] + 1000/3) <= currenttime:
                if b2 == True:
                    b.setvertices(ppos[0], ppos[1])
                    b2 = False
                b.draw(len(b.bulletlist) - 2)
            if (bstatus[2] + 2000/3) <= currenttime:
                if b3 == True:
                    b.setvertices(ppos[0], ppos[1])
                    b3 = False
                b.draw(len(b.bulletlist) - 3)
            if bstatus[2] + 1000 <= currenttime:
                bstatus[2] = currenttime
                b1 = True
                b2 = True
                b3 = True

        if currenttime % (100/gamespeed) >= 80/gamespeed and o == True:
            spawnenemy(20)
            o = False
        if currenttime % (100/gamespeed) <= 20 and o == False:
            o = True

        if int(len(e1.enemylist)) > 0:
            for i in range(len(e1.enemylist)):
                e1.draw(i - 1)
                i += 1
        if int(len(e2.enemylist)) > 0:
            for j in range(len(e2.enemylist)):
                e2.draw(j - 1)
                j += 1

        for bullet in b.bulletlist:
            pos_btip = [bullet[0][0] + 1, bullet[0]
                        [1] + 1, bullet[0][2] + 1]
            neg_btip = [bullet[0][0] - 1, bullet[0]
                        [1] - 1, bullet[0][2] - 1]
            for enemy in e1.enemylist:
                enemypt = enemy[1]
                if (neg_btip[0] < enemypt[0] < pos_btip[0] and neg_btip[1] < enemypt[1] < pos_btip[1] and neg_btip[2] < enemypt[2] < pos_btip[2]) == True:
                    e1.delete(e1.enemylist.index(enemy))
            for enemy in e2.enemylist:
                enemypt = enemy[1]
                if (neg_btip[0] < enemypt[0] < pos_btip[0] and neg_btip[1] < enemypt[1] < pos_btip[1] and neg_btip[2] < enemypt[2] < pos_btip[2]) == True:
                    e2.delete(e2.enemylist.index(enemy))

        for enemy in e1.enemylist:
            enemypt = enemy[1]
            if enemy[1][1] < -15:
                e1.delete(e1.enemylist.index(enemy))
        for enemy in e2.enemylist:
            enemypt = enemy[1]
            if enemy[1][1] < -15:
                e2.delete(e2.enemylist.index(enemy))

        e1.move(0, -gamespeed, 0)
        e2.move(0, -gamespeed, 0)
        b.move(1)
        p.draw(0, 0, 0)
        pygame.display.flip()

render()

|>> download student-examples/opengl_demo.py