Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored code, added new Button class, reduced rerenders #25

Merged
merged 42 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
5becdbe
Created class Button to avoid hardcoding button functionalities in ot…
jriyyya Mar 29, 2023
d189280
Refactored code to employ D.R.Y principles and reduce CPU consumption
jriyyya Mar 29, 2023
ad46629
added a line to start the game as included in the new main.py file
jriyyya Mar 29, 2023
4d6dc05
Refactored: Employed new Button class and made seperate functions for…
jriyyya Mar 29, 2023
01be221
autopep8 was run
jriyyya Mar 30, 2023
17bdd8b
removed whitespaces at the end of the files
jriyyya Mar 31, 2023
30da576
Made button act when released instead of when pressed
jriyyya Apr 5, 2023
2e51d35
Fixed typo - changed 'lack' to 'black'
jriyyya Apr 5, 2023
31af7c4
added not None check to score to avoid None type comparison
jriyyya Apr 5, 2023
1f432cf
Add .flake8 file and fix flake8 warnings in main.py
jriyyya Apr 5, 2023
dbef2b8
Fix flake8 warnings in scorescreen.py
jriyyya Apr 5, 2023
33fd5c7
Remove gi.require_version wherever it was unnecessary
jriyyya Apr 5, 2023
15fcc1f
renamed variables
jriyyya Apr 10, 2023
1d2f127
used update_highscore function instead of hardcoded method
jriyyya Apr 10, 2023
861d2c9
Fix flake8 warnings in button.py
jriyyya Apr 22, 2023
9a31892
Remove import not required
quozl May 29, 2023
825d3bd
Deepen sugar3.activity.activity import
quozl May 29, 2023
926b14e
Run without Sugar
quozl May 30, 2023
791fa67
Use running not crashed
quozl May 30, 2023
49856c0
Change import order
quozl May 30, 2023
a862cf1
Remove unused import
quozl May 30, 2023
bc1a550
Strictly follow the Sugargame TestActivity template
quozl May 30, 2023
325a137
Correctly handle all Pygame events rather than just one
quozl May 30, 2023
f06c7b0
Slow the main menu update rate
quozl May 30, 2023
bd6792f
Centered Screen and Restructured code
jriyyya May 30, 2023
ec491e0
Changed divison to floor division
jriyyya Jun 1, 2023
43a9dfb
Change object names for better understanding
jriyyya Jun 1, 2023
363793c
Remove unecessary print statement
jriyyya Jun 1, 2023
28d3052
Create class Spike in spike.py
jriyyya Jun 11, 2023
ab0e7e8
Created generic Game and Generator class
jriyyya Jun 11, 2023
863a12b
created Guy class in guy.py
jriyyya Jun 11, 2023
e6f2771
Updated main.py to use newer code structure
jriyyya Jun 11, 2023
830f254
deleted hardcoded gamemodes
jriyyya Jun 11, 2023
91f19ff
Made format for spikes_config consistent
jriyyya Jun 15, 2023
c88e15b
Removed parantheses where it was not necessary
jriyyya Jun 15, 2023
95d47dd
Adjusted parantheses for better readability
jriyyya Jun 15, 2023
f096254
improve readability by moving declaration closer to function
jriyyya Jun 15, 2023
5b117ff
Declared BLACK and SPAWN_SPIKE_EVENT as constants
jriyyya Jun 15, 2023
38346f0
made keymap into list comprehension
jriyyya Jun 15, 2023
9b36885
Added license
jriyyya Jun 15, 2023
ea573b0
Removed personal identification from license
jriyyya Jun 21, 2023
77ada49
optimise event handling
jriyyya Jun 21, 2023
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
21 changes: 13 additions & 8 deletions button.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@

import pygame


class Button:

def __init__(self, x, y, image_path, action, text = None):
def __init__(self, x, y, image_path, action, text=None):
self.graphic = pygame.image.load(image_path)
self.rect = self.graphic.get_rect()

self.x = x
self.y = y
self.rect.x = self.x
self.rect.x = self.x
self.rect.y = self.y

self.text = text
self.gameDisplay = pygame.display.get_surface()

self.action = action

self.draw()

def draw(self):
if self.hovered():
scaled_graphic = pygame.transform.scale(self.graphic, (self.rect.width + 10, self.rect.height + 5))
scaled_graphic = pygame.transform.scale(
self.graphic, (self.rect.width + 10, self.rect.height + 5))
self.gameDisplay.blit(scaled_graphic, (self.x - 5, self.y))
else:
self.gameDisplay.blit(self.graphic, (self.x, self.y))

if self.text is not None:
text_rect = self.text.get_rect()
self.gameDisplay.blit(self.text, (self.x + self.rect.width / 2 - text_rect.width / 2 , self.y + self.rect.height - 1.5 * text_rect.height))
self.gameDisplay.blit(self.text, (self.x + self.rect.width / 2 -
text_rect.width / 2, self.y + self.rect.height - 1.5 * text_rect.height))

def hovered(self):
return self.rect.collidepoint(pygame.mouse.get_pos())
Expand All @@ -37,3 +41,4 @@ def update(self):

if self.hovered() and pygame.mouse.get_pressed()[0] == 1:
self.action()

23 changes: 12 additions & 11 deletions cardiac.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

from random import *
import sys
import pygame
import pickle
from gi.repository import Gtk
import gi
gi.require_version('Gtk', '3.0')
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
from gi.repository import Gtk
import pickle
import pygame
import sys
from random import *


class pane2heartwindow:
Expand Down Expand Up @@ -136,7 +136,7 @@ def run(self, gameDisplay, info):
rightmove -= 34
time2 += 1

#[350,608] [659, 916]
# [350,608] [659, 916]

# Guy Display

Expand Down Expand Up @@ -206,26 +206,26 @@ def run(self, gameDisplay, info):
flag = 0
speed += 0.1

if (y_axis1 <= -40 and flag1 != 1) or (y_axis2 <= -40 and flag2 != 1) or (y_axisa <= -40 and flag3 != 1)or (y_axisb <= -40 and flag4 != 1):
if (y_axis1 <= -40 and flag1 != 1) or (y_axis2 <= -40 and flag2 != 1) or (y_axisa <= -40 and flag3 != 1) or (y_axisb <= -40 and flag4 != 1):
collide.play(0)
numberofhearts += 1

if(y_axis1 < -40):
if (y_axis1 < -40):
orientation1 = randint(0, 1)
flag1 = 0
y_axis1 = 700

if(y_axis2 < -40):
if (y_axis2 < -40):
orientation2 = randint(0, 1)
flag2 = 0
y_axis2 = 700

if(y_axisa < -40):
if (y_axisa < -40):
orientation4 = randint(0, 1)
flag3 = 0
y_axisa = 700

if(y_axisb < -40):
if (y_axisb < -40):
orientation5 = randint(0, 1)
flag4 = 0
y_axisb = 700
Expand Down Expand Up @@ -285,3 +285,4 @@ def run(self, gameDisplay, info):

if numberofhearts == 3:
return score

25 changes: 13 additions & 12 deletions fear.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

from random import *
import sys
import pygame
import pickle
from gi.repository import Gtk
import gi
gi.require_version('Gtk', '3.0')
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
from gi.repository import Gtk
import pickle
import pygame
import sys
from random import *


class pane4window:
Expand Down Expand Up @@ -62,7 +62,7 @@ def run(self, gameDisplay, info):
timer = pygame.time.Clock()

sound = True

jump = pygame.mixer.Sound("data/sound/jump.wav")
scoremusic = pygame.mixer.Sound("data/sound/score.wav")
collide = pygame.mixer.Sound("data/sound/fall.wav")
Expand All @@ -75,7 +75,7 @@ def run(self, gameDisplay, info):
event = pygame.event.poll()
# totaltime+=timer.tick()
if event.type == pygame.QUIT:
return
return

# print event

Expand Down Expand Up @@ -188,7 +188,7 @@ def run(self, gameDisplay, info):
rightdownmove -= 30
time2 += 1

#[350,608] [659, 916]
# [350,608] [659, 916]

# upper Guys Display

Expand Down Expand Up @@ -266,22 +266,22 @@ def run(self, gameDisplay, info):
scoremusic.play(0)
score += 1

if(y_axis1 < -40):
if (y_axis1 < -40):
orientation1 = randint(0, 1)

y_axis1 = 400

if(y_axis2 < 380):
if (y_axis2 < 380):
orientation2 = randint(0, 1)

y_axis2 = 700

if(y_axisa < -40):
if (y_axisa < -40):
orientation4 = randint(0, 1)

y_axisa = 400

if(y_axisb < 380):
if (y_axisb < 380):
orientation5 = randint(0, 1)

y_axisb = 700
Expand Down Expand Up @@ -309,3 +309,4 @@ def run(self, gameDisplay, info):

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

19 changes: 10 additions & 9 deletions howtoplay.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

from random import *
import sys
import pygame
import pickle
from gi.repository import Gtk
import gi
gi.require_version('Gtk', '3.0')
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
from gi.repository import Gtk
import pickle
import pygame
import sys
from random import *


class rules:
Expand All @@ -26,7 +27,7 @@ def run(self, gameDisplay, info):
back = pygame.transform.scale(back, (70, 40))

sound = True

while not self.crashed:
# Gtk events

