Full disclosure: I am not any good at programming. This is for a programming project that I have to do at school; we're using Pygame but I don't really know how to use it so I've been following a tutorial but I've got confused.
Basically I've got it to draw some boxes, which are supposed to act the floor for a stickman character to walk on (it's just a simple 2D platformer), which is done using an object oriented approach (have learnt about it somewhat but I'm not very confident). The issue is that I have no idea how to get the stickman on the screen or how to get it to actually appear standing on the boxes.
import random
import pygame
import time
pygame.init()
display_width = 800 # game window width and height
display_height = 600
background = (255,255,255) # some variables to be used later
bg = pygame.image.load('smb.png')
black = (0,0,0) # some colours for buttons and stuff
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
object_width = 50 # width hitbox of the object
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('the greatest 2d platformer of all time: the game')
clock = pygame.time.Clock()
def button(msg,x,y,w,h,ic,ac,action=None): # button function allows menu to work later
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
gameDisplay.blit(textSurf, textRect)
def message_display(text): # message function that makes messages appear after various actions (usually crashing)
largeText = pygame.font.Font('freesansbold.ttf',100)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def text_objects(text, font): # some text thing? think it just allows messages to work
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def things(thingx, thingy, thingw, thingh, color): # i think this is for the floating squares?
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
def game_intro(): # basically the main menu
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
# sets white background and adds the text and buttons for the main menu
gameDisplay.fill(white)
largeText = pygame.font.SysFont("comicsansms",25)
TextSurf, TextRect = text_objects("the greatest 2d platformer of all time: the game", largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
button("start",150,450,100,50,green,bright_green, game_loop)
button(":(",550,450,100,50,red,bright_red,pygame.quit)
pygame.display.update()
clock.tick(15)
class Sprite(pygame.sprite.Sprite):
def __init__(self, image, startx, starty):
super().__init__()
self.image = pygame.image.load('boxAlt.png')
self.rect = self.image.get_rect()
self.rect.center = [startx, starty]
def update(self):
pass
def draw(self, screen):
screen.blit(self.image, self.rect)
class Player(Sprite):
def __init__(self, startx, starty):
super().__init__("stickman.png", startx, starty)
class Box(Sprite):
def __init__(self, startx, starty):
super().__init__("boxAlt.png", startx, starty)
def game_loop():
pygame.init()
gameDisplay = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()
player = Player(200,400)
boxes = pygame.sprite.Group()
for bx in range(0,400,70):
boxes.add(Box(bx,300))
while True:
pygame.event.pump()
player.update()
#draw loop
gameDisplay.fill(background)
player.draw(gameDisplay)
boxes.draw(gameDisplay)
pygame.display.flip()
clock.tick(60) # 60fps frame rate
game_intro()