Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Pygame module for developing a game in python and installing pygame in pc, Study notes of Computer Science

This report contains the information about installing the pygame module.

Typology: Study notes

2018/2019

Uploaded on 12/12/2019

DJ5468
DJ5468 🇮🇳

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 | P a g e
A Technical Report On
PyGame Module
Submitted in fulfillment of Self Creation Parameter of
DSE4-030010411:Multi-paradigm Programming
Subject
By
201703100110008- Rutvik Patel,
201703100110020- Aakash Patel,
201703100110057- Jay Devani,
201703100110047- Abhishek Patel
Class :- SY-BCA[A] 4th Sem
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Pygame module for developing a game in python and installing pygame in pc and more Study notes Computer Science in PDF only on Docsity!

A Technical Report On

PyGame Module

Submitted in fulfillment of Self Creation Parameter of

DSE4-030010411:Multi-paradigm Programming

Subject

By

201703100110008 - Rutvik Patel, 201703100110020 - Aakash Patel, 201703100110057 - Jay Devani, 201703100110047 - Abhishek Patel

Class :- SY-BCA[A] 4 th^ Sem

Table of Contents

  • Abstract
  • Introduction
  • Installing package in computer
  • Developed Module
  • Output __________________1
  • Methods of the pygame module
  • References

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

DEVELOPED MODULE:

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:

1. Apple.py

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'] ] )

2. Config.py

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']

Fill background and draw game area

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 ] )

Draw an apple

apple_rect = apple.draw()

Move and Re-Draw Snake

snake.move(x_change, y_change) snake_rect = snake.draw() snake.draw_body()

Detect collison with wall

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'])

4. Snake.py

import pygame

from src.Config import Config

class Snake:

def init(self, display):

self.x_pos = (Config['game']['width'] - 30) / 2

self.y_pos = (Config['game']['height'] - 30) / 2

self.display = display

self.body = []

self.max_size = 0

def eat(self):

self.max_size += 1

def draw(self):

return pygame.draw.rect(

self.display,

Config['colors']['white'],

[

self.x_pos,

self.y_pos,

Config['snake']['height'],

Config['snake']['width']

]

def draw_body(self):

for item in self.body:

pygame.draw.rect(

self.display,

Config['colors']['green'],

[

item[0],

item[1],

Config['snake']['width'],

Config['snake']['height']

]

def move(self, x_change, y_change):

self.body.append((self.x_pos, self.y_pos))

self.x_pos += x_change

self.y_pos += y_change

if len(self.body) > self.max_size:

del(self.body[0])