Skip to content
Snippets Groups Projects
Commit ddf2f00a authored by Daniel Moix's avatar Daniel Moix
Browse files

Adding Lecture 5

parent e072f151
Branches
No related tags found
No related merge requests found
# Add up the odd integers entered by the user
entry = 0
total = 0
while entry >= 0:
entry = int(input("Enter a positive integer. Negative quits\n"))
if entry > 0 and entry % 2 == 1:
total = total + entry
print("The total of the odd numbers entered is ", total)
\ No newline at end of file
for n in range(10, 1, -1):
print(n, end=' ')
print()
message = "I love donuts"
for letter in message:
if letter.lower() in ('a', 'e', 'i', 'o', 'u'):
print(letter)
def check(secret, guess):
if len(secret) != len(guess):
return "ERROR: Strings must be of equal length."
emptyString = ''
for i in range(0, len(secret)):
if secret[i] == guess[i]:
emptyString += secret[i]
else:
emptyString += '-'
#
# while secret != output
#
# for elements in guess:
# secret == guess
return emptyString
print(check("CAT", "HAT"))
\ No newline at end of file
lecture = 5
while lecture <= 10:
# print(f"Hello Lecture {lecture}!")
lecture = lecture - 1
print("I am done. Finished at lecture", lecture)
#
# if lecture <= 10:
# print(f"Hello Lecture {lecture}!")
# lecture = lecture + 1
\ No newline at end of file
stuff = set()
stuff.add("Eggs")
stuff.add("Milk")
stuff.add("Cadillac")
print(stuff)
for item in stuff:
print(item)
\ No newline at end of file
for i in range(1, 6):
for j in range (0, i):
print('*', end=' ')
print()
\ No newline at end of file
from drawable import Drawable
import pygame
class Bubble(Drawable):
def __init__(self, x=0, y=0, color=(0, 0, 0), size=100):
super().__init__(x,y)
self.__color = color
self.__size = size
def draw(self, surface):
pygame.draw.circle(surface, self.__color, self.getLocation(), self.__size)
def move(self):
x, y = self.getLocation()
y = y + 1
self.setLocation((x, y))
def get_rect(self):
pass
\ No newline at end of file
import pygame
import random
class Bubble:
def __init__(self, pos, size=255, color=(0, 0, 0)):
self.__pos = pos
self.__size = size
self.__color = color
self.__dropSpeed = 5
def bounce(self):
self.__dropSpeed = -12
def draw(self, surface):
red, green, blue = self.__color
redStep = (255 - red) / self.__size
greenStep = (255 - green) / self.__size
blueStep = (255 - blue) / self.__size
currentSize = self.__size
while currentSize > 0:
pygame.draw.circle(surface, (red, green, blue), self.__pos, currentSize)
currentSize -= 1
red = redStep
green += greenStep
blue += blueStep
x, y = self.__pos
y += self.__dropSpeed
self.__pos = (x, y)
self.__dropSpeed += .8
if __name__ == "__main__":
pygame.init()
surface = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
bubble = Bubble((400, 300), 100, (0, 128, 0))
while True:
surface.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT or \
event.type == pygame.KEYDOWN and \
event.__dict__['key'] == pygame.K_q:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and \
event.__dict__['key'] == pygame.K_SPACE:
bubble.bounce()
bubble.draw(surface)
pygame.display.update()
clock.tick(30)
import pygame
import random
class Bubble:
def __init__(self, pos, size=255, color=(0, 0, 0)):
self.__pos = pos
self.__size = size
self.__color = color
def draw(self, surface):
red, green, blue = self.__color
redStep = (255 - red) / self.__size
greenStep = (255 - green) / self.__size
blueStep = (255 - blue) / self.__size
currentSize = self.__size
while currentSize > 0:
pygame.draw.circle(surface, (red, green, blue), self.__pos, currentSize)
currentSize -= 1
red = redStep
green += greenStep
blue += blueStep
if __name__ == "__main__":
pygame.init()
surface = pygame.display.set_mode((800, 600))
surface.fill((255, 255, 255))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or \
event.type == pygame.KEYDOWN and \
event.__dict__['key'] == pygame.K_q:
pygame.quit()
exit()
nextBubble = Bubble((random.randint(0, 800), random.randint(0, 600)), random.randint(10, 100), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
nextBubble.draw(surface)
pygame.display.update()
clock.tick(5)
import pygame
# Define some colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
# Define some game variables
width = 800
height = 600
# Load some Dragons
blueDragon = pygame.image.load("mario_blue.png")
goldDragon = pygame.image.load("mario_gold.png")
currentDragon = blueDragon
pygame.init()
surface = pygame.display.set_mode((width, height))
surface.fill((255, 255, 255))
while True:
for event in pygame.event.get():
if ((event.type == pygame.QUIT) or \
(event.type == pygame.KEYDOWN and \
event.__dict__['key'] == pygame.K_q)):
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
print("Click at", event.pos)
clickX, clickY = event.pos
dragonWidth = currentDragon.get_width()
dragonHeight = currentDragon.get_height()
x = clickX - dragonWidth / 2
y = clickY - dragonHeight / 2
surface.blit(currentDragon, (x, y))
if currentDragon == blueDragon:
currentDragon = goldDragon
else:
currentDragon = blueDragon
elif event.type == pygame.KEYDOWN and \
event.__dict__['key'] == pygame.K_SPACE:
surface.fill(white)
pygame.display.update()
\ No newline at end of file
from abc import ABC, abstractmethod
class Drawable(ABC):
def __init__(self, x=0, y=0):
self.__x = x
self.__y = y
def getLocation(self):
return (self.__x, self.__y)
def setLocation(self, point):
self.__x = point[0]
self.__y = point[1]
@abstractmethod
def draw(self, surface):
pass
@abstractmethod
def get_rect(self):
pass
\ No newline at end of file
import pygame
from drawable import Drawable
from bubble import Bubble
import random
width = 800
height = 600
pygame.init()
surface = pygame.display.set_mode((width, height))
surface.fill((255, 255, 255))
pygame.display.set_caption("Hello CS 172")
fpsClock = pygame.time.Clock()
bubbles = []
for _ in range(0, 100):
bubbles.append(Bubble(random.randint(0,width), random.randint(-1000,height), (random.randint(0,255), random.randint(0,255), random.randint(0,255)), random.randint(20,150)))
while True:
surface.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT or \
(event.type == pygame.KEYDOWN and event.__dict__['key'] == pygame.K_q):
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
print(event.pos)
#pygame.draw.line(surface, (0, 128, 0), (0, 0), (800, 600), 5)
#pygame.draw.circle(surface, (128, 0, 0), (width/2, height/2), 150)
for bubble in bubbles:
bubble.draw(surface)
bubble.move()
pygame.display.update()
fpsClock.tick(30)
\ No newline at end of file
import pygame
pygame.init()
width = 800
height = 600
radius = 200
surface = pygame.display.set_mode((width, height))
white = (255, 255, 255)
red = (200, 0, 0)
blue = (0, 0, 200)
targetColor = white
targetPos = (width/2, height/2)
surface.fill(white)
def drawTarget(surface, pos, size, targetColor=(255,255,255)):
while size > 0:
pygame.draw.circle(surface, targetColor, targetPos, size)
size -= 25
if targetColor == blue:
targetColor = white
elif targetColor == white:
targetColor = red
else:
targetColor = blue
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or \
event.type == pygame.KEYDOWN and \
event.__dict__['key'] == pygame.K_q:
exit()
if event.type == pygame.MOUSEMOTION:
surface.fill(white)
targetColor = white
targetPos = event.pos
drawTarget(surface, targetPos, radius, targetColor)
pygame.display.update()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment