Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- pygame.init()
- orange = (255, 165, 0)
- red = (255, 0, 0)
- green = (0, 150, 0)
- white = (255, 255, 255)
- black = (0, 0, 0)
- wind_width = 1200
- wind_height = 700
- # Changeable variables
- wall_width = 10
- wall_length = 50
- ball_size = 7
- speed = 3
- # Pygame setup
- gameDisplay = pygame.display.set_mode((wind_width, wind_height))
- pygame.display.set_caption("Mini Golf!")
- pygame.display.update()
- gameExit = False
- clock = pygame.time.Clock()
- # Class for vertical walls
- class Vwall:
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.type = "vwall"
- # math to get all walls lined up right
- self.rect = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y, wall_width, wall_length)
- # Draws walls based on the rect predefined
- def render(self):
- pygame.draw.rect(gameDisplay, orange, self.rect)
- # Class for horizontal walls
- class Hwall:
- def __init__(self,x,y):
- self.x = x
- self.y = y
- self.type = "hwall"
- self.rect = pygame.Rect(self.x, self.y + (wall_length / 2) - (wall_width / 2), wall_length, wall_width)
- def render(self):
- pygame.draw.rect(gameDisplay, orange, self.rect)
- # Class for corner pieces
- class Corner:
- def __init__(self, x, y, orien):
- self.x = x
- self.y = y
- self.type = "corner"
- # orientation tells which was the corner faces
- self.orien = orien
- # 2 rects needed to make the corner, and 4 possible corner alingments
- if self.orien == 0:
- self.rect_h = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + wall_width, wall_width)
- self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y, wall_width, (wall_length / 2) + (wall_width / 2))
- elif self.orien == 1:
- self.rect_h = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), wall_width, (wall_length / 2) + wall_width)
- self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + wall_width, wall_width)
- elif self.orien == 2:
- self.rect_h = pygame.Rect(self.x, self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + (wall_width / 2), wall_width)
- self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), wall_width, (wall_length / 2) + wall_width)
- elif self.orien == 3:
- self.rect_h = pygame.Rect(self.x, self.y + (wall_length / 2) - (wall_width / 2), (wall_length / 2) + (wall_width / 2), wall_width)
- self.rect_v = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y, wall_width, (wall_length / 2) + (wall_width / 2))
- def render(self):
- pygame.draw.rect(gameDisplay, red, self.rect_h)
- pygame.draw.rect(gameDisplay, red, self.rect_v)
- # Class for the golf ball
- class Ball:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- self.type = "ball"
- # determines direction of ball
- self.x_change = 0
- self.y_change = 0
- self.rect = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), ball_size, ball_size)
- # draws ball and updates rectangle to new position each frame
- def render(self):
- self.rect = pygame.Rect(self.x + (wall_length / 2) - (wall_width / 2), self.y + (wall_length / 2) - (wall_width / 2), ball_size, ball_size)
- pygame.draw.rect(gameDisplay, white, self.rect)
- # level design
- # H is a horizontal wall
- # V is a vertical wall
- # O is nothing but a placeholder
- # 1, 2, 3, and 4 are corners with different orientations
- # B is where the ball starts
- level = [
- ["1","H","H","H","H","2"],
- ["V","O","O","O","O","V"],
- ["V","O","O","O","O","V"],
- ["V","O","O","1","H","3"],
- ["V","O","O","V"],
- ["V","O","O","V"],
- ["V","O","O","V"],
- ["V","O","O","V"],
- ["V","O","O","V"],
- ["V","O","B","V"],
- ["0","H","H","3"]
- ]
- # list for all wall objects
- walls = []
- # translates the level into walls and corners stored in the walls list
- for y in range(len(level)):
- for x in range(len(level[y])):
- piece = level[y][x]
- if piece == "V":
- new_wall = Vwall(x * wall_length, y * wall_length)
- walls.append(new_wall)
- elif piece == "H":
- new_wall = Hwall(x * wall_length, y * wall_length)
- walls.append(new_wall)
- elif piece == "0" or piece == "1" or piece == "2" or piece == "3":
- new_wall = Corner(x * wall_length, y * wall_length, int(piece))
- walls.append(new_wall)
- elif piece == "B":
- ball = Ball(x * wall_length, y * wall_length)
- # game loop
- while not(gameExit):
- # events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- gameExit = True
- # Starts moving ball when the mouse is clicked
- if event.type == pygame.MOUSEBUTTONUP:
- ball.x_change = -(speed)
- ball.y_change = -(speed)
- gameDisplay.fill(green)
- # draws walls
- for wall in walls:
- wall.render()
- # Move in x direction
- ball.x += ball.x_change
- # Check for collision
- for wall in walls:
- if wall.type == "vwall":
- if wall.rect.colliderect(ball.rect):
- # If colliding with a vertical wall, undo the last two frames of movement (more than neccesary), and make the ball move in the opposite vertical direction
- ball.x -= ball.x_change * 2
- ball.x_change = -(ball.x_change)
- # same thing but for corners
- elif wall.type == "corner":
- if wall.rect_v.colliderect(ball.rect):
- ball.x -= ball.x_change * 2
- ball.x_change = -(ball.x_change)
- # move in y direction
- ball.y += ball.y_change
- # check for collision
- for wall in walls:
- if wall.type == "hwall":
- if wall.rect.colliderect(ball.rect):
- # If colliding with a horizontal wall, undo the last two frames of movement (more than neccesary), and make the ball move in the opposite horizontal direction
- ball.y -= ball.y_change * 2
- ball.y_change = -(ball.y_change)
- # same thing for corners
- elif wall.type == "corner":
- if wall.rect_h.colliderect(ball.rect):
- ball.y -= ball.y_change * 2
- ball.y_change = -(ball.y_change)
- ball.render()
- pygame.display.update()
- clock.tick(100)
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement