Answers for "how to use python for game development"

0

how to make a game in python

# Snake Game

import turtle
import random
import time

score = 0

# screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Snake Game By Atharva")
screen.tracer(0)

# Snake
snake = turtle.Turtle()
snake.shape("square")
snake.color("green")
snake.penup()
snake_pos = "None"

# apple
apple = turtle.Turtle()
apple.shape("circle")
apple.color("red")
apple.penup()
apple.goto(0, 100)

# classes


class SnakeMovements:
  y = 0
  x = 0
  snpos = snake_pos
  
  snpos = None
	def main(self):
      if self.snpos == "up":
        self.y = snake.ycor()
        snake.sety(self.y + 15)
        
      if self.snpos == "down":
        self.y = snake.ycor()
        snake.sety(self.y - 15)
        
      if self.snpos == "right":
        self.x = snake.xcor()
        snake.setx(self.x + 15)
        
      if self.snpos == "left":
        self.x = snake.xcor()
        snake.setx(self.x - 15)
        
    def SnakeUp(self):
      self.snpos = "up"
      
    def SnakeDown(self):
      self.snpos = "down"
      
    def SnakeRight(self):
      self.snpos = "right"
      
    def SnakeLeft(self):
      self.snpos = "left"
      
instanceVar = SnakeMovements()

# Keyboard Binding

screen.listen()
screen.onkeypress(instanceVar.SnakeUp, "Up")
screen.onkeypress(instanceVar.SnakeDown, "Down")
screen.onkeypress(instanceVar.SnakeRight, "Right")
screen.onkeypress(instanceVar.SnakeLeft, "Left")

# Mainloop of the game

while True:
  if snake.distance(apple) < 20:
    score += 1
    print(score)
    applex = random.randint(-290, 290)
    appley = random.randint(-290, 290)
    apple.goto(applex, appley)
    
    time.sleep(0.1)
  
  instanceVar.main()
  screen.update()
Posted by: Guest on November-30-2020
0

python libraries for game development

import pygame
pygame.init()

win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True

while run:
    pygame.time.delay(100) # This will delay the game the given amount of milliseconds. In our casee 0.1 seconds will be the delay

    for event in pygame.event.get():  # This will loop through a list of any keyboard or mouse events.
        if event.type == pygame.QUIT: # Checks if the red button in the corner of the window is clicked
            run = False  # Ends the game loop

pygame.quit()  # If we exit the loop this will execute and close our game
Posted by: Guest on May-28-2021
-1

how to make a game in python

# Snake Game

import turtle
import random
import time

score = 0

# screen
screen = turtle.Screen()
screen.bgcolor("black")
screen.title("Snake Game By Atharva")
screen.tracer(0)

# Snake
snake = turtle.Turtle()
snake.shape("square")
snake.color("green")
snake.penup()
snake_pos = "None"

# apple
apple = turtle.Turtle()
apple.shape("circle")
apple.color("red")
apple.penup()
apple.goto(0, 100)

# classes


class SnakeMovements:
  y = 0
  x = 0
  snpos = snake_pos
  
  snpos = None
	def main(self):
      if self.snpos == "up":
        self.y = snake.ycor()
        snake.sety(self.y + 15)
        
      if self.snpos == "down":
        self.y = snake.ycor()
        snake.sety(self.y - 15)
        
      if self.snpos == "right":
        self.x = snake.xcor()
        snake.setx(self.x + 15)
        
      if self.snpos == "left":
        self.x = snake.xcor()
        snake.setx(self.x - 15)
        
    def SnakeUp(self):
      self.snpos = "up"
      
    def SnakeDown(self):
      self.snpos = "down"
      
    def SnakeRight(self):
      self.snpos = "right"
      
    def SnakeLeft(self):
      self.snpos = "left"
      
instanceVar = SnakeMovements()

# Keyboard Binding

screen.listen()
screen.onkeypress(instanceVar.SnakeUp, "Up")
screen.onkeypress(instanceVar.SnakeDown, "Down")
screen.onkeypress(instanceVar.SnakeRight, "Right")
screen.onkeypress(instanceVar.SnakeLeft, "Left")

# Mainloop of the game

while True:
  if snake.distance(apple) < 20:
    score += 1
    print(score)
    applex = random.randint(-290, 290)
    appley = random.randint(-290, 290)
    apple.goto(applex, appley)
    
    time.sleep(0.1)
  
  instanceVar.main()
  screen.update()
 ++
Posted by: Guest on November-30-2020
0

python libraries for game development

python3 -m pip install pygame <<<(mac)>>>
or
python -m pip install pygame <<<(windows)>>>
or
sudo apt install python3-pygame <<<(ubuntu)>
Posted by: Guest on May-28-2021

Code answers related to "how to use python for game development"

Python Answers by Framework

Browse Popular Code Answers by Language