(back to SCUBA24)
import pygame import os pygame.init() window = pygame.display.set_mode((600,600)) window.fill(...) pygame.display.set_caption('Characters') class Character: def __init__(self, name, age, flavor, position, image_filename): # finish the init function self.image = # for this one you need a function...(look at the docs) def rect(self): # fill in the arguments to pygame.Rect return pygame.Rect() def main(): # create 3 character objects # put the characters in the list characters = [...] def repaint(): window.fill(...) for c in characters: # draw the characters using the position and image pygame.display.update() # ask the player to click a character ... app_is_running = True while app_is_running == True: for event in pygame.event.get(): # check if the event is quitting: # quit # check if the event is pressing a key: # if K_q # quit elif event.type == pygame.MOUSEBUTTONDOWN: mousepos = # get the mouse postion for c in characters: rect = c.rect() if # check if rect collides with mousepos (using collidepoint): # print their name, age, and their favorite ice cream # call the repaint function pygame.quit() main()
import pygame pygame.init() window = pygame.display.set_mode(...) def main(): fixed_rect = pygame.Rect(...) follower_rect = pygame.Rect(0, 0,20,20) fixed_color = ... follower_color = ... def repaint(): window.fill(...) pygame.draw.rect(...) # draw fixed_rect pygame.draw.rect(...) # draw follower_rect pygame.display.update() app_is_running = True while app_is_running: for event in pygame.event.get(): if event.type==pygame.QUIT: app_is_running = False elif event.type==pygame.KEYDOWN: if event.key==pygame.K_q: app_is_running = False elif event.type==pygame.MOUSEMOTION: follower_rect.center = pygame.mouse.get_pos() # this makes folllower_rect follow the cursor if fixed_rect.colliderect(follower_rect): fixed_color = ... else: fixed_color = ... repaint() pygame.quit() main() # based on the colliderect example at # https://github.com/Rabbid76/PyGameExamplesAndAnswers/blob/master/documentation/pygame/pygame_collision_and_intesection.md # as of Jul 15 2024
import pygame # below, set the size of the window window = pygame.display.set_mode((...)) # below, fill the window with white window.fill(...) # below, add a title to your window pygame.display.set_caption(...) # this custom user event (ball_move_event)... ball_move_event = pygame.USEREVENT + 1 # is scheduled to happen every 10 milliseconds (100 times per second) pygame.time.set_timer(ball_move_event, 10) class Ball: def __init__(self, color, center_x, center_y, radius, velocity_x, velocity_y): self.color = color # finish the init function def draw(self): # draw a circle using pygame.draw.circle using self.color, self.center_x, etc. # main() function should be outside of the class def main(): # create a Ball with color, center_x, center_y, radius, velocity_x, velocity_y b = Ball(...) def repaint(): # fill the window with a color # call the draw method of the ball object # update pygame window app_is_running = True while app_is_running == True: for event in pygame.event.get(): # check if the event is quitting: # quit # check if the event is pressing a key: # if K_q # quit # if K_s # decrease the ball radius by a number elif event.type == ball_move_event: # add the x-velocity to the ball's center x # add the y-velocity to the ball's center y # if the ball is beyond the left edge of the window # multiply the x-velocity by -1 # if the ball is beyond the right edge of the window # multiply the x-velocity by -1 # if the ball is beyond the top edge of the window # multiply the y-velocity by -1 # if the ball is beyond the bottom edge of the window # multiply the y-velocity by -1 #call repaint pygame.quit() main()
import pygame, random pygame.init() #below, give values for the size of window window = pygame.display.set_mode((...)) #below,fill the window to be white window.fill(...) #below, add a caption for your window pygame.display.set_caption(...) #create a function called random_color def random_color(): #use random to make red be a random integer from 0-255 #use random to make green be a random integer from 0-255 #use random to make blue be a random integer from 0-255 #use return to get the values for red,green,and blue #create a function called main: def main(): #below, make the ball color black ball_color = ... #below, set the ball center coordinates #we want the ball to start on the far left and half way down ball_center = [...] #make the radius of your ball be equal to 2 ball_radius = ... #make a function called repaint def repaint(): #draw the circle you created onto your window #update the window #call repaint() app_is_running = True while app_is_running==True: for event in pygame.event.get(): #check if the event is quitting: #quit #check if the event is pressing a key: #check if the key is Q #quit #check if the ball is all the way to the right side of the widnow using window.get_width if ... : #make the ball color be a random color using your function #make the ball center coordinates be x = 0 and a random number for y #make the ball radius be a random number from 1-5 else: #add to the x coordinate of the ball so it moves right ball_center[0] += 1 #call repaint() pygame.quit() main()
import pygame import os # pygame initialization pygame.init() window = pygame.display.set_mode((256,256)) def main(): # set window title pygame.display.set_caption('Fader') # Fill window with black and update display red = 0 green = 0 blue = 0 mode = 'red' app_is_running = True while app_is_running==True: for event in pygame.event.get(): # Quit Event # Key Events: # q is quit # r makes mode red and figlets: RED # g makes mode green and figlets: GREEN # b makes mode blue and figlets: BLUE # down arrow should reset red green and blue variables to 0, fill the window, and update display # up arrow should reset red green and blue variables to 255, fill the window, and update display elif event.type == pygame.MOUSEMOTION: # The next line grabs the position of the mouse (mx, my) = pygame.mouse.get_pos() # depending on the mode, make one of red, green, or blue equal to mx # window fill with red, green and blue # update display pygame.quit() main()
# RECOMMENDED STRUCTURE: # 1) make the character walk to the right # 2a) (extension) make an "about" screen # 2b) (extension) make the character able to walk to the left too # 3) allow the user to choose a character import pygame import os # pygame initialization pygame.init() window = pygame.display.set_mode((740,415)) window.fill((255,255,255)) # set window title pygame.display.set_caption(...) # give the app a title # background bg = pygame.image.load(...) # add the file path for your background image #this for loop will iterate through all your images robot_frames_right = [] for i in range(...): #give it a range filename = # build the right filename for number i img = pygame.image.load(filename) # load the image of your character robot_frames_right.append(img) def main(): robot_x = 0 robot_y = 260 window.blit(bg,(0,0)) robot_index = 0 pygame.font.init() font = pygame.font.SysFont('Verdana',24) def refresh(): print(robot_index) #blit the background # grab the current frame # blit the frame # update display app_is_running = True refresh() while app_is_running==True: for event in pygame.event.get(): # if it is a quit event # make the app stop running # if it is a keystroke # if its a q # make the app stop running # if it is a RIGHT # update robot index and position # call refresh pass pygame.quit() main()
import guizero as gz def reset_sliders(): # set sliders back to black color redslider.value = greenslider.value = blueslider.value = def update_sample(): red = # get the current value of the red slider, convert to int green = # get the current value of the green slider, convert to int blue = # get the current value of the blue slider, convert to int message.value = # construct the string of the form "(10,20,30)" topsample.bg = # construct the RGB tuple from red, green, blue above bottomsample.bg = # construct the RGB tuple from red, green, blue above # define the base app window app = gz.App('RGB Sliders', width=500, height=500) # add GUI components to app one at a time topsample = gz.Box(app, width='fill', height=60) topsample.bg = 'black' redslider = gz.Slider(app, start=0, end=255, command=update_sample, width='fill', height=30) redslider.bg = 'lightgray' greenslider = gz.Slider(...) # fill this in similar to redslider above greenslider.bg = 'lightgray' blueslider = gz.Slider(...) # fill this in similar to redslider, greenslider above blueslider.bg = 'lightgray' bottomsample = gz.Box(app, width='fill', height=60) bottomsample.bg = 'black' reset_button = gz.PushButton(app, text='Reset', command=reset_sliders) message = gz.Text(app, width=30, height=30, size=24, text='(0,0,0)') app.display()
import pygame pygame.init() windowsize = (200, 800) skyblue = (135, 206, 235) cone = pygame.image.load('images/cone.png') vanilla = pygame.image.load('images/scoop-vanilla.png') chocolate = pygame.image.load('images/scoop-chocolate.png') strawberry = pygame.image.load('images/scoop-strawberry.png') def main(): cone_position = (75, 700) pygame.display.set_caption('Ice Cream Cone Builder') window = pygame.display.set_mode(windowsize) window.fill(skyblue) pygame.display.update() scoops = [] def draw_cone(): #make the background sky blue #blit the cone to its position #make scoop_top and scoop_left which are where the next scoop goes #scoop_left should be the x coord of the cone #scoop_top should be slightly above the cone for scoop in scoops: #if scoop is vanilla #blit vanilla onto scoop positions # if scoop is chocolate # blit chocolate onto scoop positions # if scoop is strawberry # blit strawberry onto scoop positions #change where the next scoop goes by changing scoop_top pass pygame.display.update() draw_cone() app_is_running = True while app_is_running: for event in pygame.event.get(): if event.type == pygame.QUIT: app_is_running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q: app_is_running = False elif event.key == pygame.K_v: #append vanilla to scoops draw_cone() elif event.key == pygame.K_c: #append chocolate to scoops draw_cone() elif event.key == pygame.K_s: #append strawberry to scoops draw_cone() elif event.key == pygame.K_r: #reset scoops to empty list draw_cone() pygame.quit() main()