Pygame Sprite Motion with Wrap-Around


import pygame

WIDTH = 500
HEIGHT = 500
FPS = 60

#define colors
#colors are defined in red,green,blue
#values from 0-255
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
  
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.Surface((50,40))
    self.image.fill(GREEN)
    self.rect = self.image.get_rect()
    self.rect.centerx = 30
    self.rect.centery = 30
    self.speedx = 0
    self.speedy = 0
    self.ticker = 0
  
  def update(self):
    speed = 5
    self.speedx = 0
    self.speedy = 0
    keystate = pygame.key.get_pressed()
    if keystate[pygame.K_LEFT]:
      self.speedx = -speed
    if keystate[pygame.K_RIGHT]:
      self.speedx = speed
    if keystate[pygame.K_UP]:
      self.speedy = -speed
      self.ticker += 1 #NEW CODE
    if keystate[pygame.K_DOWN]:
      self.speedy = speed
      self.ticker += 1 #NEW CODE
    self.rect.x += self.speedx
    
    
    #HERE IS WHERE WE 
    self.ticker = self.ticker % 20
    if self.ticker  >9:
      self.image.fill(GREEN)
    else:
      self.image.fill(BLUE)
    #HERE WE ARE GOING TO WRAP AROUND THE EDGES IF THE SPRITE
    #GOES OFF THE SCREEN
    #REMEMBER THAT X IS LEFT TO RIGHT AND Y IS UP AND DOWN
    if self.rect.x > WIDTH:
      self.rect.x = 0
    if self.rect.x < 0:
      self.rect.x = WIDTH
    self.rect.y += self.speedy
    if self.rect.y > HEIGHT:
      self.rect.y = 0
    if self.rect.y < 0:
      self.rect.y = HEIGHT
      
    

all_sprites = pygame.sprite.Group()
james = Player()
all_sprites.add(james)
running = True

while running:
  
  clock.tick(FPS)
  pygame.event.get()
  all_sprites.update()
  screen.fill(BLACK)
  all_sprites.draw(screen)
  pygame.display.flip()
    
 

Python Brickbreaker with Tkinter

 
from tkinter import *
import random
import time

tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
tk.update()

class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
        self.canvas.move(self.id, 245, 100)
        starts = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()
        
    def draw(self):
        self.canvas.move(self.id, self.x, self.y)
        pos = self.canvas.coords(self.id)
        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.y = -3
        if self.hit_paddle(pos) == True:
            self.y = -3
        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3
            
    def hit_paddle(self, pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                return True
        return False

class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.canvas.move(self.id, 200, 300)
        self.x = 0
        self.canvas_width = self.canvas.winfo_width()
        self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
        self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
        
    def turn_left(self, evt):
        self.x = -2
        
    def turn_right(self, evt):
        self.x = 2
        
    def draw(self):
        self.canvas.move(self.id, self.x, 0)
        pos = self.canvas.coords(self.id)
        if pos[0] <= 0:
            self.x = 0
        elif pos[2] >= self.canvas_width:
            self.x = 0
     
paddle = Paddle(canvas, 'blue')
ball = Ball(canvas, paddle, 'red')

while 1:
    ball.draw()
    paddle.draw()
    tk.update_idletasks()
    tk.update()
    time.sleep(0.01)

PyGame Basic Setup

 

import pygame
import random

WIDTH = 480
HEIGHT = 480
FPS = 30

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()

# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False

    # Update

    # Draw / render
    screen.fill(BLACK)
    # *after* drawing everything, flip the display
    pygame.display.flip()

pygame.quit()

 

 

Werewolves of Miller’s Hollow Roles

Werewolves

Each night, the werewolves pick 1 (this can change with the Personnages expansion pack) player to kill. The victim can be anyone except the Moderator, including other werewolves. The next day, they pretend to be a villager and try to seem unsuspicious. The number of werewolves in a game varies depending on the number of players.

Villagers

They don’t have any special power except thinking and the right to vote.

Seer/ Fortune Teller/ Oracle

Each night, they can discover the real identity of a player. They must help the other villagers but discreetly to not be found by werewolves.

Hunter

If they are killed by werewolves or eliminated by vote, they must immediately kill another player of their choice.

Cupido

The first night, Cupid chooses 2 players and make them fall in love, then becomes a villager. If one dies, the other dies too. A lover can’t vote against the other lover. If the lovers are a villager and a werewolf, their objective changes; they must eliminate all the players except them.

Witch

She has two potions:

  • one to save the werewolves’s victim
  • one to eliminate a player

She can only use each potion once during the game. She can use both potions during the same night. She can save herself if she has been attacked by the werewolves on the first night.

Little Girl

The little girl can secretly look at the werewolves during their turn. If she is caught in the act, she dies instead of the victim. Because she will be scared to death, or depending on how you play the werewolves will kill her later on in the game. It is also possible for her to die immediately along with the victim and can not be saved.

Sheriff/Captain

This card is given to a player besides their card. They are elected by a vote. This player’s vote counts for two instead of one. If they die, they will decide who will be the next Sheriff/Chief. The Sheriff/Chief can be of any role, including a werewolf.

Thief

If the thief is played, two cards more than the number of players need to be played. After each player receives a card, the two cards are put in the center of the table. The thief can,they want to, during the first night, exchange their cards with one of those cards that they will play until the end of the game. If the two cards are werewolf, the thief has to take one of the two werewolf cards, until the end of the game.

Game’s turns

1st Night Only

  • Thief
  • Cupid
  • Lovers

Every night

  • Werewolves
  • Little Girl
  • Seer
  • Witch

Python Turtle Demo Spirograph

 <br> import random,time,turtle<br>bai = turtle.Turtle()<br>bai.pendown<br>bai.speed(10)<br>bai.tracer(300)<br>bai.hideturtle()<br>for i in range (1000):<br>  for i in range(4):<br>   for i in range (40):<br>    bai.forward(50)<br>    bai.left(100)<br>    r = random.randint(0,255)<br>    g = random.randint(0,255)<br>    b = random.randint(0,255)<br>    bai.pencolor((r,g,b))<br>    for i in range(4):<br>       bai.forward(10)<br>       bai.left(90)<br>   time.sleep(0.1)<br>  bai.penup<br>  bai.forward(100)<br>  bai.pendown  

   <br><br> import turtle<br>import time<br>import random<br>bob = turtle.Turtle()<br>bob.tracer(300)<br>bob.pendown()<br>bob.hideturtle()<br>for i in range (20):<br>  for i in range(200):<br>    bob.forward(158)<br>    bob.left(200)<br>    bob.right(1)<br>    r = random.randint(1,255)<br>    g = random.randint(1,255)<br>    b = random.randint(1,255)<br>    bob.pencolor((r,g,b))<br>  time.sleep(0.1)    <br>

Hex/Binary/Base64

Base64 table[edit]

The Base64 index table:

IndexBinaryCharIndexBinaryCharIndexBinaryCharIndexBinaryChar
0000000A16010000Q32100000g48110000w
1000001B17010001R33100001h49110001x
2000010C18010010S34100010i50110010y
3000011D19010011T35100011j51110011z
4000100E20010100U36100100k521101000
5000101F21010101V37100101l531101011
6000110G22010110W38100110m541101102
7000111H23010111X39100111n551101113
8001000I24011000Y40101000o561110004
9001001J25011001Z41101001p571110015
10001010K26011010a42101010q581110106
11001011L27011011b43101011r591110117
12001100M28011100c44101100s601111008
13001101N29011101d45101101t611111019
14001110O30011110e46101110u62111110+
15001111P31011111f47101111v63111111/
Padding=

https://en.wikipedia.org/wiki/Base64

UTF8 – Hex

U+0021!21EXCLAMATION MARK
U+002222QUOTATION MARK
U+0023#23NUMBER SIGN
U+0024$24DOLLAR SIGN
U+0025%25PERCENT SIGN
U+0026&26AMPERSAND
U+002727APOSTROPHE
U+0028(28LEFT PARENTHESIS
U+0029)29RIGHT PARENTHESIS
U+002A*2aASTERISK
U+002B+2bPLUS SIGN
U+002C,2cCOMMA
U+002D2dHYPHEN-MINUS
U+002E.2eFULL STOP
U+002F/2fSOLIDUS
U+0030030DIGIT ZERO
U+0031131DIGIT ONE
U+0032232DIGIT TWO
U+0033333DIGIT THREE
U+0034434DIGIT FOUR
U+0035535DIGIT FIVE
U+0036636DIGIT SIX
U+0037737DIGIT SEVEN
U+0038838DIGIT EIGHT
U+0039939DIGIT NINE
U+003A:3aCOLON
U+003B;3bSEMICOLON
U+003C<3cLESS-THAN SIGN
U+003D=3dEQUALS SIGN
U+003E>3eGREATER-THAN SIGN
U+003F?3fQUESTION MARK
U+0040@40COMMERCIAL AT
U+0041A41LATIN CAPITAL LETTER A
U+0042B42LATIN CAPITAL LETTER B
U+0043C43LATIN CAPITAL LETTER C
U+0044D44LATIN CAPITAL LETTER D
U+0045E45LATIN CAPITAL LETTER E
U+0046F46LATIN CAPITAL LETTER F
U+0047G47LATIN CAPITAL LETTER G
U+0048H48LATIN CAPITAL LETTER H
U+0049I49LATIN CAPITAL LETTER I
U+004AJ4aLATIN CAPITAL LETTER J
U+004BK4bLATIN CAPITAL LETTER K
U+004CL4cLATIN CAPITAL LETTER L
U+004DM4dLATIN CAPITAL LETTER M
U+004EN4eLATIN CAPITAL LETTER N
U+004FO4fLATIN CAPITAL LETTER O
U+0050P50LATIN CAPITAL LETTER P
U+0051Q51LATIN CAPITAL LETTER Q
U+0052R52LATIN CAPITAL LETTER R
U+0053S53LATIN CAPITAL LETTER S
U+0054T54LATIN CAPITAL LETTER T
U+0055U55LATIN CAPITAL LETTER U
U+0056V56LATIN CAPITAL LETTER V
U+0057W57LATIN CAPITAL LETTER W
U+0058X58LATIN CAPITAL LETTER X
U+0059Y59LATIN CAPITAL LETTER Y
U+005AZ5aLATIN CAPITAL LETTER Z
U+005B[5bLEFT SQUARE BRACKET
U+005C\5cREVERSE SOLIDUS
U+005D]5dRIGHT SQUARE BRACKET
U+005E^5eCIRCUMFLEX ACCENT
U+005F_5fLOW LINE
U+0060`60GRAVE ACCENT
U+0061a61LATIN SMALL LETTER A
U+0062b62LATIN SMALL LETTER B
U+0063c63LATIN SMALL LETTER C
U+0064d64LATIN SMALL LETTER D
U+0065e65LATIN SMALL LETTER E
U+0066f66LATIN SMALL LETTER F
U+0067g67LATIN SMALL LETTER G
U+0068h68LATIN SMALL LETTER H
U+0069i69LATIN SMALL LETTER I
U+006Aj6aLATIN SMALL LETTER J
U+006Bk6bLATIN SMALL LETTER K
U+006Cl6cLATIN SMALL LETTER L
U+006Dm6dLATIN SMALL LETTER M
U+006En6eLATIN SMALL LETTER N
U+006Fo6fLATIN SMALL LETTER O
U+0070p70LATIN SMALL LETTER P
U+0071q71LATIN SMALL LETTER Q
U+0072r72LATIN SMALL LETTER R
U+0073s73LATIN SMALL LETTER S
U+0074t74LATIN SMALL LETTER T
U+0075u75LATIN SMALL LETTER U
U+0076v76LATIN SMALL LETTER V
U+0077w77LATIN SMALL LETTER W
U+0078x78LATIN SMALL LETTER X
U+0079y79LATIN SMALL LETTER Y
U+007Az7aLATIN SMALL LETTER Z

https://www.utf8-chartable.de/

7-Segment Display with Switch/Case

int a = 6;
int b = 5;
int c = 2;
int d = 3;
int e = 4;
int f = 7;
int g = 8;
int p = 9;

void setup() {
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  Serial.begin(9600);
}

int num = 1;
int xvalue;

void loop() {
  
  LEDplay(num);
  delay(1000);
  xvalue = analogRead(A1);
  
  if(xvalue &gt; 1000){
    num = num+1;
    num = num%10;
  }
  if(xvalue &lt; 10){
    num = num-1;
    num = num%10;
  }
  if(num &lt; 0){
    num = 9;
  }
  
}

void LEDplay(int num){
  switch (num) {
    case 0:
      zero();
      break;
    case 1:
      one();
      break;
    case 2:
      two();
      break;
    case 3:
      three();
      break;
    case 4:
      four();
      break;
    case 5:
      five();
      break;
    case 6:
      six();
      break;
    case 7:
      seven();
      break;
    case 8:
      eight();
      break;
    case 9:
      nine();
      break;
    default:
      //do nothing
      break;
  }
}

void one(){
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void two(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void three(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void four(){
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void five(){
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void six(){
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void seven(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void eight(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void nine(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, LOW);
}

void zero(){
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}