Expand All @@ -45,7 +46,7 @@ def run(self, gameDisplay, info):
if back.get_rect(center=(750 + 35, 10 + 15)).collidepoint(mos_x, mos_y):
gameDisplay.blit(pygame.transform.scale(
back, (75, 45)), (725, 5))
if(pygame.mouse.get_pressed())[0] == 1 and press == 0:
if (pygame.mouse.get_pressed())[0] == 1 and press == 0:
press = 1
return

Expand All @@ -55,7 +56,7 @@ def run(self, gameDisplay, info):
else:
gameDisplay.blit(back, (725, 5))

#pygame.draw.circle(gameDisplay,(255,255,255), (750+35,20+15),5,2)
# pygame.draw.circle(gameDisplay,(255,255,255), (750+35,20+15),5,2)

pygame.display.update()
clock.tick(60)
clock.tick(60)
chimosky marked this conversation as resolved.
Show resolved Hide resolved
28 changes: 15 additions & 13 deletions impossible.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

from random import *
import sys
import pygame
import pickle
from gi.repository import Gtk
import gi
gi.require_version('Gtk', '3.0')
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
from gi.repository import Gtk
import pickle
import pygame
import sys
from random import *


class pane6window:
Expand Down Expand Up @@ -74,7 +75,7 @@ def run(self, gameDisplay, info):
timer = pygame.time.Clock()

sound = True

jump = pygame.mixer.Sound("data/sound/jump.wav")
scoremusic = pygame.mixer.Sound("data/sound/score.wav")
collide = pygame.mixer.Sound("data/sound/fall.wav")
Expand Down Expand Up @@ -255,7 +256,7 @@ def run(self, gameDisplay, info):
rightdownmove -= 30
time2 += 1

#[350,608] [659, 916]
# [350,608] [659, 916]

# upper Guy Display

Expand Down Expand Up @@ -313,32 +314,32 @@ def run(self, gameDisplay, info):
scoremusic.play(0)
score += 1

if(y_axis1 < -40):
if (y_axis1 < -40):
orientation1 = randint(0, 1)

y_axis1 = 400

if(y_axis2 < 380):
if (y_axis2 < 380):
orientation4 = randint(0, 1)

y_axis2 = 700

if(y_axisa < -40):
if (y_axisa < -40):
orientation2 = randint(0, 1)

y_axisa = 400

if(y_axisb < 380):
if (y_axisb < 380):
orientation5 = randint(0, 1)

y_axisb = 700

if(y_axisx < -40):
if (y_axisx < -40):
orientation3 = randint(0, 1)

y_axisx = 400

if(y_axisy < 380):
if (y_axisy < 380):
orientation6 = randint(0, 1)

y_axisy = 700
Expand Down Expand Up @@ -445,3 +446,4 @@ def run(self, gameDisplay, info):

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

chimosky marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 13 additions & 12 deletions inferno.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

from random import *
import sys
import pygame
import pickle
from gi.repository import Gtk
import gi
gi.require_version('Gtk', '3.0')
jriyyya marked this conversation as resolved.
Show resolved Hide resolved
from gi.repository import Gtk
import pickle
import pygame
import sys
from random import *


class pane5window:
Expand Down Expand Up @@ -75,7 +75,7 @@ def run(self, gameDisplay, info):
timer = pygame.time.Clock()

sound = True

jump = pygame.mixer.Sound("data/sound/jump.wav")
scoremusic = pygame.mixer.Sound("data/sound/score.wav")
collide = pygame.mixer.Sound("data/sound/fall.wav")
Expand Down Expand Up @@ -228,7 +228,7 @@ def run(self, gameDisplay, info):
rightdownmove -= 30
time2 += 1

#[350,608] [659, 916]
# [350,608] [659, 916]

# up Guys Display

Expand Down Expand Up @@ -301,27 +301,27 @@ def run(self, gameDisplay, info):
scoremusic.play(0)
score += 1

if(y_axis1 < -40):
if (y_axis1 < -40):
orientation1 = randint(0, 1)

y_axis1 = 400

if(y_axis2 < 380):
if (y_axis2 < 380):
orientation2 = randint(0, 1)

y_axis2 = 700

if(y_axisa < -40):
if (y_axisa < -40):
orientation3 = randint(0, 1)

y_axisa = 400

if(y_axisb < 380):
if (y_axisb < 380):
orientation4 = randint(0, 1)

y_axisb = 700

if(y_axisx < -40):
if (y_axisx < -40):
orientation5 = randint(0, 1)

y_axisx = 400
Expand Down Expand Up @@ -399,3 +399,4 @@ def run(self, gameDisplay, info):

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

chimosky marked this conversation as resolved.
Show resolved Hide resolved
Loading