






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This report contains the information about installing the pygame module.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Submitted in fulfillment of Self Creation Parameter of
201703100110008 - Rutvik Patel, 201703100110020 - Aakash Patel, 201703100110057 - Jay Devani, 201703100110047 - Abhishek Patel
The best way toinstall pygame is with the pip tool (which is what python uses to install packages). Note, this comes with python in recent versions. We use the --user flag to tell it to install into the home directory, rather than globally. python3 - m pip install - U pygame – user .open CMD 2.Path should locate where pip is installed for me it is C:\Users\qwrht\AppData\Local\Programs\Python\Python36- 32 \Scripts> if python is in other directory follow steps in link CMD command to change directories 3.getting to the right path where pip is installed use command pip install pygame.in CMD 4.after Downloading pygame current version, Successfully installed pygame message will be displayed
Main File : import pygame from src.Game import Game from src.Config import Config def main(): display = pygame.display.set_mode(( Config['game']['width'], Config['game']['height'] )) pygame.display.set_caption(Config['game']['caption']) game = Game(display) game.loop() if name == 'main': main() Source Files:
import random import pygame from src.Config import Config class Apple: def init(self, display): self.x_pos = 0 self.y_pos = 0 self.display = display self.randomize() def randomize(self): height = Config['game']['height'] width = Config['game']['width'] bumper = Config['game']['bumper_size'] max_x = (height - bumper - Config['snake']['width']) max_y = (height - bumper - Config['snake']['height']) self.x_pos = random.randint(bumper, max_x) self.y_pos = random.randint(bumper, max_y) def draw(self): return pygame.draw.rect( self.display, Config['colors']['red'], [ self.x_pos, self.y_pos, Config['apple']['height'], Config['apple']['width'] ] )
import pygame from src.Config import Config from src.Snake import Snake from src.Apple import Apple class Game: def init(self, display): self.display = display self.score = 0 def loop(self): clock = pygame.time.Clock() snake = Snake(self.display) apple = Apple(self.display) x_change = 0 y_change = self.score = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: x_change = - Config['snake']['speed'] y_change = 0 elif event.key == pygame.K_RIGHT: x_change = Config['snake']['speed'] y_change = 0 elif event.key == pygame.K_UP: x_change = 0
y_change = - Config['snake']['speed'] elif event.key == pygame.K_DOWN: x_change = 0 y_change = Config['snake']['speed']
self.display.fill(Config['colors']['green']) pygame.draw.rect( self.display, Config['colors']['black'], [ Config['game']['bumper_size'], Config['game']['bumper_size'], Config['game']['height'] - Config['game']['bumper_size'] * 2, Config['game']['width'] - Config['game']['bumper_size'] * 2 ] )
apple_rect = apple.draw()
snake.move(x_change, y_change) snake_rect = snake.draw() snake.draw_body()
bumper_x = Config['game']['width'] - Config['game']['bumper_size'] bumper_y = Config['game']['height'] - Config['game']['bumper_size'] if ( snake.x_pos < Config['game']['bumper_size'] or snake.y_pos < Config['game']['bumper_size'] or snake.x_pos + Config['snake']['width'] > bumper_x or
self.display.blit(score, score_rect) self.display.blit(title, title_rect) pygame.display.update() clock.tick(Config['game']['fps'])