Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions chapter16/author.txt

This file was deleted.

2 changes: 0 additions & 2 deletions chapter16/ch.txt

This file was deleted.

137 changes: 72 additions & 65 deletions chapter16/collision1.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,87 @@
#!/usr/bin/python
# collision.py
# Chapter 16 Game Programming
# Author: William C. Gunnells
# Rapid Python Programming
"""
collision.py
Chapter 16 Game Programming
Author: William C. Gunnells
Rapid Python Programming
"""


# libs
# lib
import pygame
import random


white=(255,255,255) # RGB values
black=(0,0,0)
green=(0,255,0)
red=(255,0,0)
yellow=(255,255,0)
blue=(0,0,255)
pygame.init()
display=pygame.display.set_mode((800,600))
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
yellow = (255, 255, 0)
blue = (0, 0, 255)
pygame.init()
display = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
font=pygame.font.SysFont(None,25)
bg=pygame.image.load("gimpbackground.png")
font=pygame.font.SysFont(None, 25)
bg = pygame.image.load("gimpbackground.png")


star = pygame.image.load('assortstar1.png')


star=pygame.image.load('assortstar1.png')
def message(msg, color, posx, posy):
mytext = font.render(msg, True, color)
display.blit(mytext, [posx, posy])

def message(msg,color,posx,posy):
mytext=font.render(msg,True,color)
display.blit(mytext,[posx,posy])

def game():
exit=False
x=300; y=300
xChange=0; yChange=0
boxX=round(random.randrange(0,800-10) /10.0) *10.0
boxY=round(random.randrange(0,600-10) /10.0) *10.0
while not exit: # exit loop
for event in pygame.event.get(): # event handling
if event.type==pygame.QUIT:
exit=True
k=pygame.key.get_pressed()
if k[pygame.K_LEFT]:
xChange-=10
elif k[pygame.K_RIGHT]:
xChange+=10
elif k[pygame.K_UP]:
yChange-=10
elif k[pygame.K_DOWN]:
yChange+=10
else:
xChange=0
yChange=0
if x==700: # borders
xChange=-10; yChange=0
if x==10:
xChange=10; yChange=0
if y==500:
yChange=-10; xChange=0
if y==10:
exit=True
x += xChange # continue from previous location
y += yChange
def game():
exit = False
x = 300
y = 300
xChange = 0
yChange = 0
boxX = round(random.randrange(0, 800-10) / 10.0) * 10.0
boxY = round(random.randrange(0, 600-10) / 10.0) * 10.0
while not exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
k = pygame.key.get_pressed()
if k[pygame.K_LEFT]:
xChange -= 10
elif k[pygame.K_RIGHT]:
xChange += 10
elif k[pygame.K_UP]:
yChange -= 10
elif k[pygame.K_DOWN]:
yChange += 10
else:
xChange = 0
yChange = 0
if x == 700: # borders
xChange = -10
yChange = 0
if x == 10:
xChange = 10
yChange = 0
if y == 500:
yChange = -10
xChange = 0
if y == 10:
exit = True
x += xChange
y += yChange

display.blit(bg,(0,0))
message("Move square in any direction...", yellow, 300,50)
pygame.draw.rect(display,yellow,[boxX,boxY,40,40])
display.blit(star,[x,y])
pygame.display.update()
display.blit(bg, (0, 0))
message("Move square in any direction...", yellow, 300, 50)
pygame.draw.rect(display, yellow, [boxX, boxY, 40, 40])
display.blit(star, [x, y])
pygame.display.update()

if x== boxX and y==boxY: #collision
print("Boom!!!")
boxX=round(random.randrange(0,800-10) /10.0) *10.0
boxY=round(random.randrange(0,600-10) /10.0) *10.0
clock.tick(10)
pygame.quit()
quit()
if x == boxX and y == boxY: # collision
print("Boom!!!")
boxX = round(random.randrange(0, 800-10) / 10.0) * 10.0
boxY = round(random.randrange(0, 600-10) / 10.0) * 10.0
clock.tick(10)
pygame.quit()
quit()

game()

23 changes: 12 additions & 11 deletions chapter16/display1.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#!/usr/bin/python
#display1.py
# Chapter 16 Game Programming
# Author: William C. Gunnells
# Rapid Python Programming
"""
display1.py
Chapter 16 Game Programming
Author: William C. Gunnells
Rapid Python Programming
"""

# libs
# lib
import pygame


pygame.init()
mydisplay=pygame.display.set_mode((800,600))
print pygame.display.set_mode((800,600))
mode=help('pygame.display.set_mode')
my_display = pygame.display.set_mode((800,600))
print(pygame.display.set_mode((800,600)))
mode = help('pygame.display.set_mode')
display=help('pygame.display')
print mode
print display
print(mode)
print(display)
191 changes: 99 additions & 92 deletions chapter16/gameloop1.py
Original file line number Diff line number Diff line change
@@ -1,107 +1,114 @@
#!/usr/bin/python
# gameloop1.py
# Chapter 16 Game Programming
# Author: William C. Gunnells
# Rapid Python Programming
"""
gameloop1.py
Chapter 16 Game Programming
Author: William C. Gunnells
Rapid Python Programming
"""

# libs

# lib
import pygame


white=(255,255,255) # RGB values
black=(0,0,0)
green=(0,255,0)
red=(255,0,0)
yellow=(255,255,0)
blue=(0,0,255)
pygame.init()
display=pygame.display.set_mode((800,600))
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 255, 0)
red = (255, 0, 0)
yellow = (255, 255, 0)
blue = (0, 0, 255)
pygame.init()
display=pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
font=pygame.font.SysFont(None,25)


def splash():
intro=True
while intro:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type==pygame.KEYDOWN:
if event.key == pygame.K_c:
intro=False
if event.key==pygame.K_q:
pygame.quit()
quit()
display.fill(white)
message("Wellcome!!!", black,300,300 )
message("Press 'c' to continue or 'q' to Quit",black,300,400)
pygame.display.update()
clock.tick(15)
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_c:
intro = False
if event.key == pygame.K_q:
pygame.quit()
quit()
display.fill(white)
message("Welcome!!!", black, 300, 300)
message("Press 'c' to continue or 'q' to Quit", black, 300, 400)
pygame.display.update()
clock.tick(15)


def message(msg,color,posx,posy):
mytext = font.render(msg,True,color)
display.blit(mytext,[posx,posy])

def message(msg,color,posx,posy):
mytext=font.render(msg,True,color)
display.blit(mytext,[posx,posy])

def game():
exit=False
over=False
x=300; y=300
xChange=0; yChange=0
while not exit: # exit loop
while over == True: # over loop
display.fill(blue)
message("Game over 'c' to continue 'q' to quit", yellow, 300,300)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.QUIT:
over=False
exit=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
exit = True
over = False
if event.key == pygame.K_c:
game()
for event in pygame.event.get(): # event handling
if event.type==pygame.QUIT:
exit=True
k=pygame.key.get_pressed()
if k[pygame.K_LEFT]:
xChange-=10
elif k[pygame.K_RIGHT]:
xChange+=10
elif k[pygame.K_UP]:
yChange-=10
elif k[pygame.K_DOWN]:
yChange+=10
else:
xChange=0
yChange=0

if x==700: # borders
xChange=-10
yChange=0
if x==10:
xChange=10
yChange=0
if y==500:
yChange=-10
xChange=0
if y==10:
over=True
def game():
exit = False
over = False
x = 300
y = 300
xChange = 0
yChange = 0
while not exit:
while over:
display.fill(blue)
message("Game over 'c' to continue 'q' to quit", yellow, 300,300)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
over = False
exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
exit = True
over = False
if event.key == pygame.K_c:
game()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit = True
k = pygame.key.get_pressed()
if k[pygame.K_LEFT]:
xChange -= 10
elif k[pygame.K_RIGHT]:
xChange += 10
elif k[pygame.K_UP]:
yChange -= 10
elif k[pygame.K_DOWN]:
yChange += 10
else:
xChange = 0
yChange = 0

x += xChange # continue from previous location
y += yChange
if x == 700: # borders
xChange =- 10
yChange = 0
if x == 10:
xChange = 10
yChange = 0
if y == 500:
yChange =- 10
xChange = 0
if y == 10:
over = True

x += xChange # continue from previous loc.
y += yChange

display.fill(black)
message("Move square in any direction...", yellow, 300, 50)
message("Too far up exits loop...", yellow, 300, 70)
pygame.draw.rect(display,white,[x,y,20,20])
pygame.display.update()
clock.tick(10)
pygame.quit()
quit()

display.fill(black) # not rendered without update
message("Move square in any direction...", yellow, 300,50)
message("Too far up exits loop...", yellow, 300,70)
pygame.draw.rect(display,white,[x,y,20,20]) # coords, w,h
pygame.display.update()
clock.tick(10)
pygame.quit()
quit()

splash()
game()

Loading