Answers for "how to make game with python"

1

simple game with python

# 1 - Import library
import pygame
from pygame.locals import *

# 2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))

# 3 - Load images
player = pygame.image.load("resources/images/dude.png")

# 4 - keep looping through
while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the screen elements
    screen.blit(player, (100,100))
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        # check if the event is the X button 
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit() 
            exit(0)
Posted by: Guest on January-13-2021
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
1

how to make a simple game in python

# player move forever forward
# background a colour
# bubble for player to collect

import turtle
import random
import time

# quick variables

player_speed = 0.2

# making the turtles

player = turtle.Turtle()
enemy = turtle.Turtle()
bubble = turtle.Turtle()
window = turtle.Screen()
display = turtle.Turtle()


# turtle setup
display.penup()
player.penup()

display.hideturtle()
player.hideturtle()
bubble.hideturtle()
enemy.hideturtle()
# introduction

window.bgcolor("sky blue")
display.setposition(-200, 100)
display.write("Use the arrow keys to move left and right", font = ['Arial', 20, 'normal'])

count_down_to_start = 5

display.setposition(-200 , -100)
display.write("The game will start in {} seconds." . format(count_down_to_start), font = ['Arial', 20, 'normal'])

time.sleep(2)

for count in range(5):
    time.sleep(1)
    count_down_to_start = count_down_to_start - 1
    display.clear()
    display.write("The game will start in {} seconds.".format(count_down_to_start), font = ['Arial', 20, 'normal'])

# more turtle setup

display.showturtle()
player.showturtle()
bubble.showturtle()



random_color = ["orange", "sky blue", "green", "red", "purple", "black", "turquoise"]

score = 2

display.clear()
display.setposition(0, 200)

display.write("Score: 0", font = ['Arial', 20, 'normal'])

bubble.color(random.choice(random_color))
enemy.color(random.choice(random_color))
display.hideturtle()
turtle.hideturtle()
enemy.showturtle()
player.color("red")
window.bgcolor("pink")
enemy.shape("square")
player.shape("turtle")
bubble.shape("circle")
enemy.turtlesize(1)
player.turtlesize(2)
enemy.penup()
bubble.penup()
player.penup()

window.tracer(0)

# functions


def forward_faster():
    player.forward(1)


def player_left():
    player.left(15)
    player.forward(1)


def player_right():
    player.right(15)
    player.forward(1)


def bubble_teleport():
    bubble.setx(random.randint(-400, 400))
    bubble.sety(random.randint(-250, 250))
    bubble.color(random.choice(random_color))
    bubble.dot(50)


def enemy_teleport():
    enemy.setx(random.randint(-400, 400))
    enemy.sety(random.randint(-250, 250))
    enemy.color(random.choice(random_color))
    enemy.shape("square")



# movement keys

window.onkeypress(player_left, "Left")
window.onkeypress(player_right, "Right")
window.onkeypress(forward_faster, "Up")
window.listen()

bubble_teleport()
enemy_teleport()
# game loop

while True:
    player.forward(player_speed)

    window.update()
#  if the player is touching bubble teleport

    if player.distance(bubble) < 40:
        bubble.clear()
        bubble_teleport()
        score = score + 2
        display.clear()
        display.write("Score: {}".format(score), font = ["Arial", 20, "normal"])
        enemy.clear()
        enemy_teleport()

#   if player is on the side of the screen teleport

    if player.xcor() > 600:
        player.setx(-600)
    if player.xcor() < -600:
        player.setx(600)
    if player.ycor() > 300:
        player.sety(-300)
    if player.ycor() < -300:
        player.sety(300)

# if the enemy is touching bubble teleport

    if enemy.distance(bubble) < 50:
        enemy.clear()
        enemy_teleport()

    if player.distance(enemy) < 40:
        enemy.clear()
        enemy_teleport()
        score = -1
        display.clear()
        display.write("Score: {}".format(score), font=["Arial", 20, "normal"])


    if score < 0:
        player.hideturtle()
        display.clear()
        enemy.hideturtle()
        bubble.hideturtle()
        display.setposition(-70, 0)
        display.write("you died", font=["Arial", 50, "normal"])
        time.sleep(200)



    if score == 10:
       enemy.turtlesize(2)
    if score == 20:
        enemy.turtlesize(4)
    if score > 22:
        player.hideturtle()
        display.clear()
        enemy.hideturtle()
        bubble.hideturtle()
        display.setposition(-70, 0)
        display.write("You Win!!!", font=["Arial", 50, "normal"])
        time.sleep(200)
Posted by: Guest on October-30-2021
2

how to make a game on python

#Easy game in python

import random
import time

Passwordlist = ['abc123', '1234', '123456', 'QWERT', 'ASFDJKL;', '0102', '0', '100', '1000', '10000', '123', '2048', '1024', 'JSON', '1234567890']

ans = input('Enter an account name: ')
time.sleep(1)

print('Finding the password for it...')
time.sleep(1)
print(random.choice(Passwordlist)+ ' is the password')
Posted by: Guest on October-06-2020
-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

how to make a game in python

#modules required - time and random
#Game 1
#Password game
import time
import random

Password = ['1', '2', 'a', 'G', '5', '89']

answer = input('Guess the password')
print(random.choice(Password)+' is the password')
Posted by: Guest on October-04-2020

Code answers related to "how to make game with python"

Python Answers by Framework

Browse Popular Code Answers by Language