r/learnpython • u/Emotional_Reporter35 • 12d ago
Guitar hero Student project (need assistance)
Hello all! I am a student in CS 101. I am trying to make my final project which is gonna be a derivative of guitar hero.
I don't wanna use ai but also it is not allowed.
My main concern with the code is wondering how I might be able to have multiple notes in a single lane at the same time. If you look at the code, the spawn rate of the notes is very dependent on the previous note in the same lane being undrawn.
if another is drawn while one is active note.move will abandon the previous note. and/or will undraw the whole lane.
all help will be appreciated
Main game driver
from graphics import *
import time
from gamelogicwip3 import *
win = GraphWin('Guitar Villain', 800, 600, autoflush=False)
win.setCoords(0, 0, 800, 600)
activenotes = []
# Establishing all the variables that will need to be added onto or changed
notecounter = 0
tickloop = 0
loopcount = 0
speed = 10
#Txt file that maps notes that need to be launched (songmap for updates)
songmap = open('maryhadalittlelamb.txt', 'r')
#reads all the lines in the map file
songlines = songmap.readlines()
# Line where notes should be hit at
noteline = Line(Point(120, 100), Point(680, 100))
noteline.draw(win)
#Establishing a note for each lane
note0 = Note(0, speed)
note1 = Note(1, speed)
note2 = Note(2, speed)
note3 = Note(3, speed)
while win.isOpen():
tickloop = tickloop +1
if tickloop %20 == 0:
songline = songlines[loopcount].strip()
lane = songline.find('1')
if lane == 0:
note0.draw(0, win)
if lane == 1:
note1.draw(1, win)
if lane == 2:
note2.draw(2, win)
if lane == 3:
note3.draw(3, win)
loopcount = loopcount+1
if tickloop == 20:
tickloop = 0
note1.send(speed, win)
note2.send(speed, win)
note0.send(speed, win)
note3.send(speed, win)
seeKey(win, note0, note1, note2, note3, 0)
update(55)
_=win.getMouse()
win.close()
Game Logic:
from graphics import *
class Note:
def __init__(self, lane, speed):
self.lane = lane
self.notelane0 = None
self.notelane1 = None
self.notelane2 = None
self.notelane3 = None
self.hitboxlane0 = 640
self.hitboxlane1 = 640
self.hitboxlane2 = 640
self.hitboxlane3 = 640
self.speed = speed
self.score = 0
def draw(self, lane, win):
if lane == 0:
self.notelane0 = Rectangle(Point(220, 620), Point(260, 660))
self.notelane0.setFill('coral')
self.notelane0.setOutline('red')
self.notelane0.draw(win)
self.hitboxlane0 = 640
if lane == 1:
self.notelane1 = Rectangle(Point(340, 620), Point(380, 660))
self.notelane1.setFill('yellow')
self.notelane1.setOutline('olive')
self.notelane1.draw(win)
self.hitboxlane1 = 640
if lane == 2:
self.notelane2 = Rectangle(Point(440, 620), Point(480, 660))
self.notelane2.setFill('turquoise')
self.notelane2.setOutline('blue')
self.notelane2.draw(win)
self.hitboxlane2 = 640
if lane == 3:
self.notelane3 = Rectangle(Point(540, 620), Point(580, 660))
self.notelane3.setFill('olive')
self.notelane3.setOutline('darkgreen')
self.notelane3.draw(win)
self.hitboxlane3 = 640
def send(self, speed, win):
if self.notelane0:
pluck0 = None
self.notelane0.move(0, -speed)
self.hitboxlane0 = self.hitboxlane0 - speed
if self.hitboxlane0 < -20:
self.notelane0.undraw()
if self.notelane1:
pluck1 = None
self.notelane1.move(0, -speed)
self.hitboxlane1 = self.hitboxlane1 - speed
if self.hitboxlane1 < -20:
self.notelane1.undraw()
if self.notelane2:
pluck2 = None
self.notelane2.move(0, -speed)
self.hitboxlane2 = self.hitboxlane2 - speed
if self.hitboxlane2 < -20:
self.notelane2.undraw()
if self.notelane3:
pluck3 = None
self.notelane3.move(0, -speed)
self.hitboxlane3 = self.hitboxlane3 - speed
if self.hitboxlane3 < -20:
self.notelane3.undraw()
harda, hardb = 80, 140
easya, easyb = 50, 170
def seeKey(win, note0, note1, note2, note3, score):
keystroke = win.checkKey()
if keystroke == 'a' and (easya<note0.hitboxlane0<easyb):
note0.notelane0.undraw()
print('HIT')
score = score+1
if keystroke == 's' and (easya<note1.hitboxlane1<easyb):
note1.notelane1.undraw()
print('HIT')
score = score+1
if keystroke == 'd' and (easya<note2.hitboxlane2<easyb):
note2.notelane2.undraw()
print('HIT')
score = score+1
if keystroke == 'f' and (easya<note3.hitboxlane3<easyb):
note3.notelane3.undraw()
print('HIT')
score = score+1
if keystroke == 'Escape':
win.close()
return score
11
u/TraditionalYam4500 12d ago
I think you need to repost with the correct indentation… which is kinda significant in Python.
1
u/Massive_Distance_616 12d ago
The reason is how your Note class is built.
Each Note keeps one rectangle per lane as an attribute, so when you draw a second note in the same lane, the new rectangle replaces the reference to the old one, and the old one never gets moved or undrawn again.
A simpler shape is for each Note object to be a single note, with its own rectangle and its own y position.
Keep one list of active notes, and on each tick loop over the list, move every note, and undraw and remove the ones that have gone past the line.
Spawning a note is then just creating a new Note and appending it, so a lane no longer has to be empty first.
For a key press, look through the list for the lowest note in that lane and check whether it’s inside your hit window.
One more thing you’ll hit once that works: you call seeKey with 0 as the score every frame and never store what it returns, so the score will reset each time.
Also, as the others say, the indentation got lost in your post, so it’s worth reposting the code in a code block.
Does that fit with how your song file spawns the notes?
1
u/zaphodikus 9d ago
Post in a gist or in a github, easier to read that way.